Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 37,677 times

Contents

Related Categories

Variables - User-defined Types

User-defined Types

In Visual Basic, you can declare you own types or classes in a module or form. This will then behave as any other object in Visual Basic, except it only has properties. For example, the Screen object in Visual Basic exposes a number of properties, including FontCount and ActiveControl. These can be accessed by entering Screen.FontCount. You can create a custom version of this, that will allow you to define various elements that can store values. For example:

Type EmployeeRecord    ' Create user-defined type.
    ID As Integer    ' Define elements of data type.
    Name As String * 20 ' Fill with 20 spaces
    Address As String * 30 ' Fill with 30 spaces
    Phone As Long
    HireDate As Date
End Type

This user-defined type has 5 properties. At the moment you cannot access them, as Visual Basic requires that you declare another variable that is referenced to this user-defined type:

Dim MyRecord As EmployeeRecord

Note that if you want to declare a Type in a form, it must be declared as private. So, the following code fills a few values into this user-defined type:

Private Type EmployeeRecord    ' Create user-defined type.
    ID As Integer    ' Define elements of data type.
    Name As String * 20 ' Fill with 20 spaces
    Address As String * 30 ' Fill with 30 spaces
    Phone As Long
    HireDate As Date
Private End Type

' Reference variable to user-defined type
Dim MyRecord As EmployeeRecord    ' Declare variable.

' Comment1_Click event.
Sub Command1_Click()
    ' Assignment to EmployeeRecord variable must occur in a procedure.
   
    MyRecord.ID = 12003    ' Assign a value to an element.
    MyRecord.Phone = 1813670690
End Sub

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • new solution

    Posted by jawsper on 17 Apr 2004

    i just found out how i can make my original idea work.

    i read something about the "eval" function in the microsotf script controls, so now ik kan just say: ScriptControl1.Eval(line) and it will fin...

  • Posted by LouisRose on 02 Mar 2004

    Hey cwamsley,

    Welcome to DevFusion!

    This sounds like a control array problem - if you're using VB (and not .NET) that is. You can have several controls with the same name, but with an index, lik...

  • String to Object Continued

    Posted by cwamsley on 01 Mar 2004

    Louis,

    I am working on a somewhat similar problem (to the String to Object comments from Aug 5, 2003) and hope you can suggest a course of action.

    I'm trying to create a simple way to set and mo...

  • Posted by Rde on 01 Mar 2004

    The following lists the public, private, and default visibility
    for the various elements you can declare in your programs.

    Default means declaring without anything proceding (or with Dim
    in the c...

  • Posted by LouisRose on 05 Aug 2003

    I'm glad that you came up with a solution.

    For muti-lingual support you best bet is to use a Resource file. Then the User's language and Locale will be detected automatically and your program will ...