Home All Groups Group Topic Archive Search About

how to raise a non shared event from a shared method ?

Author
17 Nov 2006 9:32 AM
Pon
Hi everybody,

Simple question : Inside a class that has a shared method, how to raise an
event on a given instance of this class from the shared method ?

Author
17 Nov 2006 11:54 AM
rowe_newsgroups
Just pass the instance variable to the shared method. Here's a working
example:

Module Module1

    Sub Main()
        Dim c As New YourClass
        YourClass.ChangeName(c, "funny pants")
        Console.WriteLine(c.Name)
        Console.Read()
    End Sub

End Module

Public Class YourClass

    Private m_Name As String

    Public Shared Sub ChangeName(ByVal c As YourClass, ByVal newName As
String)
        c.Name = newName
    End Sub

    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set(ByVal value As String)
            m_Name = value
        End Set
    End Property

End Class

Thanks,

Seth Rowe


Pon wrote:
Show quoteHide quote
> Hi everybody,
>
> Simple question : Inside a class that has a shared method, how to raise an
> event on a given instance of this class from the shared method ?
Author
20 Nov 2006 10:01 PM
Pon
i'm probably missing something but... uh... where's the event ?

"rowe_newsgroups" <rowe_em***@yahoo.com> a écrit dans le message de news:
1163764452.340213.116***@m7g2000cwm.googlegroups.com...
Show quoteHide quote
> Just pass the instance variable to the shared method. Here's a working
> example:
>
> Module Module1
>
>    Sub Main()
>        Dim c As New YourClass
>        YourClass.ChangeName(c, "funny pants")
>        Console.WriteLine(c.Name)
>        Console.Read()
>    End Sub
>
> End Module
>
> Public Class YourClass
>
>    Private m_Name As String
>
>    Public Shared Sub ChangeName(ByVal c As YourClass, ByVal newName As
> String)
>        c.Name = newName
>    End Sub
>
>    Public Property Name() As String
>        Get
>            Return m_Name
>        End Get
>        Set(ByVal value As String)
>            m_Name = value
>        End Set
>    End Property
>
> End Class
>
> Thanks,
>
> Seth Rowe
>
>
> Pon wrote:
>> Hi everybody,
>>
>> Simple question : Inside a class that has a shared method, how to raise
>> an
>> event on a given instance of this class from the shared method ?
>
Author
17 Nov 2006 2:05 PM
Herfried K. Wagner [MVP]
"Pon" <pongla.pub***@laposte.net> schrieb:
> Simple question : Inside a class that has a shared method, how to raise an
> event on a given instance of this class from the shared method ?

In order to do that you need a reference to an instance of the class.  If
you implement the event using the .NET event pattern you can call the
corresponding protected 'On<event name>' method to raise the event.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
20 Nov 2006 9:59 PM
Pon
Perfect and obvious. Thanx a lot.

"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> a écrit dans le
message de news: uXcN4FlCHHA.***@TK2MSFTNGP06.phx.gbl...
Show quoteHide quote
> "Pon" <pongla.pub***@laposte.net> schrieb:
>> Simple question : Inside a class that has a shared method, how to raise
>> an event on a given instance of this class from the shared method ?
>
> In order to do that you need a reference to an instance of the class.  If
> you implement the event using the .NET event pattern you can call the
> corresponding protected 'On<event name>' method to raise the event.
>
> --
> M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
> V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>