can i call a udt`s member by name?

hi all.

can i call a user-defined-types member by his name, as i can do with objects (callbyname-function)?

type foo
  bar as integer
  baz as integer
end type

dim t as foo
dim s as string

now access "" using the variable "s"

is this possible?

regards, holli
Who is Participating?
Nitin SontakkeConnect With a Mentor DeveloperCommented:
I don't know how to react to inthedark's comment. However, i would like to add the following:

In principle, it is true that nothing is impossible. However, the fact remains that you cannot do something holli wants to do in VB.

It is for the reason that there could always be workarounds, I always address everybody with extreme politeness because i am distinctly aware of the fact that there could be ways to do it, which i might not be aware of.

I have been doing programming ever since man invented Fire. However, i was never impressed by the technological advances, etc. Because we, as a Humanity, has to go thru steps. The things we think virtually impossible today, will be routine things for tomorrow. This, however, doesn't make future man any smarter than todays. Because, the importance and efforts of the people who build the building blocks of todays technology will always valuable, wherever in time and place they may be.

By the way, from where the zHolder came into existance. Is it a DLL? A custom code? Is it some standard functionality provided? Never heard of it before.
Nitin SontakkeDeveloperCommented:
To the best of my knowledge NO. You cannot do such things in VB as it doesn't support macro substitution which i have used a plenty of times in Clipper. Being a compiler VB expects that it knows in advance all the variable names used.

However, you can do such things in VBScript (using EVAL), as it is an interpreter environment.

I hope you are with me.
I created a dynamic holder, where you can do things a little bit like that. I was inspired by the IIS Application and Session objects which can be used to hold anuthing a bit like a collection object but handle vb data types and objects too.

Dim Holder as New zHolder ' this is the only variable declaration you need in your function.

You can now put things in it like this:

No need to define "Title" just bung data in and it creates a field on the fly.

Holder("Title")="Sales Transaction"
Holder("Records") = Holder.NewHolder ' create a sub holder

Holder("Records")("Record1") = Holder.NewHolder


Or like this:

Dim SC ' ShortCut

Set SC = Holder("Records")("Record1")
SC("Field3") = "MoreDataButNeater"

Aned when you want to see what you have got in your holder:

Mgsbox Holder.Expose

Title=Sales Transaction
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.

The problem with UDT's is that you can't have them as public objects, or arrays either.  So thats why zHolder got invented.
  I teach VB and we use arrays of public UDT's all the time. I guess I don't understand your comment.
holliAuthor Commented:
ventond, inthedark is right. you cannot have udts as a public member of a class or object.

inthedark, the purpose of this question is to write a similar functionality. i`m doing this for practice.

so mr. sontake gets the points for the "no" if no one comes up with a solution (not a workaround).

The answer to your question holli is that nothing is impossible, it just looks different and may take longer and if somebody tells you it is impossible don't belive them just look harder! There is always a solution and in this case zHolder has so many advantages over the original concept, it just shows that the person asking the question of you is still in the stone age. Welcome to the future!
Scratches head and thinks......time passes.....

Holli, you were a bit quick on this one here is the answer as you can see it is possible to refer to a UTD member:

==========code for your form

Option Explicit

Private Type Foo ' User defined type
    Bar As New oInteger
    Baz As New oInteger
End Type

Private Sub Command1_Click()

Dim T As Foo
Dim S

T.Bar = 1234
T.Baz = 5678
Set S = T.Bar

S = 4321 ' referring to a UTD member

MsgBox CStr(S)

End Sub

=============End of form code=============
Place the following code in a text file and save it as  oInteger.cls and add the oInterger class to your project.
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
Attribute VB_Name = "oInteger"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Dim mItem As Integer
Public Property Get Item() As Integer
Attribute Item.VB_UserMemId = 0
Item = mItem
End Property

Public Property Let Item(ByVal vNewValue As Integer)
mItem = vNewValue
End Property
=================End of oInteger.cls

Soory for typo UTD=UDT
NitinSontakke, I am sorry if you got the impression that I was suggesting that you were in the stone age. The reason why I kick around on this site is for interest and challenge. Impressive answer. If you get sick of programming you could probably make it as an author too.
NitinSontakke, about the zHolder I wanted to achieve the following:

* Simple to use

* Each name can be a Value or another Sub-holder

* Infinitely recursable (no limit to sub levels)
  e.g. H("1")("2")("3")("4")

* Be able hold Business Objects (associated recordsets) example:

  One Sales Transactions has a number of Analysis records in a different tables
  Stock movements, and Invoice Lines, etc.

* I wanted to grab the business object in one simple function call.
  in ADO
  Holder = VIPBO.GetBusnessObject(CN, "ADO","Sales Transactions","I12345")
  or in DAO
  Holder =  VIPBO.GetBusnessObject(DB, "DAO","Sales Transactions","I12345")

* Be able to add other User Defined data to the associated recordsets.

* Persist the data on the clipboard

* Create an Evolution so that an entire Object can be pasted back into a database.
  Holder("KEY") = "I12346"
  VIPBO.Evolve DB, "DAO","Sales Transactions", Holder

* Persist the data in file with a registered file type.

* Simple Expose to ease debugging.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.