Link to home
Start Free TrialLog in
Avatar of parduz
parduz

asked on

Variable --> PropertyName

I have a file like this:

"PropertyName1",PropValue
"PropertyName2",PropValue
etc...

I also have a Class:
Class.PropertyName1
Class.PropertyName2
etc...

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.

HOW CAN I ACCESS A PROPERTY WHOSE NAME IS STORED IN A VARIABLE?
Avatar of AllenC_Jr
AllenC_Jr

You must have Visual Basic 6.0

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

Example
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.
Avatar of parduz

ASKER

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.
parduz


VB5
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.
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?
Avatar of parduz

ASKER

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.

M

Avatar of parduz

ASKER

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
parduz

Sorry, I am not good in explaining things.
Question:
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
    Loop
    Close #1
End Sub

Avatar of parduz

ASKER

Ameba,
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.

Thanks
parduz
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.
Avatar of parduz

ASKER

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,,,
Avatar of parduz

ASKER

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
    MyClass.Properties(VariableWithTheName)=Value
and
    MyVar=MyClass.Properties(VariableWithTheName)
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.
ASKER CERTIFIED SOLUTION
Avatar of fab1970
fab1970

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of parduz

ASKER

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.
Thanks!
Reviewing question.

darinw
Customer Service @ Experts Exchange