late binding ... should I be using reflection ? 
Author Message
 late binding ... should I be using reflection ?

I have a method in a class ... call it class Foo ... in which I want to
change some aspect of a control on a form :

public class Foo
{
    public Foo()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public void someMethod(System.Windows.Forms.Form callingForm)
    {
        // toggle panel visibility
        callingForm.panel1.Visible = callingForm.panel1.Visible;
    }

Quote:
}

So I pass in a reference to the form containing the controls in the code in
the form that creates the instance of the class :

myFoo = new Foo(this);

But the compiler throws an error when the reference to callingForm.panel1 is
encountered.

Should I be using reflection here to somehow get the reference to the
control out of the callingForm parameter in the method call ?

thanks, Bill



Sat, 20 Nov 2004 01:16:02 GMT  
 late binding ... should I be using reflection ?
Bill, you are passing your reference to your form into the constructor for
the Foo class but not using it or saving it anywhere in the Foo class
instance.  I would change your class Foo to look like:

public class Foo
{
public Foo()
{

Quote:
}

public void someMethod(System.Windows.Forms.Control formToChange)
{
// toggle panel visibility
formToChange.Visible = true;
Quote:
}
}

and call it like so:

Foo myFoo = new Foo();
myFoo.someMethod(panel1);

--
Greg
http://www.claritycon.com/


Quote:
> I have a method in a class ... call it class Foo ... in which I want to
> change some aspect of a control on a form :

> public class Foo
> {
>     public Foo()
>     {
>         //
>         // TODO: Add constructor logic here
>         //
>     }

>     public void someMethod(System.Windows.Forms.Form callingForm)
>     {
>         // toggle panel visibility
>         callingForm.panel1.Visible = callingForm.panel1.Visible;
>     }
> }

> So I pass in a reference to the form containing the controls in the code
in
> the form that creates the instance of the class :

> myFoo = new Foo(this);

> But the compiler throws an error when the reference to callingForm.panel1
is
> encountered.

> Should I be using reflection here to somehow get the reference to the
> control out of the callingForm parameter in the method call ?

> thanks, Bill



Sat, 20 Nov 2004 01:31:36 GMT  
 late binding ... should I be using reflection ?
Greg,

Thanks for replying. I could have done a better job in making clear how I
was calling the method. The method in the class 'Foo is, indeed called from
the Form in a button click event.

What puzzles me is that passing the Form reference as an argument to the
method results in a compile error while passing the Panel object does not.

The compile error is occurring when the compiler builds the class 'Foo and
encounters the reference to the callingForm.panel1 : obviously at that point
a compile error makes perfect sense : this is a binding error that should be
thrown.

What I am seeking is an understanding of what to do in cases where I want to
use late binding .... where I want the ability to in class code specify a
parameter as receiving a form object and then want to do whatever in the
code with that passed in form argument.

The trivial example I used is perhaps not illustrating that as I really
intend to.

I am assuming that reflection would enable me to do late bound inspection of
an object.

thanks, Bill


Quote:
> Bill, you are passing your reference to your form into the constructor for
> the Foo class but not using it or saving it anywhere in the Foo class
> instance.  I would change your class Foo to look like:

> public class Foo
> {
> public Foo()
> {
> }
> public void someMethod(System.Windows.Forms.Control formToChange)
> {
> // toggle panel visibility
> formToChange.Visible = true;
> }
> }
> and call it like so:

> Foo myFoo = new Foo();
> myFoo.someMethod(panel1);

> --
> Greg
> http://www.claritycon.com/



> > I have a method in a class ... call it class Foo ... in which I want to
> > change some aspect of a control on a form :

> > public class Foo
> > {
> >     public Foo()
> >     {
> >         //
> >         // TODO: Add constructor logic here
> >         //
> >     }

> >     public void someMethod(System.Windows.Forms.Form callingForm)
> >     {
> >         // toggle panel visibility
> >         callingForm.panel1.Visible = callingForm.panel1.Visible;
> >     }
> > }

> > So I pass in a reference to the form containing the controls in the code
> in
> > the form that creates the instance of the class :

