πŸš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.csharpbuilder.language.csharp
2 messages
2 total messages Started by Francois PIETTE [ICS - MidWare] Sun, 06 Aug 2006 13:40
Passing by reference a bool argument to an event
#247
Author: Francois PIETTE [ICS - MidWare]
Date: Sun, 06 Aug 2006 13:40
28 lines
2384 bytes
I need to pass by reference a boolean value to an event handler. Since 
passing arguments to event handler involve using an intermediate class 
deriving from EventArgs, the reference argument loose his reference aspect 
because bool is a value-type.

I need to pass a bool by reference to an event handler to implement default 
behaviour if no event handler is assigned and allow the event handler to 
select the action to be performed by setting the boolean value to true or 
false.

Actually it is a used in a communication application when a timeout is 
detected an event is triggered, passing a boolean "CanClose" initialized to 
"true". If no handler is assigned to the event, CanClose is true. If an 
event handler is assigned, it can select to change CanClose to false. Then 
is the component code, CanClose is used to close the socket or not to close 
it.

I designed a solution I don't like: I create a class with a boolean member 
and I used this class as argument type for my CanClose argument. Since class 
are always passed by reference, my event handler can change de boolean to 
whatever it likes, it is changed in the original class instance. So it does 
what I need but it is rather heavy code. Should be a better way to do that.

-- 
francois.piette@overbyte.be
The author for the freeware multi-tier middleware MidWare
The author of the freeware Internet Component Suite (ICS)
http://www.overbyte.be
Re: Passing by reference a bool argument to an event
#248
Author: Joanna Carter [TeamB]
Date: Sun, 06 Aug 2006 17:06
60 lines
2380 bytes
"Francois PIETTE [ICS - MidWare]" <francois.piette@overbyte.be> a οΏ½crit dans 
le message de news: 44d5f1b5@newsgroups.borland.com...

|I need to pass by reference a boolean value to an event handler. Since
| passing arguments to event handler involve using an intermediate class
| deriving from EventArgs, the reference argument loose his reference aspect
| because bool is a value-type.

Here is a simple example that uses the CancelEventArgs doing something 
similar.

  public class Test
  {
    private CancelEventHandler validating;

    public void OnValidating()
    {
      if (validating != null)
      {
        bool valid = false;
        CancelEventArgs args = new CancelEventArgs(valid);
        validating(this, args);

        valid = args.Cancel;

        Console.WriteLine(valid.ToString());
      }
    }

    public event CancelEventHandler Validating
    {
      add { validating += value; }
      remove { validating -= value; }
    }
  }

  class Program
  {
    private static void HandleValidating(object sender, CancelEventArgs 
args)
    {
      args.Cancel = true;
    }

    [STAThread]
    static void Main(string[] args)
    {
      Test t = new Test();
      t.Validating += HandleValidating;
      t.OnValidating();

      Console.ReadKey();
    }
  }

Joanna

-- 
Joanna Carter [TeamB]
Consultant Software Engineer 
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads