?
Solved

can i call a udt`s member by name?

Posted on 2002-05-23
11
Medium Priority
?
200 Views
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)?

eg.
type foo
  bar as integer
  baz as integer
end type

dim t as foo
dim s as string
s="bar"

now access "t.bar" using the variable "s"

is this possible?

regards, holli
0
Comment
Question by:holli
[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
11 Comments
 
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.
0
 
LVL 17

Expert Comment

by:inthedark
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

Holder("Records")("Record1")("Field1")="MyData"
Holder("Records")("Record1")("Field2")="MoreData"

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
Records=zHolder
    Record1=zHolder
        Field1=MyData
        Field2=MoreData
        Field3=MoreDataButNeater
0
 
LVL 17

Expert Comment

by:inthedark
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Expert Comment

by:ventond
ID: 7030866
inthedark,
  I teach VB and we use arrays of public UDT's all the time. I guess I don't understand your comment.
0
 
LVL 6

Author Comment

by:holli
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).

holli
0
 
LVL 17

Expert Comment

by:inthedark
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!
0
 
LVL 10

Accepted Solution

by:
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.
0
 
LVL 17

Expert Comment

by:inthedark
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.
==========oInter.CLS====================
VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
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

0
 
LVL 17

Expert Comment

by:inthedark
ID: 7034014
Soory for typo UTD=UDT
0
 
LVL 17

Expert Comment

by:inthedark
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.
0
 
LVL 17

Expert Comment

by:inthedark
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.



0

Featured Post

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!

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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.
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…
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
Course of the Month8 days, 3 hours left to enroll

765 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