how do I create delegates for my own classes

Coding Questions, IDE related or anything else

how do I create delegates for my own classes

Postby udo.killermann » Wed Feb 16, 2011 7:56 am

Board,

is there a way to create my own delegates (or btw: events) for my own classes? Or is this only possible if I use ObjC?

Regards
Udo
--------------
MacBook (2GHz, 4 GByte) - Lion (10.7.3)
started Objective Basic coding in April 2010 - still enjoy it ;-)
udo.killermann
 
Posts: 247
Joined: Thu Apr 08, 2010 6:46 am
Location: Hannover, Germany

Re: how do I create delegates for my own classes

Postby berndnoetscher » Sun Feb 20, 2011 4:43 pm

udo.killermann wrote:Board,

is there a way to create my own delegates (or btw: events) for my own classes? Or is this only possible if I use ObjC?

Regards
Udo


Hello Udo
Delegates and events are just functions. The difference is that they are not directly called by your code.
If events or delegates of Cocoa are used, there is always a wrapper function hidden to the objb developer created in your code. It is needed for type transformations and so on.

The example DeclareEvent shows how to access Cocoa events directly.

Code: Select all
Declare Class "NSBezierPath"
Declare Sub "NSBezierPath" - (void)fill
Declare Sub "NSBezierPath" + (void)fillRect:(NSRect)aRect
Declare Function "NSBezierPath" + (NSBezierPath)bezierPathWithRect:(NSRect)aRect
Declare Sub "NSColor" - (void)set

Declare Event "Scroller" - (void)mouseDown:(NSEvent *)theEvent




Event mouseDown(theEvent As NSEvent)
  Alert("mouseDown", Info(theEvent))
End Event

Event DrawRect(Argument As Rect)
' do drawing here
  Dim r As Rect
  Dim bp As NSBezierPath
  Dim colour As NSColor = BlueColor
 
  r = Rect(10.0, 10.0, 50.0, 60.0)
  bp = NSBezierPath.bezierPathWithRect(r)
  ' colour = BlueColor
  colour.set()
  bp.fill()
End Event


ObjCDeclareDelegate is here as well:

Code: Select all

' declare delegates are valid for the entire project

Declare Delegate "NSTableView" - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
Declare Delegate "NSTableView" - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
Declare Delegate "NSTableView" - (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex


IBOutlet table As NSTableView
 
Dim a As Array("a", "b", "c")

Delegate tableView(aTableView As NSTableView, aTableColumn As NSTableColumn, rowIndex As Integer) As id
  Return a.Object(rowIndex)
End Delegate

Delegate numberOfRowsInTableView(aTableView As NSTableView) As Integer
  Return a.Count()
End Delegate

Delegate tableView(aTableView As NSTableView, anObject As id, aTableColumn As NSTableColumn, rowIndex As Integer)
  a.Replace(anObject, rowIndex)
End Delegate



Applying those declare statements to your own classes is possible as well.
berndnoetscher
Site Admin
 
Posts: 280
Joined: Fri Feb 20, 2009 11:58 am

Re: how do I create delegates for my own classes

Postby udo.killermann » Mon Feb 21, 2011 7:50 pm

Bernd,

Applying those declare statements to your own classes is possible as well.
- That's the thing I am looking for. In Objective C a protocol is used to define the interface between caller and callee. How would I do this in ObjB?

The other usage scenarios of Delegates work well in my own code.

Kind regards
Udo
--------------
MacBook (2GHz, 4 GByte) - Lion (10.7.3)
started Objective Basic coding in April 2010 - still enjoy it ;-)
udo.killermann
 
Posts: 247
Joined: Thu Apr 08, 2010 6:46 am
Location: Hannover, Germany

Re: how do I create delegates for my own classes

Postby berndnoetscher » Wed Feb 23, 2011 1:42 pm

udo.killermann wrote:Bernd,

Applying those declare statements to your own classes is possible as well.
- That's the thing I am looking for. In Objective C a protocol is used to define the interface between caller and callee. How would I do this in ObjB?

The other usage scenarios of Delegates work well in my own code.

Kind regards
Udo


Protocols are not supported yet. I am not sure, if I got your right. Can you give me an example of what you try to accomplish, please?
berndnoetscher
Site Admin
 
Posts: 280
Joined: Fri Feb 20, 2009 11:58 am

Re: how do I create delegates for my own classes

Postby udo.killermann » Thu Feb 24, 2011 8:00 am

Bernd,

in the good old days of Apple-Basic there was "def fn" as a way to define a math function function in one place that you could refer to within your code. I was thinking about building a class the iterates over an interval and evaluates a user supplied function. I thought a delegate would be a nice facility to to so - but I am stuck and don't know how to start this.

I was thinking to define a Delegate (function prototype) inside the class and calling this Delegate within the class. The concrete function should be supplied by the programmer using my class.

Code: Select all
Delegate calc(arg As Integer) As Integer

Function callMe(what As Integer) As Integer
  Dim ret As Integer
 
  ret = calc(what)
 
  Return(ret) 
End Function


As most of the time the problem seems to be layer 8 (user) related regarding to the ISO 7 layer model :lol:
Regards
Udo
--------------
MacBook (2GHz, 4 GByte) - Lion (10.7.3)
started Objective Basic coding in April 2010 - still enjoy it ;-)
udo.killermann
 
Posts: 247
Joined: Thu Apr 08, 2010 6:46 am
Location: Hannover, Germany

Re: how do I create delegates for my own classes

Postby berndnoetscher » Thu Mar 03, 2011 12:23 pm

Udo,

as far as I understand, your problem could be solved by a custom super class (if objb would support this feature) and overriding the default behaviour of any functions provided by the super class by the sub class.


udo.killermann wrote:Bernd,

in the good old days of Apple-Basic there was "def fn" as a way to define a math function function in one place that you could refer to within your code. I was thinking about building a class the iterates over an interval and evaluates a user supplied function. I thought a delegate would be a nice facility to to so - but I am stuck and don't know how to start this.

I was thinking to define a Delegate (function prototype) inside the class and calling this Delegate within the class. The concrete function should be supplied by the programmer using my class.

Code: Select all
Delegate calc(arg As Integer) As Integer

Function callMe(what As Integer) As Integer
  Dim ret As Integer
 
  ret = calc(what)
 
  Return(ret) 
End Function


As most of the time the problem seems to be layer 8 (user) related regarding to the ISO 7 layer model :lol:
Regards
Udo
berndnoetscher
Site Admin
 
Posts: 280
Joined: Fri Feb 20, 2009 11:58 am

Re: how do I create delegates for my own classes

Postby udo.killermann » Thu Mar 03, 2011 7:02 pm

Bernd,

subclassing would be one way to do it and I would like to see this feature in ObjB.

But delegates are more about function prototypes that are to be implemented in any another class. We use this mechanism with Cocoa all the time by setting the Delegate property and then implementing the method defined by the delegate. The means to do so are protocols in Objective C or interfaces in Java.

Kind regards
Udo
--------------
MacBook (2GHz, 4 GByte) - Lion (10.7.3)
started Objective Basic coding in April 2010 - still enjoy it ;-)
udo.killermann
 
Posts: 247
Joined: Thu Apr 08, 2010 6:46 am
Location: Hannover, Germany


Return to Objective-Basic (Xcode 3)

Who is online

Users browsing this forum: No registered users and 0 guests

cron