• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

Variable --> PropertyName

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.

  • 7
  • 4
  • 2
  • +5
1 Solution
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.
parduzAuthor Commented:
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.

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.
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.
>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?
parduzAuthor Commented:
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.
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.
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.


parduzAuthor Commented:
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

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

parduzAuthor Commented:
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.

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.
parduzAuthor Commented:
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,,,
parduzAuthor Commented:
This is a great advance to the solution of this problem,
from Mark Dominic Hurst (visit his site to http://www.keysound.com, 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?

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.
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.
parduzAuthor Commented:
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.
Reviewing question.

Customer Service @ Experts Exchange
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 7
  • 4
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now