Sunteți pe pagina 1din 10

Paper solution for Visual Basic 6 November 2005

Q1. Arithmetic operators


A The arithmetic operators used in VB are as follows
+ : Addition - : Subtraction * :Multiplication /:
Division
The order in which operations are performed 3
determines the result. For example
4*5–3
This expression will result into 17 if the multiplication
is done first and will result into 8 if subtraction is done
first. The order of precedence, in arithmetic
expressions from high to low is
1.Exponentiation
2. Multiplication & Division
3. addition & Substraction
In the above example if multiplication is to be
performed first then it is to be written as ( 4* 5) – 3
Multiple operations at the same level are performed
left to right. For example 12 / 6 * 2 will result in 4 and
not into 1. The first operation is 12 / 6 and 2 * 2 is the 3
second.

Logical Operators

There are three logical operators used in VB. They are


OR : If one condition or both conditions are true then
the entire condition is true.
Ex. txt1.Text =2 Or txt2.Text = 4

And : Both conditions must be true for the entire


condition to be true.
Ex. txt1.Text =2 And txt2.Text = 4

Not : Reverses the condition so that a true condition


will become false and vice versa.
Ex. Not txt1.Text = 2
These operators can also be combined to create
compounded conditions. I f a condition has both And
and an Or, the And is evaluated before Or. However
the order of precedence can be changed by using
paranthesis.
Ex.
If txt1.Text = 2 Or txt2.Text2.Text = 3 and txt3.Text4
= 4 Then
‘ do the following
End If

Q1. VB has four data formatting functions


B 1. FormatCurrency
2. FormatNumer
3. FormatPercent
4. FormatDateTime

1: FormatCurrency (Expression to format) 2


syntax : FormatCurrency(Expression [,
NumberOfDecimalPositions [, LeadingDigit
[,
UseParenthesisForNegative [,
GroupForDigits] ] ] ]

The FormatCurrency returns a string of characters


formatted as dollars and cents. By default the
currency value displays a dollar sigh, commas, and
two positions to the right of the decimal place. The
default format can be changed by changing the
computer’s regional settings.
Function
Output
Ex : FormatCurrency(1234.56) 2
$1,234.56
FormatCurrency( .56)
$0.56

2. FormatNumber(Expression)
syntax : FormatCurrency(Expression [,
NumberOfDecimalPositions [, LeadingDigit
[,
UseParenthesisForNegative [,
GroupForDigits] ] ] ]

The FormatNumber function will generally format the


number with commas and no digits to the right of the
decimal point. You can specify the exact number of 2
decimal places to be displayed in the number by using
the second argument.
Function
Output
Ex : FormatNumber(1234.56)
1,234
FormatNumber( 2345.567,1)
2,345.5
FormatNumber( 2345.567,2)
2,345.56

3. FormatPercent(Expression)
Syntax : FormatPercent( Expression [, _
NumberOfDecimalPositions [, LeadingDigit
_ 2
[, UseParenthesisForNegative [,
GroupForDigits ] ] ] ]

It is used to display numeric values as a percent. This


function multiplies the argument by 100, adds a
percent sign, and roumds to two decimal places.
Ex: Function Output
FormatPercent( .42)
42%
FormatPercent( .42,1)
42.0%
FormatPercent( .241)
24%

4. FormatDateTime(Expression)
Syntax : FormatDateTime( Expression [,
NamedFormat] )

This function is used to format an expression as date


and or time. The expression may be a string that
holds a date or time value, or a function that returns
date. The named format uses your computer regional
settings. If the named format is not specified the
function returns date using vbGeneralDate.
The various named formats are as follows
Named Format Returns
Example
VbGeneralDate If exp holds a date, returns 11/11/05
1:20:04PM
a short date. If it holds time
, returns a long time. If it
holds both, returns both a
long date and time.
VbLongDate Day, Month day, Year Friday,
November 11, 2005
vbShortdate MM/DD//YY 11/11/05
vbLongTime HH:MM:SS AM/PM 1:20:04 PM
vbShortTime HH:MM(24 hour clock) 13:20

Q1. Full marks can be allotted if the entire application is


C right (Using ay logic and following the specifications
given ). The student is expected to draw the layout of
the application, use standard naming conventions
unless specified in the layout and write the code
properly.
Marks can be deducted if you feel that the logic is
wrong.

Q2. Visual Basic is an Event-Driven programming 2


A language. When you write any VB application you
must decide how the application interacts with the
user. In other words, you must decide how each
control reacts to users actions, such as click of a
mouse, keystroke, and so on, and you must program
these reactions. You program to react to various
external conditions called Events, and the user’s
actions determines the application flow.
In proper terms Events determine the control’s
reactions to external conditions. Some common
events are Click, GotFocus etc.
Events are written as subroutines. For Ex the Click
event for a CommandButton Command1 would be
written as
Private Sub Command1_Click( )
‘Action to be performed
End Sub

In VB you have to work with Objects, which have 2


properties and methods associated with it.

Properties
Properties tell something about an object, such as its
name, color, size, location on the form etc. You can
think of properties as adjectives that describes
objects. There are some properties that are only
available at design time, some only during run time
and some are available during both.
When you refer to a property, the name of the object 2
is written first followed a period ( . ) and then the
property name. For example the Text property of a
TextBox called txt1 will be written as
txt1.Text

Methods
Actions associated with objects are called Methods.
Methods are verbs of object-oriented programming.
Some common methods are Print, Resize, Clear etc.
You refer to methods as
ObjectName.Method Name
Ex. Form1.Print

Q2. Hide and Unload


B Any two differences 4
One example each 2

Q2. i) Dynaset 2
C A Dynaset is a temporary set of data taken from one
or more tables in a databse. A dynaset may be a
querry that was defined in an Access database, a
single table, a table subset of a table, or the result of
joining multiple tables. Like a table, a dynaset is
updatable if the database is not locked or opened for
ReadOnly. Dynasets are updated every time the user
changes the database, and changes they make to the
corresponding RecordSet are reflected in the
underlying tables. It is the most powerful and flexible
type of a recordset.
ii) Itemdata 2
This is a property for ListBoxes and ComboBoxes
which is useful while working with indixes. The
ItemData property can associate a specific number
with each item in the list. Each element of the list
property can have a corresponding ItemData that
does not change even when the list is sorted.
You can set initial values for the ItemData property,
just as you set the initial values for the List property. If
you add an item to the list during run-time, you can
also add a corresponding value to the ItemData
property.
Syntax : Object.ItemData(Index) = Value
The values placed in the ItemData property need not
be in any sequence.
2
iii) Snapshot
A Snapshot recordset, like a dynaset may be taken
from one or more tables. The difference is that the
Snapshot is not updatable and are also not live. A
snapshot recordset is like a photograph – a picture of
reality at any given point in time. 2

