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

x
?
Solved

Type References... (?)

Posted on 1998-10-22
5
Medium Priority
?
169 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 40 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…
Suggested Courses

824 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