> > myFoo = new Foo(this);

> > But the compiler throws an error when the reference to
callingForm.panel1
> is
> > encountered.

> > Should I be using reflection here to somehow get the reference to the
> > control out of the callingForm parameter in the method call ?

> > thanks, Bill



Sat, 20 Nov 2004 02:03:24 GMT  
 late binding ... should I be using reflection ?
Bill, I understand what you mean now.  You don't have to use reflection.
The problem is that the argument you are passing is a generic Form type.
You need to pass in a specific type, in my example it is Form1 (in your case
it will be whatever the name is of your Form class).  After you change that
you will have to change the panel1 declaration from private to internal
(could make it public if you really wanted to but internal is safer).  Once
you've done those two things you are ready to go.  Here's updated sample
code.

internal System.Windows.Forms.Panel panel1;

...

private void button1_Click(object sender, System.EventArgs e)

{

Foo myFoo = new Foo();

myFoo.someMethod(this);

Quote:
}

...

public void someMethod(Form1 formToChange)

{

// toggle panel visibility

formToChange.panel1.Visible = true;

Quote:
}

--
Greg
http://www.claritycon.com/


Quote:
> Greg,

> Thanks for replying. I could have done a better job in making clear how I
> was calling the method. The method in the class 'Foo is, indeed called
from
> the Form in a button click event.

> What puzzles me is that passing the Form reference as an argument to the
> method results in a compile error while passing the Panel object does not.

> The compile error is occurring when the compiler builds the class 'Foo and
> encounters the reference to the callingForm.panel1 : obviously at that
point
> a compile error makes perfect sense : this is a binding error that should
be
> thrown.

> What I am seeking is an understanding of what to do in cases where I want
to
> use late binding .... where I want the ability to in class code specify a
> parameter as receiving a form object and then want to do whatever in the
> code with that passed in form argument.

> The trivial example I used is perhaps not illustrating that as I really
> intend to.

> I am assuming that reflection would enable me to do late bound inspection
of
> an object.

> thanks, Bill



> > Bill, you are passing your reference to your form into the constructor
for
> > the Foo class but not using it or saving it anywhere in the Foo class
> > instance.  I would change your class Foo to look like:

> > public class Foo
> > {
> > public Foo()
> > {
> > }
> > public void someMethod(System.Windows.Forms.Control formToChange)
> > {
> > // toggle panel visibility
> > formToChange.Visible = true;
> > }
> > }
> > and call it like so:

> > Foo myFoo = new Foo();
> > myFoo.someMethod(panel1);

> > --
> > Greg
> > http://www.claritycon.com/



> > > I have a method in a class ... call it class Foo ... in which I want
to
> > > change some aspect of a control on a form :

> > > public class Foo
> > > {
> > >     public Foo()
> > >     {
> > >         //
> > >         // TODO: Add constructor logic here
> > >         //
> > >     }

> > >     public void someMethod(System.Windows.Forms.Form callingForm)
> > >     {
> > >         // toggle panel visibility
> > >         callingForm.panel1.Visible = callingForm.panel1.Visible;
> > >     }
> > > }

> > > So I pass in a reference to the form containing the controls in the
code
> > in
> > > the form that creates the instance of the class :

> > > myFoo = new Foo(this);

> > > But the compiler throws an error when the reference to
> callingForm.panel1
> > is
> > > encountered.

> > > Should I be using reflection here to somehow get the reference to the
> > > control out of the callingForm parameter in the method call ?

> > > thanks, Bill



Sat, 20 Nov 2004 03:24:04 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Reflection - Late-Bound Objects

2. object type reflection late binding .ctor

3. Early binding vs late binding

4. Call COM+ from VC using Late Binding

5. How to create a Com Dll to use it using Late Binding in Visual Basic

6. early binding and late binding in ATL

7. Late Binding and Early Binding

8. call vb component using late binding

9. How to Binding the Later binding Controls's event

10. Late-bound results for late-bound clients without penalizing VTBL clients.

11. Reflection and Event Binding

12. How to Bind Late?

 

 
Powered by phpBB® Forum Software