Solved

Type References... (?)

Posted on 1998-10-22
5
160 Views
Last Modified: 2010-04-30
I have created a "type" object in VB5.  Is it possible to refer to an item in the object with a variable?  For Example:

Type MyType
  Item1 as string
End Type

Dim SomeVariable as String
SomeVariable = "Item1"

MyType.SomeVariable = "yadayadayada"

MyType.Item1 should now equal "yadayadayada"

Obviously, it doesn't work just like that--is there some other way to do this?
0
Comment
Question by:IsleOfView
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:rmichels
ID: 1441212
Create a Class and use the following in the class



Private item1 As String
Private item2 As String


Property Get ItemData(sIndex As String) As String


    Select Case UCase(sIndex)
        Case "ITEM1"
            ItemData = item1
        Case "ITEM2"
            ItemData = item2
        Case Else
            ItemData = ""
    End Select

End Property

Property Let ItemData(sIndex As String, sData As String)

    Select Case UCase(sIndex)
       Case "ITEM1"
            item1 = sData
        Case "ITEM2"
            item2 = sData
    End Select

End Property


Then you can create that object as follows

Dim c as Class (class is what ever you name your class)
Dim s as string

s = "ITEM1"

c.ItemData(s) = "Hello"


0
 
LVL 3

Expert Comment

by:rmichels
ID: 1441213
I forgot to add, if you want to add more variables, just add more private ones at the top of the class and add conditions to the Case statements in the Get and Let properties of the class
0
 

Author Comment

by:IsleOfView
ID: 1441214
This works fine with a single object (or class), but I need to use a dynamic array of these classes.  For some reason, I am able to "ReDim Preserve" once, and then my app quits processing with no error (it just doesn't continue through the routine).  Here is some test code i put together:

Public ClassArray() As New MyRecords

ReDim ClassArray(0)
Dim i As Integer
Dim j As Integer
j = 10

For i = 0 To 4
    ClassArray(i).SomeProperty = j
    MsgBox "successfully made assignment number " & i + 1
    i = i + 1
    j = j + 10
    ReDim Preserve ClassArray((UBound(ClassArray) + 1))
    MsgBox "successfully Redimmed to " & UBound(ClassArray)
Next i

I only get one set of message boxes...This only goes through one time.
0
 
LVL 3

Accepted Solution

by:
rmichels earned 10 total points
ID: 1441215
I'm confused..did part of my original answer help you out? If it did, but you have more questions, just leave the question as anwered/locked and ask for clarification.

The code example you gave, you are incrementing i (i = i + 1), but the Next loop does that for you.  Not sure why it only runs through once.  With your i=i+1 it should run through twice.

0
 

Author Comment

by:IsleOfView
ID: 1441216
Thanks...your answer did help immensely...I found errors elsewhere in my code.  *thwaps self*
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
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…

929 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

15 Experts available now in Live!

Get 1:1 Help Now