Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Selectable" structure data type

Status
Not open for further replies.

Motor11

Technical User
Jul 30, 2002
60
US
Is there a way to choose the data type of a structure in code? A silly example to illustrate the idea:

type mystruct
x as double
y as double
end type

private sub testsub()
dim myvariable as mystruct
dim flag as string

flag = "x"

myvariable.flag = 10
end sub


Of course, this doesn't work but perhaps there is a way to accomplish this?

Thanks in advance,
Ryan
 
Try this in declarations:
Code:
Private Type myType
x As Variant
y As Variant
End Type

Then this on a command button
Code:
Dim myVar As myType
myVar.y = "Fred"
myVar.x = 3
MsgBox myVar.x & vbCrLf & myVar.y
Dim myVar1 As myType
myVar1.x = "Fred"
myVar1.y = False
MsgBox myVar1.x & vbCrLf & myVar1.y

Is that what you are after? Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Thanks for the response John. Let me re-phrase my question, because that wasn't quite what I was after.

I have a structure variable with numerous elements (ie: time, x, y, z....)

I have a subroutine that plots time vs. x, or time vs. y...

Can I reference X or Y with the same piece of code, for example:

type mystruct
time as double
x as double
y as double
.
.
.
end type

private sub testsub()
dim myvariable as mystruct
dim flag as string

flag = "x"

call plotpoints(myvar.flag)
end sub


Or, am I forced to code it as:

if flag = "x"
call plotpoint(myvar.x)
else
if flag="y"
call plotpoint(myvar.y)
endif
endif

Does this make sense? Probable not as I'm beginning to confuse even myself. :)

Regards,
Ryan
 

No you cannot do what you are wanting to do the way you are doing it. However, you can add the "flag" element to your UDT if you wanted and could then test it like you would in your "forced to code".

[tt]
If MyVar.Flag = X then 'OR "X"[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top