Using none-shared method from shared.

M

Mr. X.

Is there any way to use shared method from non-shared one in the same class
?

Problem is :
I am running by code the defaultEventHandler, and the defaultEventHandler
should be declared as shared (compiler force me).

As the following code :
===============

Private Sub setDefaultEvents(byVal c as control)
Dim i As Integer
Dim c As Control
Dim d As DefaultEventAttribute
Dim ei As EventInfo
Dim mi As MethodInfo
Dim de As [Delegate]

For Each att In c.GetType.GetCustomAttributes(True)
If TypeOf att Is DefaultEventAttribute Then
d = att
ei = c.GetType.GetEvent(d.Name)
mi = Me.GetType.GetMethod("defaultEventHndlr")
de = [Delegate].CreateDelegate(ei.EventHandlerType, mi)
ei.AddEventHandler(c, de)
End If
Next
End Sub

Private Shared Sub defaultEventHndlr(ByVal sender As System.Object,
ByVal e As System.EventArgs)
... '**** I cannot call any other method of the current class, that is
not shareable.
End sub

The problem is marked in asterisks !

The class has it's own components, and I cannot write a new instance to use
any of the methods with no problems.

Is there any design-pattern, or any solution for the above?

Thanks :)
 
H

Herfried K. Wagner [MVP]

Am 10.07.2010 01:46, schrieb Mr. X.:
Is there any way to use shared method from non-shared one in the same
class ?

Problem is :
I am running by code the defaultEventHandler, and the
defaultEventHandler should be declared as shared (compiler force me).

As the following code :
===============

Private Sub setDefaultEvents(byVal c as control)
Dim i As Integer
Dim c As Control
Dim d As DefaultEventAttribute
Dim ei As EventInfo
Dim mi As MethodInfo
Dim de As [Delegate]

For Each att In c.GetType.GetCustomAttributes(True)
If TypeOf att Is DefaultEventAttribute Then
d = att
ei = c.GetType.GetEvent(d.Name)
mi = Me.GetType.GetMethod("defaultEventHndlr")
de = [Delegate].CreateDelegate(ei.EventHandlerType, mi)
ei.AddEventHandler(c, de)
End If
Next
End Sub

Private Shared Sub defaultEventHndlr(ByVal sender As System.Object,
ByVal e As System.EventArgs)
... '**** I cannot call any other method of the current class, that is
not shareable.
End sub

What's the intention of marking this method as shared? Also note that
you can set up handlers using the 'AddHandler' statement.
 
M

Mr. X.

MethodInfo : mi = Me.GetType.GetMethod("defaultEventHndlr") returns nothing,
if the method is not public and not shared.
mi = nothing, so I cannot go on.
Besides, if I get any control, with unknown class. That's why using
reflection.

For general solution, is there any way to call a non-shared method from
shared on?

Thanks :)

Herfried K. Wagner said:
Am 10.07.2010 01:46, schrieb Mr. X.:
Is there any way to use shared method from non-shared one in the same
class ?

Problem is :
I am running by code the defaultEventHandler, and the
defaultEventHandler should be declared as shared (compiler force me).

As the following code :
===============

Private Sub setDefaultEvents(byVal c as control)
Dim i As Integer
Dim c As Control
Dim d As DefaultEventAttribute
Dim ei As EventInfo
Dim mi As MethodInfo
Dim de As [Delegate]

For Each att In c.GetType.GetCustomAttributes(True)
If TypeOf att Is DefaultEventAttribute Then
d = att
ei = c.GetType.GetEvent(d.Name)
mi = Me.GetType.GetMethod("defaultEventHndlr")
de = [Delegate].CreateDelegate(ei.EventHandlerType, mi)
ei.AddEventHandler(c, de)
End If
Next
End Sub

Private Shared Sub defaultEventHndlr(ByVal sender As System.Object,
ByVal e As System.EventArgs)
... '**** I cannot call any other method of the current class, that is
not shareable.
End sub

What's the intention of marking this method as shared? Also note that you
can set up handlers using the 'AddHandler' statement.
 
M

Mr. X.

Solved.
I added a shared variable, that points to the current class :

private shared FInst as MyControlClass
.....
public sub new()
....
FInst = me
end sub

private sub someMethod()
end sub

and on the shared function (It should be public & shared. I cannot figure
out other solution) :

