Forums

raise statement: should this work?

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

raise statement: should this work?

Postby kobi7 » Wed Nov 21, 2012 5:25 am

Hi Charles, (and everyone) should this code work?
I haven't tried in C# yet.
Am I doing something wrong here?

Code: Select all
sig Update
interface IView
   event finishedUpdate as Update

class X implements IView
   event finishedUpdate as Update

class MyProgram
   def main
      x = X()
      raise x.finishedUpdate
Falun Dafa is Good.
Truth, Compassion, Forbearance is Good.
kobi7
 
Posts: 82
Location: Israel

Re: raise statement: should this work?

Postby Charles » Wed Nov 21, 2012 10:43 am

Actually, in .NET, you're only supposed to raise events within the same class. See:

http://stackoverflow.com/questions/7903 ... in-c-sharp
http://stackoverflow.com/questions/4378 ... in-c-sharp

Besides the hacks shown in some answers above, you have two options. One is to switch from using an event to using a property whose type is the same as the delegate. The other is to provide a public method that raises the event like so:
sig UpdateHandler

interface IView

event finishedUpdate as UpdateHandler

class X implements IView

event finishedUpdate as UpdateHandler

def raiseFinishedUpdate
raise x.finishedUpdate

class MyProgram

def main
x = X()
x.raiseFinishedUpdate

But note that in most C# code I see, the method to raise an event is not public. Instead, events are raised from within the class to notify listeners when something interesting has happened for that class and object. Putting the logic for raising an event outside the class might be the event-based analog of "spaghetti code". So really the code should look more like:
sig UpdateHandler

interface IView

event finishedUpdate as UpdateHandler

class X implements IView

event finishedUpdate as UpdateHandler

def update
# do some update stuff
# and then:
raise x.finishedUpdate

class MyProgram

def main
x = X()
x.update

Also, note that I suffixed the sig name with "Handler". This is a common .NET style for declaring sigs/delegates that will be used for events.

I'll look into improving the Cobra compiler error checking and message.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: should this work?

Postby Charles » Wed Nov 21, 2012 11:11 am

FYI, in C#, if you try to raise an event for another class, the error message is:

The event 'A.Updated' can only appear on the left hand side of += or -= (except when used from within the type 'A')

... eh, not very friendly even if it's technically correct.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: should this work?

Postby Charles » Wed Nov 21, 2012 6:02 pm

I have now added a proper error check for this situation:
Code: Select all
Cannot raise events for other objects.

And made some minor updates at the Raise Statement wiki page.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: raise statement: should this work?

Postby kobi7 » Thu Nov 22, 2012 5:14 am

thanks
Falun Dafa is Good.
Truth, Compassion, Forbearance is Good.
kobi7
 
Posts: 82
Location: Israel


Return to Discussion

Who is online

Users browsing this forum: No registered users and 15 guests