Delegates sample

Feb 21
2016

Hi,

when working with X# and Vulcan.NET, I have created this particular sample. It should help understand how delegates work (and that they are immutable):

#using DelegateSample

begin namespace DelegateSample

public delegate SampleDelegate( cParameter as string ) as void

function Start( ) as void
  local oSample as SampleClass
  local oSample2 as Sample2Class
  local oDelegate as SampleDelegate
  local oDelegate2 as SampleDelegate

  System.Console.WriteLine(“Start SampleDelegate”)

  oSample := SampleClass{}
  oSample2 := Sample2Class{}

  oDelegate := SampleDelegate{ oSample:Method1 }
  oDelegate += oSample:Method2
  oDelegate += oSample2:Method3

  oDelegate( “Hi” )
  oDelegate2 := oDelegate

  oDelegate -= oSample:Method2
  oDelegate -= oSample2:Method3

  oDelegate( “Hott” )
  oDelegate2( “Hups” )

  return

class SampleClass

  constructor()

  return

  public method Method1( cParameter as string ) as void

    System.Console.WriteLine( “method1, parameter ” + cParameter )

    return

  public method Method2( cParameter as string ) as void

    System.Console.WriteLine( “method2, parameter ” + cParameter )

    return

end class

class Sample2Class

  public method Method3( cParameter as string ) as void

    System.Console.WriteLine( “method3, Sample2Class, parameter ” + cParameter )

    return

end class

end namespace

Comments are closed.