public Shared Sub defaultEventHndlr(ByVal sender As System.Object,
ByVal e As System.EventArgs)
....
FInst.someMethod()
end sub

Thanks :)

Herfried K. Wagner said:
Am 10.07.2010 01:46, schrieb Mr. X.:
Is there any way to use shared method from non-shared one in the same
class ?

Problem is :
I am running by code the defaultEventHandler, and the
defaultEventHandler should be declared as shared (compiler force me).

As the following code :
===============

Private Sub setDefaultEvents(byVal c as control)
Dim i As Integer
Dim c As Control
Dim d As DefaultEventAttribute
Dim ei As EventInfo
Dim mi As MethodInfo
Dim de As [Delegate]

For Each att In c.GetType.GetCustomAttributes(True)
If TypeOf att Is DefaultEventAttribute Then
d = att
ei = c.GetType.GetEvent(d.Name)
mi = Me.GetType.GetMethod("defaultEventHndlr")
de = [Delegate].CreateDelegate(ei.EventHandlerType, mi)
ei.AddEventHandler(c, de)
End If
Next
End Sub

Private Shared Sub defaultEventHndlr(ByVal sender As System.Object,
ByVal e As System.EventArgs)
... '**** I cannot call any other method of the current class, that is
not shareable.
End sub

What's the intention of marking this method as shared? Also note that you
can set up handlers using the 'AddHandler' statement.
 
A

Armin Zingler

Am 10.07.2010 10:55, schrieb Mr. X.:

Not solved. That's what is called "a hack". :)

First, the method Type.GetMethod can also retrieve Shared methods.
Pass Bindingflags.Static instead of Bindngflags.Instance as an additional parameter.

Second, you can not access an instance member from
a shared member. There can be 0 or oo (infinite) number of
instances. Which instance do you want to access (if there's any)?

Third, why does it have to be shared?

4th, what are you trying to do?
I added a shared variable, that points to the current class :

private shared FInst as MyControlClass
....
public sub new()
...
FInst = me
end sub

private sub someMethod()
end sub

and on the shared function (It should be public & shared. I cannot figure
out other solution) :

public Shared Sub defaultEventHndlr(ByVal sender As System.Object,
ByVal e As System.EventArgs)
...
FInst.someMethod()
end sub


Don't make yourself unhappy with this "solution" in the long run.
 
C

Cor

Armin,

When I saw this thread I thought, what is he trying to do, this does give
code which is not to maintain.

Why have you set that as 4th, could be on the first.

:)

Cor
 
M

Mr. X.

When changing to :
mi = Me.GetType.GetMethod("defaultEventHndlr", BindingFlags.Instance Or
BindingFlags.Public)
and defaultEventHandler to :
Public Sub defaultEventHndlr(ByVal sender As System.Object, ByVal e As
System.EventArgs)

I got an exception when
de = [Delegate].CreateDelegate(ei.EventHandlerType, mi)

Error binding to target method.
Changing back to :
mi = Me.GetType.GetMethod("defaultEventHndlr", BindingFlags.Static Or
BindingFlags.Public)
and defaultEventHandler to :
Public Sub defaultEventHndlr(ByVal sender As System.Object, ByVal e As
System.EventArgs)

mi = nothing.

I am trying to make a generic solution for some object, and handle their
default method to do some tasks.

Thanks :)
 
A

Armin Zingler

Am 10.07.2010 22:44, schrieb Mr. X.:
When changing to :
mi = Me.GetType.GetMethod("defaultEventHndlr", BindingFlags.Instance Or
BindingFlags.Public)
and defaultEventHandler to :
Public Sub defaultEventHndlr(ByVal sender As System.Object, ByVal e As
System.EventArgs)

I got an exception when
de = [Delegate].CreateDelegate(ei.EventHandlerType, mi)

Error binding to target method.
Changing back to :
mi = Me.GetType.GetMethod("defaultEventHndlr", BindingFlags.Static Or
BindingFlags.Public)
and defaultEventHandler to :
Public Sub defaultEventHndlr(ByVal sender As System.Object, ByVal e As
System.EventArgs)

mi = nothing.

I am trying to make a generic solution for some object, and handle their
default method to do some tasks.

This is confusing. Inside the event handler, which object do you want to access ?

- An instance of the class that contains the event handler?
- The object raising the event?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top