Solved

Easy points: Initializing a class

Posted on 2004-03-22
3
152 Views
Last Modified: 2010-05-01
I have a really easy question for everyone - it really sucks, but I've never dealt with new classes before. My question is: How do I initialize a new class in Visual Basic? I want the new class to have 1 undefined variable and 2 Integers. And, after the class is already defined, to use the variable in my code to create an array of this class. How can all this be done?

Thank you in advance for your help!
0
Comment
Question by:razar
3 Comments
 
LVL 2

Expert Comment

by:TeddyZero
ID: 10651527
This sample is in VB.NET

    Public Class AClass
        Private Var1 As Object
        Private Int1 As Integer
        Private Int2 As Integer
        Public Sub New()
        End Sub
        Public Sub New(ByVal MyVar1 As Object, ByVal MyInt1 As Integer, ByVal MyInt2 As Integer)
            Var1 = MyVar1
            Int1 = MyInt1
            Int2 = MyInt2
        End Sub
        Public Sub SetValues(ByVal MyVar1 As Object, ByVal MyInt1 As Integer, ByVal MyInt2 As Integer)
            Var1 = MyVar1
            Int1 = MyInt1
            Int2 = MyInt2
        End Sub
        Public Property MyVar1() As Object
            Get
                MyVar1 = Var1
            End Get
            Set(ByVal Value As Object)
                Var1 = Value
            End Set
        End Property
        Public Property MyInt1() As Integer
            Get
                MyInt1 = Int1
            End Get
            Set(ByVal Value As Integer)
                Int1 = Value
            End Set
        End Property
        Public Property MyInt2() As Integer
            Get
                MyInt2 = Int2
            End Get
            Set(ByVal Value As Integer)
                Int2 = Value
            End Set
        End Property
    End Class

Simple, declare an array of the type AClass, like this:
    Public MyArray(2) As AClass

I hope this help's you.
Enjoy it. :-)
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 50 total points
ID: 10652415
For VB6.

This is a class (name it CEmployee):

Option Explicit

Private mvarFirstName As String
Private mvarLastName As String
Private mvarSalary As Currency

Public Sub Hire()
    MsgBox FirstName & " " & LastName & " hired!"
End Sub

Public Property Let Salary(ByVal vData As Currency)
    If vData < 25000 Then
        MsgBox "Salary must be more than 25,000"
    Else
        mvarSalary = vData
    End If
End Property
Public Property Get Salary() As Currency
    Salary = mvarSalary
End Property


Public Property Let LastName(ByVal vData As String)
    mvarLastName = vData
End Property
Public Property Get LastName() As String
    LastName = mvarLastName
End Property


Public Property Let FirstName(ByVal vData As String)
    mvarFirstName = vData
End Property
Public Property Get FirstName() As String
    FirstName = mvarFirstName
End Property


On a form of the same project:

Private Sub Command1_Click()
Dim x As New CEmployee

    With x
        .LastName = "Moreau"
        .FirstName = "Éric"
        .Hire
    End With
End Sub

0
 

Author Comment

by:razar
ID: 10722731
Thank you both, guys. I figured out the thing myself, but I give emoreau the points since his code helped me more! I programme in VB6, not in .NET - sorry I didn't mention that earlier!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question