Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

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)?

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
holli
Asked:
holli
1 Solution
 
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.
0
 
inthedarkCommented:
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
 
inthedarkCommented:
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
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!

 
ventondCommented:
inthedark,
  I teach VB and we use arrays of public UDT's all the time. I guess I don't understand your comment.
0
 
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).

holli
0
 
inthedarkCommented:
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
 
Nitin SontakkeDeveloperCommented:
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
 
inthedarkCommented:
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
 
inthedarkCommented:
Soory for typo UTD=UDT
0
 
inthedarkCommented:
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
 
inthedarkCommented:
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

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now