Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 159
  • Last Modified:

Easy points: Initializing a class

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
razar
Asked:
razar
1 Solution
 
TeddyZeroCommented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
razarAuthor Commented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now