Easy points: Initializing a class

Posted on 2004-03-22
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!
Question by:razar
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

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
                MyVar1 = Var1
            End Get
            Set(ByVal Value As Object)
                Var1 = Value
            End Set
        End Property
        Public Property MyInt1() As Integer
                MyInt1 = Int1
            End Get
            Set(ByVal Value As Integer)
                Int1 = Value
            End Set
        End Property
        Public Property MyInt2() As Integer
                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. :-)
LVL 70

Accepted Solution

É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"
        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"
    End With
End Sub


Author Comment

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!

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

617 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