Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Variable --> PropertyName

Posted on 1999-06-22
Medium Priority
Last Modified: 2010-05-02
I have a file like this:


I also have a Class:

As i don't know the order of lines written in the file,
i need to assign the PropValue (the 2nd field of the line of the file) to the right property, by reading the property name in the 1st field of the file.

Question by:parduz
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
  • 7
  • 4
  • 2
  • +5

Expert Comment

ID: 1519045
You must have Visual Basic 6.0

Use the CallByName Function, you can return or set or let a property by using it.

Dim mVarName as String
mVarName = "Caption"
CallByName Me, mVarName, vbLet, "TEST"

This following example would set the caption of the object (or Me in this case).

That is the only way I know how to do it.

If it is not in VB6 Then I don't think there 'is' any way to do so.

Author Comment

ID: 1519046
You're in right. In VB5 your answer don't work. (don't exist the "CallByName" function)
For now, i reject your answer, but if is really impossible to make this in VB5, i put a comment here, you repropose the answer, and i give you the points.
Thanks a lot.

LVL 15

Expert Comment

ID: 1519047
If you know all properties of the class, you can use a big Select Case statement
    Select Case propName
    Case "Property1"
        class.Property1 = propValue
    Case "Property2"
        class.Property2 = propValue
    End Select

If you don't know this info, you'll need to use TypeLib Information dll to get property names and a third party tool or scripting dll to set values. And to get all this working (COMPLETE AND WORKING), you'll need many EE points.
If you are very busy, save your time and go for VB6.
Industry Leaders: 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: 1519048
That would be a bad idea ameba.  If He wanted something that would make it possible where he only needed the Properies name and wanted it to be changable without CHANGING any code then that would be the wrong way to go.
LVL 15

Expert Comment

ID: 1519049
>That would be a bad idea ameba.
AllenC_Jr, perhaps you can provide better info on what to do/what is possible in VB5!? Ah, you don't.

>that would be the wrong way to go.
I said: save your time and go for VB6. I don't see anything wrong in my arguments. Do you?

Author Comment

ID: 1519050
I cannot go to VB6, is my firm that decide software upgrade, and my boss say:
"What you want? We have buy VB5ent, Office97pro, etc etc etc, and after a year you need upgrade? WE STAY HERE" (Augh!)

Regarding "a third party tool or scripting dll" and EE points, if this is a °increase points" requst, i can go to 347 max, but i don't like much third party controls, i try to mantain my code the must "internal" possible.
LVL 18

Expert Comment

ID: 1519051
In my experience I would go with ameba's suggestion.  It'll work, you'll get the results you need and you will be able to maintain it.  When/if you go to VB6 you'll be able to go with the more elegant solution.
LVL 12

Expert Comment

ID: 1519052
Ummm, there is an answer.

dim obj as object
set obj="object.property1"
obj = propertyValue

I've done this with form names and captions and such. I don't have the code at my fingertips, but I think that how I did it.



Author Comment

ID: 1519053
Sorry, but i can't figure how can i use this code.
If i put your code i have a "Type mismatch" compile error.

But i think can be on the right way; i put here a reply to a mail i send
to Rod Stephens (!):
>Unfortunately I don't know how to do that in VB5. If you are designing the
>object, you could store the properties in a collection using their names
>as keys. The normal property procedures could return the values from
>the collection, and you could provide access to the collection to get them by
>name. I don't know how to do this with an object you are not creating.

My poor english and my poor VB knowledge make me difficult to understand this answer.

Note for AMEBA:
The last phrase apply also to your first comment.
I don't know the dll you talk, and the thirdy app too.
Please, can you explain better for a Italian stressed programmer?
Thanks to all

LVL 15

Expert Comment

ID: 1519054
Sorry, I am not good in explaining things.
It seems, you know all properties of your class. You are doing this for your own classes?

Answer (say if it is wrong):
Yes, I know all property names. I don't need to extract these names from DLL.

Good. Then you don't need typelib dll I mentioned.

Here is the simple solution ("big Select Case")
' code can be something like this
Private Sub Form_Click()
    ' Create instance of your class Class1
    Dim obj As Class1
    Set obj = New Class1
    ' Open your properties file
    Dim numfile As Integer, filetxt As String, FileName As String
    FileName = "props.txt"
    numfile = FreeFile
    Open FileName For Input As #numfile
    ' main loop
    Do Until EOF(numfile)
        ' read one line
        Line Input #numfile, filetxt
        Dim propname As String, propvalue As Variant
        ' now you'll need a small parser for your line
        ' it reads a line of text (filetxt), sample: "Height",55
        ' it fills propname (part of filetxt before comma)
        '      and propvalue (part of filetxt after comma)
        Call ParseProps(filetxt, propname, propvalue)
        ' biiig select case, include all property names here
        Select Case propname
        Case "Property1"
            obj.Property1 = propvalue
        Case "Property2"
            obj.Property2 = propvalue
        End Select
    Close #1
End Sub


Author Comment

ID: 1519055
don't worry explaining things, the problem is my english knowledge and my self-made VB formation: i have problem understandig a italian friend that have 15 years of informatic school; when we discuss about VB, he use terms i don't know, we talk for an hour and i discover that i know the concept he explains, but not with the words he uses!

About your code, it's right, but i have 9 classes with 10 to 30 properties each to do this work, then i take the "select case" solution as a last resource.

I thing that creating a collection of properties can be useful to solve this problem, but i can't figure how i can make this.


Expert Comment

ID: 1519056
You might try an array of controls or classes, like Control(1).Property, Control(2).Property, etc. Then, you can access them as Control(i).Property.

Author Comment

ID: 1519057
for hddp66:
my problem is that i dont know the property name i'm reading in the file.
using index of an array of properties requires the "select case" procedure mentioned by ameba,,,

Author Comment

ID: 1519058
This is a great advance to the solution of this problem,
from Mark Dominic Hurst (visit his site to, we all can learn a lot about VB!):

In the Class i have, i put a new property:

    Private m_colProps As Collection

    Public Property Let Properties(ByVal Name As String, _
                                   ByVal Val As Variant)
        m_colProps.Remove Name
        m_colProps.Add Val, Name
    End Property

    Public Property Get Properties(ByVal Name As String) As Variant
        Properties = m_colProps(Name)
    End Property

    Sub Class_Initialize
        Set m_colProps = New Collection
        m_colProps.Add "", "MyName"
        m_colProps.Add CInt(0), "MyVal"
    End Sub

Now we can use in the app
that is very close to the solution of my problem.

Now, how we can copy the data stored in the collection to the right property of the class?

LVL 15

Expert Comment

ID: 1519059
You can add collections and make things look cool and complicated, if this makes you happy.
The good answer to your requirements is my 'biiig Select Case' solution.
But, you are the boss of this question and it is your right to decide what is good.

Accepted Solution

fab1970 earned 300 total points
ID: 1519060
This may be the Answer:
Is a "mix" between the "Select Case" proposed by ameba, and the "collection" proposed by parduz.
parduz must write ALL the prop in the class, and creating the collection.
But, in the "main" code, he can access the prop as usual or by name.
Now, the code:
--------------------Class code------------------------
' Class "PropertyByName" code:

Option Explicit
' All properties of the Class have his variable in an array, _
 instead of a variable each
Private Real_Properties_Variables(1 To 3) As Variant
' The collection "save" an index related to the "Key"
Private Properties_Names As Collection

Private Sub Class_Initialize()
    Set Properties_Names = New Collection
    ' For each property, add in the collection his index, _
    an store in the "Key" the name of the Property
    Properties_Names.Add 1, "Prop1"
    Properties_Names.Add 2, "Prop2"
    Properties_Names.Add 3, "Prop3"

End Sub

Public Property Let Property_By_Name(ByVal Name As String, ByVal Val As Variant)
    ' When i set a property by his name, i take his index _
     and set his variable.
    Dim Property_Index As Integer
    Property_Index = Properties_Names(Name)
    Real_Properties_Variables(Property_Index) = Val

End Property
Public Property Get Property_By_Name(ByVal Name As String) As Variant
    ' When i read a property by his name, i take his index _
     and read his variable.
    Dim Property_Index As Integer
    Property_Index = Properties_Names(Name)
    Property_By_Name = Real_Properties_Variables(Property_Index)

End Property

Public Property Get Prop1() As String
    Prop1 = Real_Properties_Variables(1)
End Property

Public Property Let Prop1(ByVal Val As String)
    Real_Properties_Variables(1) = Val
End Property

Public Property Get Prop2() As Integer
    Prop2 = Real_Properties_Variables(2)
End Property

Public Property Let Prop2(ByVal Val As Integer)
    Real_Properties_Variables(2) = Val
End Property

Public Property Get Prop3() As Double
    Prop3 = Real_Properties_Variables(3)
End Property

Public Property Let Prop3(ByVal Val As Double)
    Real_Properties_Variables(3) = Val
End Property

----------------------End of code----------------------
Now, in the form:

    Dim PBN As CPropertyByName
    Set PBN = New CPropertyByName

To write the prop value, we can use
    PBN.Prop1 = PropValue
    PBN.Property_By_Name("Prop1")= PropValue

And, to read the prop:

Hope this help.

Author Comment

ID: 1519061
This is right!
Is not exactly what i need, but put the annoying part of code (write all properties while creating the collection, and manage all properties by function, instead of use Public variables) into the class, leaving the "form" code simple.

Expert Comment

ID: 1519062
Reviewing question.

Customer Service @ Experts Exchange

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.

Question has a verified solution.

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

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.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

670 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