Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am a newbie to C# and I am sure this is a very basic question for you guys.

 

In vb, we had a "handles" for the code block right?

 

What is the version for this in C#?

 

Thing is, in VB I had the habit of adding another control's event to a common control. And I can't seem to be able to do it in C#.

 

Thanks in advance for helping out a newbie!

Posted

e.g.,

Public Sub Test(ByVal sender As Object, ByVal e As EventArgs) Handles SomeObject.SomeEvent

End Sub

 

would be:

public void InitializeComponent()

{

//INSTANT C# NOTE: Converted event handlers:

SomeObject.SomeEvent += new System.EventHandler(Test);

 

}

public void Test(object sender, EventArgs e)

{

}

Posted

Thanks so much.

 

you wouldn't happen to know how to convert this:

 

Public Sub Test(ByVal sender As Object, ByVal e As EventArgs) Handles SomeObject.SomeEvent, SecondObject.SomeEvent, ThirdObject.SomeEvent

End Sub

Posted

The actual output from our Instant C# VB to C# converter is:

 

//TODO: INSTANT C# TODO TASK: Insert the following converted event handlers at the end of the 'InitializeComponent' method for forms, 'Page_Init' for web pages, or into a constructor for other classes:

SomeObject.SomeEvent += new System.EventHandler(Test);

SecondObject.SomeEvent += new System.EventHandler(Test);

ThirdObject.SomeEvent += new System.EventHandler(Test);

 

public void Test(object sender, EventArgs e)

{

}

 

As stated, the event wireups should be added somewhere like InitializeComponent or some other method that executes on start up of the form.

Posted

Code in InitializeComponent

 

As stated, the event wireups should be added somewhere like InitializeComponent or some other method that executes on start up of the form.

 

For forms, I would suggest placing any custom event linking code in the constructor, after the call to InitializeComponent, since the comments generated by the forms designer clearly indicate that the InitializeComponent method should not be manually edited using the code editor.

 

Also, C# 2.0 supports a slightly more concise method of specifying a delegate:

 

ThirdObject.SomeEvent += new System.EventHandler(Test);
//Can be rewritten as:
ThirdObject.SomeEvent += Test;

 

Good luck :)

Never trouble another for what you can do for yourself.
  • Leaders
Posted

Re: Code in InitializeComponent

 

Eduardo, I'm glad the problem seems to be resolved, but I have a recommendation. Investigate the AddHandler keyword in VB and see how it relates to the Handles clause. C#'s use of += is the same as VB's use of AddHandler, which can produce the same effect as Handles. Being familiar with the relationship between these things will give you a thorough understanding of how events work in C#.

[sIGPIC]e[/sIGPIC]
Posted

Re: Code in InitializeComponent

 

For forms, I would suggest placing any custom event linking code in the constructor, after the call to InitializeComponent, since the comments generated by the forms designer clearly indicate that the InitializeComponent method should not be manually edited using the code editor.

 

Yes, but this is where event wireups normally belong. In addition, our testing showed that code inserted into the InitializeComponent method that normally 'belongs' in that method (such as event wireups) will not be trashed by the designer.

Posted

Re: Code in InitializeComponent

 

Eduardo, I'm glad the problem seems to be resolved, but I have a recommendation. Investigate the AddHandler keyword in VB and see how it relates to the Handles clause. C#'s use of += is the same as VB's use of AddHandler, which can produce the same effect as Handles. Being familiar with the relationship between these things will give you a thorough understanding of how events work in C#.

 

Thanks marble_eater for the advice. I do have plans of understanding C# as deep as possible but am afraid I was thrown into the C# cauldron quite unceremoniously. I was added to this project without formal core concept training in C#.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...