Reflection Question How to Call a Methode of a Member without Createing a new instance 
Author Message
 Reflection Question How to Call a Methode of a Member without Createing a new instance

I have following Situation

I have one Class which has 3 Objects, the 3 Objects defined by Class 2

        Public Class Class1
                Public ChildClass1 As New Class2()
                Public ChildClass2 As New Class2()
                Public ChildClass3 As New Class2()
        End Class

Here is Class 2:

        Public Class Class2
                Public Sub Test()
                    MsgBox("Hello")
                End Sub
        End Class

The I have Code (in a Form e.g.) where I Created a Object of Class1
and then call the Methode "Test" from the Class to via Class1.

        Public Class Form1
              Private Sub Button1_Click(..........
                    Dim vlob_MyObject As New Class1()
                    vlob_MyObject.ChildClass1.Test()
            End Sub
        End Class

Only at Runtime I know which of the Three Child Objects I want to Call.

                    vlob_MyObject.ChildClass1.Test()
or
                    vlob_MyObject.ChildClass2.Test()
or
                    vlob_MyObject.ChildClass3.Test()

I get the Name of the Object via a String "ChildClass1"

How can I do this ?

All Samples which I saw with Reflection make an new Instance of a Class
but in this Case I allread have Instances of the Child Objects and won't
Create new Objects
because I need the Information the Objects allread have.

Thanx for any Adwise
Micha



Tue, 26 Apr 2005 06:56:51 GMT  
 Reflection Question How to Call a Methode of a Member without Createing a new instance
Jerry,

Quote:
>I get the Name of the Object via a String "ChildClass1"

>How can I do this ?

Dim objname As String = "ChildClass1".
Dim child As Object =
GetType(Class1).GetField(objname).GetValue(vlob_MyObject)
DirectCast(child, Class2).Test()

Mattias

===

http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.



Tue, 26 Apr 2005 07:11:55 GMT  
 Reflection Question How to Call a Methode of a Member without Createing a new instance
Found the anwer myself.

With the CallByName Function I solved the Problem



Quote:
> I have following Situation

> I have one Class which has 3 Objects, the 3 Objects defined by Class 2

>         Public Class Class1
>                 Public ChildClass1 As New Class2()
>                 Public ChildClass2 As New Class2()
>                 Public ChildClass3 As New Class2()
>         End Class

> Here is Class 2:

>         Public Class Class2
>                 Public Sub Test()
>                     MsgBox("Hello")
>                 End Sub
>         End Class

> The I have Code (in a Form e.g.) where I Created a Object of Class1
> and then call the Methode "Test" from the Class to via Class1.

>         Public Class Form1
>               Private Sub Button1_Click(..........
>                     Dim vlob_MyObject As New Class1()
>                     vlob_MyObject.ChildClass1.Test()
>             End Sub
>         End Class

> Only at Runtime I know which of the Three Child Objects I want to Call.

>                     vlob_MyObject.ChildClass1.Test()
> or
>                     vlob_MyObject.ChildClass2.Test()
> or
>                     vlob_MyObject.ChildClass3.Test()

> I get the Name of the Object via a String "ChildClass1"

> How can I do this ?

> All Samples which I saw with Reflection make an new Instance of a Class
> but in this Case I allread have Instances of the Child Objects and won't
> Create new Objects
> because I need the Information the Objects allread have.

> Thanx for any Adwise
> Micha



Tue, 26 Apr 2005 07:17:20 GMT  
 Reflection Question How to Call a Methode of a Member without Createing a new instance
Hey Ho Mattias

That's what I was searching for :-)

Thanx you very much !!!!

Micha



Quote:
> Jerry,

> >I get the Name of the Object via a String "ChildClass1"

> >How can I do this ?

> Dim objname As String = "ChildClass1".
> Dim child As Object =
> GetType(Class1).GetField(objname).GetValue(vlob_MyObject)
> DirectCast(child, Class2).Test()

> Mattias

> ===

> http://www.msjogren.net/dotnet/
> Please reply only to the newsgroup.



Tue, 26 Apr 2005 07:19:14 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. How to access class members without New

2. PRINTING GRAPHICS (jpg and gif) WITHOUT PAINTPICTURE-METHODE

3. Createing a new user Under NT

4. Calling static functions without using New

5. Calling a shared Method with Reflection

6. Missing assembly when using reflection to call component method

7. Asyncronous method call and WRQ Reflection

8. Asyncronous method call and WRQ Reflection

9. Question about "Display" methode

10. Opening new document without a new window opening

11. I think this is a reflection question

12. 2 new 2 b called newbie - scrollbar question

 

 
Powered by phpBB® Forum Software