Solved

Easy points: Initializing a class

Posted on 2004-03-22
3
151 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
Comment Utility
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 69

Accepted Solution

by:
Éric Moreau earned 50 total points
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now