Solved

can i call a udt`s member by name?

Posted on 2002-05-23
11
191 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
11 Comments
 
LVL 6

Expert Comment

by:Nitin Sontakke
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 2

Expert Comment

by:ventond
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 17

Expert Comment

by:inthedark
Comment Utility
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 6

Accepted Solution

by:
Nitin Sontakke earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Soory for typo UTD=UDT
0
 
LVL 17

Expert Comment

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now