Jet
Jet

Reputation: 526

Accessing subclass or subproperty using CallByName

I can access class' subvalues using CallByName (In other words, I can get Class.SubValue with it).
But I get error when I want to get Class.SUBCLASS.SubValue using CallByName.
Is it possible to do it using CallByName (or by using smtg else)?

Here's a typical code:

Class Class1
    Public someVariable as Long=123 'now "Class1" has "someVariable" 
End Class
Class Class2
    Public subClass as new Class1   'now "Class2" has a SUBCLASS "Class1" 
End Class

Sub Test()
    Dim c1 as New Class1, c2 as New Class2
        'This works fine, and I can get c1.someVariable. It's OK.
    a=CallByName(c1,"someVariable",CallType.Get)
        'But I get error here... and I can't get c2.subClass.someVariable
    b=CallByName(c2,"subClass.someVariable",CallType.Get)
End Sub

-- EDIT: Here is the actual question. --

I want to LOAD parameters from a file to form's controls, and in file the parameters are written like this:
<ControlName>.<Property>=<Value> | TextBox1.Text=SomeText | Button2.Left=1234
And I get error when I use:
CallByName(MyForm, "<ControlName>.<Property>", CallType.Set, "<Value>")

How can I fix my problem (using CallByName or smtg else)?

Upvotes: 2

Views: 1431

Answers (2)

Simon Barnett
Simon Barnett

Reputation: 117

I was doing something similar. I wanted to be able to use dot notation in SQL column names to recursively populate an object hierarchy.

Sub PopulateData(r As SqlDataReader)
    For i As Integer = 0 To r.FieldCount - 1
        If Len(r(i)) > 0 Then
            Try
                Dim ob As Object = Me
                Dim name As String = r.GetName(i)

                While InStr(name, ".") > 0
                    ob = CallByName(ob, Split(name, ".")(0), CallType.Get)
                    name = name.Substring(InStr(name, "."))
                End While

                CallByName(ob, name, CallType.Set, r(i))


            Catch ex As Exception
                Console.WriteLine("Missing property: {0}", r.GetName(i))

            End Try

        End If

    Next

End Sub

Upvotes: 0

Roberto Pasquali
Roberto Pasquali

Reputation: 21

i used this simple recoursive function

  Function RecursiveGetValue(ByVal Name As String, ByVal Data As Object) As Object
If Name.Contains(".") = True Then
  RecursiveGetValue = RecursiveGetValue(Name.Substring(Name.IndexOf(".") + 1), CallByName(Data, Name.Split(".")(0), CallType.Get))
Else
  RecursiveGetValue = CallByName(Data, Name, CallType.Get)
End If

End Function

Upvotes: 2

Related Questions