Solved

Easy points: Initializing a class

Posted on 2004-03-22
3
155 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
[X]
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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

759 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