Solved

Type References... (?)

Posted on 1998-10-22
5
165 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
[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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

734 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