iv) NewIndex
It is a property for a ListBox or a ComboBox Control. If
the list is sorted the AddItem method adds the new
item alphabetically in the list. You can determine the
index of the new item using the NewIndex property,
which VB will set to the index of the new item. The
NewIndex property can be used with sorted as well as
unsorted list.
Example : list1.AddItem text1.Text
List1.ItemData(list1.NewIndex) = text2.Text

Q3. A class is a collection of objects and properties. You 2


A can create your own classes in VB by defining
variables, which are the properties of the class.
Theoretically you can declare these variables as Public
so that a;; other project code could set and retrieve
their values. However, this approach violates the rules
of encapsulation which requires each object to be in
charge of its own data. Hence you will declare all class
variables as Private.
When your program creates objects from the class,
you will need to assign values to the properties.
Because the properties are private variables, you will
use special Property procedures to pass the values to
the class module and to return values from the class
modules.
To allow the class to set its properties you have to
code the Property Procedure Let and to retrieve the
value of nay property, a Get Property procedure is to
be used. 2
When you define the Property Let & property Get, the
name you used becomes the name of the property to
the project.

Property Get
Syntax: [Public] Property Get
ProcedureName( Optional _ 2
ArgumentList ] ) [ As Datatype ]
‘statements in Procedure
ProcedureName = PropertyName
End Procedure

Property Let
Syntax: [Public] Property Let
ProcedureName( Optional _
ArgumentList , ] IncomingValue [ As
Datatype ] )
‘statements in Procedure
PropertyName = IncomingValue
End Procedure

Q3. 1) If……..Then…….Else 2
B 2) Nested If 2
Example using option Buttons 2

Q3. Program using Instr function or any other suitable 8


C logic

Q4. Rules
A i) Must begin with a letter 3
ii) Cannot contain an embedded period or any of
the type decleration character
iii) Should not exceed 255 characters
iv) Must be unique within its scope
Naming conventions
i) Must be meaningful
ii) Should have a lower case prefix that specifies 3
its datatype
iii) Capitalize each word of the name following the
prefix

Q4. Variables and Constants


B Memory locations that hold data and can be changed
during the project execution are called Variables 3
Variables are defined by using the keyword Dim
followed by the variable name followed by the
datatype
Dim intVar1 as Integer

Memory locations that hold data and cannot be 3


changed during the execution of the project are called
constants.
The constants are declared as follows
Const intConstant1 As Integer = 10
This IntConstant1 is declared as Integer Constant and
its value cannot be changed in its scope

Q4. Validation 2
C Program (Any logic can be used) 6

Q5. Objects available on any other form can be used by


A writing the form name first followed by a period, then
the object name followed by one more period to 3
access the objects properties.
FormName.Objectname.PropertyName

Ex. To assign the caption of a command button cmd2


on form2 to a TextBox txt1 on form1 the code will be
as follows
Text1.Text = form2.cmd2.Caption 3

Sample Program

Q5. Output Mode : Creates a new file 3


B Input File :- Reports an error
Example 3
Q5. Program 8
C (Any logic)

Q6. Advantages of Using Variant variables 3


A i) Flexiliblity
ii) If you assign an integer value to a variant, VB
treats it as integer
iii) If you assign a string to a variant, VB treats it
as string
iv) Can hold different data types in the run of the
same prog
v) VB will automatically perform all the required
conversions 3
Disadvantages
i) Very expensive in terms of memory
ii) Has different memory requirements
depending on the data
Holding Numbers 16 Bytes
Holding Characters 22 bytes plus 1
byte for
each
character
Q6. Program to change the font Name, face etc. using 6
B Common Dialog Control
Q6. Tasks performed by the Open statement of Data file in 8
C Output mode
i) Checks the directory for the file. If it does not
exists, it is created. If the file already exists, it
will be overwritten.
ii) For sequencial files a buffer is created in
memory
iii) A file pointer is created and set to the
beginning of the file
The file is given a file number for future references.

Q7. i) 1234.45 v) 0 1
A ii) 0 vi) 1234 0
iii) 0 vii) 12.23
iv) 1 viii) 100

Q7. Same as Q1. B 6


B
Q7. Correct logic using given specifications 1
C 0



S-ar putea să vă placă și