user define data type in visual basic

6
Visual basic 6.0 ser Defined Data type And With stateme

Upload: shubham-dwivedi

Post on 15-Apr-2017

277 views

Category:

Software


0 download

TRANSCRIPT

Page 1: User define data type In Visual Basic

Visual basic 6.0 User Defined Data type And With statement

Page 2: User define data type In Visual Basic

User-Defined Data Types in Visual Basic 6Variables of different data types when combined as a single

variable to hold several related informations is called a User-Defined data type.

A Type statement is used to define a user-defined type in the General declaration section of a form or module.

User-defined data types can only be private in form while in standard modules can be public or private.

An example for a user defined data type to hold the product details is as given below.

Private Type ProductDetails ProdID as String

ProdName as StringPrice as Currency

End Type

Page 3: User define data type In Visual Basic

The user defined data type can be declared with a variable using the Dim statement as in any other variable declaration statement. An array of these user-defined data types can also be declared.

An example to consolidate these two features is given below.

Dim ElectronicGoods as ProductDetails ' One RecordDim ElectronicGoods(10) as ProductDetails ' An array of 11 records

Page 4: User define data type In Visual Basic

A User-Defined data type can be referenced in an application by using the variable name in the procedure along with the item name in the Type block. Say, for example if the text property of a TextBox namely text1 is to be assigned the name of the electronic good, the statement can be written as given below.

Text1.Text = ElectronicGoods.ProdName

If the same is implemented as an array, then the statement becomes

Text1.Text = ElectronicGoods(i).ProdName

User-defined data types can also be passed to procedures to allow many related items as one argument.

Sub ProdData( ElectronicGoods as ProductDetails)Text1.Text = ElectronicGoods.ProdNameText1.Text = ElectronicGoods.Price

End Sub

Page 5: User define data type In Visual Basic

With statementWhen properties are set for objects or methods are called, a lot

of coding is included that acts on the same object. It is easier to read the code by implementing the With...End With statement.

Multiple properties can be set and multiple methods can be called by using the With...End With statement. The code is executed more quickly and efficiently as the object is evaluated only once. The concept can be clearly understood with following example.

With Text1.Font.Size = 14.Font.Bold = True.ForeColor = vbRed.Height = 230.Text = "Hello World"

End With

In the above coding, the object Text1, which is a text box is evaluated only once instead of every associated property or method. This makes the coding simpler and efficient.

Page 6: User define data type In Visual Basic