Click here to Skip to main content
15,888,610 members
Home / Discussions / C#
   

C#

 
AnswerRe: Ghost click mouse Pin
jsc4219-Oct-20 23:03
professionaljsc4219-Oct-20 23:03 
GeneralRe: Ghost click mouse Pin
trønderen20-Oct-20 5:54
trønderen20-Oct-20 5:54 
Questionhi im a bit stuck i followed this tut on youtube and its just not working can anyone help me plz i get error Pin
Marcus Riggs14-Oct-20 12:32
Marcus Riggs14-Oct-20 12:32 
AnswerRe: hi im a bit stuck i followed this tut on youtube and its just not working can anyone help me plz i get error Pin
OriginalGriff14-Oct-20 20:23
mveOriginalGriff14-Oct-20 20:23 
QuestionRe: hi im a bit stuck i followed this tut on youtube and its just not working can anyone help me plz i get error Pin
ZurdoDev15-Oct-20 1:18
professionalZurdoDev15-Oct-20 1:18 
Questionwhat is better way to handle audit data in sequel server? Pin
ronak752113-Oct-20 2:54
ronak752113-Oct-20 2:54 
AnswerRe: what is better way to handle audit data in sequel server? Pin
OriginalGriff13-Oct-20 2:57
mveOriginalGriff13-Oct-20 2:57 
AnswerRe: what is better way to handle audit data in sequel server? Pin
ZurdoDev13-Oct-20 3:12
professionalZurdoDev13-Oct-20 3:12 
AnswerRe: what is better way to handle audit data in sequel server? Pin
Richard MacCutchan13-Oct-20 5:24
mveRichard MacCutchan13-Oct-20 5:24 
GeneralRe: what is better way to handle audit data in sequel server? Pin
ZurdoDev14-Oct-20 5:21
professionalZurdoDev14-Oct-20 5:21 
GeneralRe: what is better way to handle audit data in sequel server? Pin
Richard MacCutchan14-Oct-20 5:39
mveRichard MacCutchan14-Oct-20 5:39 
QuestionWhat kind of dll use ftp/sftp mapping drive? Pin
MinSeokKo12-Oct-20 22:48
MinSeokKo12-Oct-20 22:48 
AnswerRe: What kind of dll use ftp/sftp mapping drive? Pin
OriginalGriff13-Oct-20 1:54
mveOriginalGriff13-Oct-20 1:54 
GeneralRe: What kind of dll use ftp/sftp mapping drive? Pin
MinSeokKo13-Oct-20 14:11
MinSeokKo13-Oct-20 14:11 
GeneralRe: What kind of dll use ftp/sftp mapping drive? Pin
Randor 13-Oct-20 15:49
professional Randor 13-Oct-20 15:49 
GeneralRe: What kind of dll use ftp/sftp mapping drive? Pin
Dave Kreskowiak13-Oct-20 18:58
mveDave Kreskowiak13-Oct-20 18:58 
GeneralRe: What kind of dll use ftp/sftp mapping drive? Pin
Randor 13-Oct-20 19:39
professional Randor 13-Oct-20 19:39 
GeneralRe: What kind of dll use ftp/sftp mapping drive? Pin
Dave Kreskowiak14-Oct-20 5:01
mveDave Kreskowiak14-Oct-20 5:01 
JokeRe: What kind of dll use ftp/sftp mapping drive? Pin
Randor 14-Oct-20 9:22
professional Randor 14-Oct-20 9:22 
Questionhow to insert unknown amount of data into a database Pin
steven_noppe11-Oct-20 7:41
steven_noppe11-Oct-20 7:41 
AnswerRe: how to insert unknown amount of data into a database Pin
Luc Pattyn11-Oct-20 8:32
sitebuilderLuc Pattyn11-Oct-20 8:32 
AnswerRe: how to insert unknown amount of data into a database Pin
OriginalGriff11-Oct-20 8:33
mveOriginalGriff11-Oct-20 8:33 
AnswerRe: how to insert unknown amount of data into a database Pin
jsc4213-Oct-20 3:50
professionaljsc4213-Oct-20 3:50 
QuestionWhy am I not allowed to do this with events Pin
Keith Barrow9-Oct-20 1:20
professionalKeith Barrow9-Oct-20 1:20 
AnswerRe: Why am I not allowed to do this with events Pin
Richard Deeming9-Oct-20 2:12
mveRichard Deeming9-Oct-20 2:12 
The technical reason is fairly simple. When you declare an event:
C#
public class Foo
{
    public event EventHandler Test;
}
the compiler actually generates two members:
C#
public class Foo
{
    [CompilerGenerated]
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private EventHandler Test;
    
    public event EventHandler Test
    {
        [CompilerGenerated]
        add
        {
            EventHandler eventHandler = this.Test;
            EventHandler eventHandler2;
            do
            {
                eventHandler2 = eventHandler;
                EventHandler value2 = (EventHandler)Delegate.Combine(eventHandler2, value);
                eventHandler = Interlocked.CompareExchange(ref this.Test, value2, eventHandler2);
            }
            while ((object)eventHandler != eventHandler2);
        }
        [CompilerGenerated]
        remove
        {
            EventHandler eventHandler = this.Test;
            EventHandler eventHandler2;
            do
            {
                eventHandler2 = eventHandler;
                EventHandler value2 = (EventHandler)Delegate.Remove(eventHandler2, value);
                eventHandler = Interlocked.CompareExchange(ref this.Test, value2, eventHandler2);
            }
            while ((object)eventHandler != eventHandler2);
        }
    }
}
Inside the same type, you will either reference the private field (for assignment, Invoke, etc.) or the event (for adding and removing handlers). The compiler will decide which.

Outside of the class, you only have access to the event. All you can do is add or remove a handler.

I believe this is to prevent other classes from raising events without the cooperation of the base class, or swapping out the entire list of handlers. But obviously you'd need to ask the original language designers to know their reasoning.

Of course, the simple workaround is to provide a protected method in the base class which raises the event on demand.
C#
protected void OnTest(EventArgs e)
{
    Test?.Invoke(this, e);
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.