Solved

Type References... (?)

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…

821 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