Solved

Type References... (?)

Posted on 1998-10-22
5
159 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

758 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

22 Experts available now in Live!

Get 1:1 Help Now