One last delegate question

  • Thread starter Thread starter CJ Taylor
  • Start date Start date
C

CJ Taylor

What does this mean?

This method creates delegates for static methods only. An instance method is
a method that is associated with an instance of a class; a static method is
a method that is associated with the class itself.



So I'm trynig to use System.Delegate.CreateDelegate (Type, MethodInfo) :
Delegate and having some issues...



I have the following code

evtInfo = objHolder.GetType().GetEvent(eRow.EvtName)

methInfo = objTarget.GetType().GetMethod(eRow.TargetDelegate)

delTemp = System.Delegate.CreateDelegate(evtInfo.EventHandlerType, methInfo)

evtInfo.AddEventHandler(objHolder, delTemp)



and have no idea what I'm doing wrong. I keep getting that there is an
ArgumentException where methinfo needs to be a static method... And there is
not a bit of documentation on this.

Now, if I understand this correctly, and understand the word "static" I
understand that to be Shared in VB.NET, would that be the correct
assumption? Or am I just completly wrong in this matter.

I've been reading a lot on this, and I want to be able to release a
framework for Windows Forms for others to use, but can't get past these
little issues. That and if you ever have a complex quesiotn I can answer
it. =)

Hopefully someone is more familar with this than I am.



Thanks again,

CJ
 
Hey CJ,

: Now, if I understand this correctly, and understand the word "static" I
: understand that to be Shared in VB.NET, would that be the correct
: assumption? Or am I just completly wrong in this matter.

Yes you are correct. Basically, you can't create the delegate using that
overload because the method is not declared as shared.

Try changing this...
: delTemp = System.Delegate.CreateDelegate(evtInfo.EventHandlerType,
methInfo)

to this:
delTemp = System.Delegate.CreateDelegate(evtInfo.EventHandlerType,
objTarget, eRow.TargetDelegate)

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Maybe it's a game called 'Punish the User'"


: What does this mean?
:
: This method creates delegates for static methods only. An instance method
is
: a method that is associated with an instance of a class; a static method
is
: a method that is associated with the class itself.
:
:
:
: So I'm trynig to use System.Delegate.CreateDelegate (Type, MethodInfo) :
: Delegate and having some issues...
:
:
:
: I have the following code
:
: evtInfo = objHolder.GetType().GetEvent(eRow.EvtName)
:
: methInfo = objTarget.GetType().GetMethod(eRow.TargetDelegate)
:
: delTemp = System.Delegate.CreateDelegate(evtInfo.EventHandlerType,
methInfo)
:
: evtInfo.AddEventHandler(objHolder, delTemp)
:
:
:
: and have no idea what I'm doing wrong. I keep getting that there is an
: ArgumentException where methinfo needs to be a static method... And there
is
: not a bit of documentation on this.
:
: Now, if I understand this correctly, and understand the word "static" I
: understand that to be Shared in VB.NET, would that be the correct
: assumption? Or am I just completly wrong in this matter.
:
: I've been reading a lot on this, and I want to be able to release a
: framework for Windows Forms for others to use, but can't get past these
: little issues. That and if you ever have a complex quesiotn I can answer
: it. =)
:
: Hopefully someone is more familar with this than I am.
:
:
:
: Thanks again,
:
: CJ
:
:
 
Alright, so yeah, it is referring a Static Method as a shared method.

But why does this seem wrong? I'm trying to link an event in one DLL to the
Method in another DLL (excuse me, assemblies). But only this will happen if
both DLL's have an instance created. Which is why I don't understand why I
have to use a static method, but maybe this is just my own stupidity.

What if someone is developing a module for this framework, and doesn't have
a static method though? Or needs to use instance variables within a class.
I don't see how a shared method will be able to access instance methods
(cause they can't unless an instance is created and that just gets tricky)

Do I have to set something in a binding flag? Like CreateInstance in order
to tell it to not go for a static method? Would that be correct in
assuming?

I understand if no one knows the answer to this, but I thought I would
through it out.

I'll keep you all posted as I figure it out.



-CJ
 
Tom Spink said:
Hey CJ,

: Now, if I understand this correctly, and understand the word "static" I
: understand that to be Shared in VB.NET, would that be the correct
: assumption? Or am I just completly wrong in this matter.

Yes you are correct. Basically, you can't create the delegate using that
overload because the method is not declared as shared.

Try changing this...
: delTemp = System.Delegate.CreateDelegate(evtInfo.EventHandlerType,
methInfo)

to this:
delTemp = System.Delegate.CreateDelegate(evtInfo.EventHandlerType,
objTarget, eRow.TargetDelegate)

Yeah this did it, I could declare the object as non static and it was fine.
Once again, some documentation other than "Target: The Target Event." would
be appreciated for those of us NOT on the .NET development team. =)


Thanks again Tom
 
You need to create an instance of the object to use an instance method,
because the instance method could rely on instance data...

Public Class Class1
Private m_Var1

Public Sub NotStatic()
MsgBox(m_Var1)
End Sub
End Class

That non-static sub relies of m_Var1, so you need an instance of Class1 to
use it.

Declaring the method as Shared will not allow you to use instance
variables/methods/properties/etc, but an instance of the class will not need
to be created in order for you to use the method.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Maybe it's a game called 'Punish the User'"


: Alright, so yeah, it is referring a Static Method as a shared method.
:
: But why does this seem wrong? I'm trying to link an event in one DLL to
the
: Method in another DLL (excuse me, assemblies). But only this will happen
if
: both DLL's have an instance created. Which is why I don't understand why
I
: have to use a static method, but maybe this is just my own stupidity.
:
: What if someone is developing a module for this framework, and doesn't
have
: a static method though? Or needs to use instance variables within a
class.
: I don't see how a shared method will be able to access instance methods
: (cause they can't unless an instance is created and that just gets tricky)
:
: Do I have to set something in a binding flag? Like CreateInstance in
order
: to tell it to not go for a static method? Would that be correct in
: assuming?
:
: I understand if no one knows the answer to this, but I thought I would
: through it out.
:
: I'll keep you all posted as I figure it out.
:
:
:
: -CJ
: : > What does this mean?
: >
: > This method creates delegates for static methods only. An instance
method
: is
: > a method that is associated with an instance of a class; a static method
: is
: > a method that is associated with the class itself.
: >
: >
: >
: > So I'm trynig to use System.Delegate.CreateDelegate (Type, MethodInfo) :
: > Delegate and having some issues...
: >
: >
: >
: > I have the following code
: >
: > evtInfo = objHolder.GetType().GetEvent(eRow.EvtName)
: >
: > methInfo = objTarget.GetType().GetMethod(eRow.TargetDelegate)
: >
: > delTemp = System.Delegate.CreateDelegate(evtInfo.EventHandlerType,
: methInfo)
: >
: > evtInfo.AddEventHandler(objHolder, delTemp)
: >
: >
: >
: > and have no idea what I'm doing wrong. I keep getting that there is an
: > ArgumentException where methinfo needs to be a static method... And
there
: is
: > not a bit of documentation on this.
: >
: > Now, if I understand this correctly, and understand the word "static" I
: > understand that to be Shared in VB.NET, would that be the correct
: > assumption? Or am I just completly wrong in this matter.
: >
: > I've been reading a lot on this, and I want to be able to release a
: > framework for Windows Forms for others to use, but can't get past these
: > little issues. That and if you ever have a complex quesiotn I can
answer
: > it. =)
: >
: > Hopefully someone is more familar with this than I am.
: >
: >
: >
: > Thanks again,
: >
: > CJ
: >
: >
:
:
 
Back
Top