Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


can i call a udt`s member by name?

Posted on 2002-05-23
Medium Priority
Last Modified: 2006-11-17
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 "t.bar" using the variable "s"

is this possible?

regards, holli
Question by:holli
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
LVL 10

Expert Comment

by:Nitin Sontakke
ID: 7029919
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.
LVL 17

Expert Comment

ID: 7030763
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
LVL 17

Expert Comment

ID: 7030768
The problem with UDT's is that you can't have them as public objects, or arrays either.  So thats why zHolder got invented.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 7030866
  I teach VB and we use arrays of public UDT's all the time. I guess I don't understand your comment.

Author Comment

ID: 7031437
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).

LVL 17

Expert Comment

ID: 7031560
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!
LVL 10

Accepted Solution

Nitin Sontakke earned 400 total points
ID: 7031869
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.
LVL 17

Expert Comment

ID: 7034011
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

LVL 17

Expert Comment

ID: 7034014
Soory for typo UTD=UDT
LVL 17

Expert Comment

ID: 7034031
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.
LVL 17

Expert Comment

ID: 7034068
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.


Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
Suggested Courses

604 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