Solved

Moving from Event Handlers in vb.net to c#.net

Posted on 2009-06-30
4
249 Views
Last Modified: 2013-12-17
Okay, I've finally given up on trying to do this. I only recently learnt to do it in vb.net and found it really useful. Translating that to C# is proving to be an absolute nightmare.

What I had in vb.net was this. A class that inherited and extended the data table class called xDataTable.

One of the main features of this extension is that the base data table will populate itself from a SQL server database if a populated SQLCommand is given to it.

I then added an event to the xDataTable which fired whenever the base data table was populated. Trying to do this in C# is proofing to be my worst nightmare. Can anyone help please.

Summary:

Class xDataTable will contain an event called hasData

Form fEditor should then add a handler to use this event.

Please could someone help and stop me from going back to vb

Thanks.
0
Comment
Question by:paul_whiting
  • 2
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
DarrenD earned 500 total points
ID: 24743577
Hi,

If you have a class xDataTable you should create a delegate like so

// Create Event

public delegate void HandleData();

public class xDataTable {

  public event HandleData hasData

  public void Fire() {
    hasData();
  }
}

//Register Event in Form
class registerEvent {

  xDataTable xdata = new xDataTable();

  ImplementationEvent myImpl = new ImplementationEvent();

  xdata.hasData += new HandleData(myImpl.DoSomething);
}

//Register Event in Form
class ImplementationEvent {

  public void DoSomething() {

   console.WriteLine("EventFired");

  }
}

Hope this helps,

Darren
0
 

Author Comment

by:paul_whiting
ID: 24752784
Thanks very much for your reply. It took a while but I got it to work with my scenario. C# is giving me nightmares, but I shall persevere.

Would there be much difference if I passed values with the events?

Thanks

Paul
0
 
LVL 18

Expert Comment

by:DarrenD
ID: 24752867
Hi Paul,

Your delegate will have to change to something like

public delegate void HandleData(string value);

You will have to call the event using a parameter

hasData("Hello");

and your implementation will have to change to

public void DoSomething(string value) {

   console.WriteLine(value);

  }


Think that's it.

Obviously haven't tested it.

Cheers,

Darren
0
 

Author Comment

by:paul_whiting
ID: 24754427
Thanks,

haven't had a chance to test it, but given what I now know via your previous solution, looks like it will  work. Thank-you so much. You have saved my sanity.

Paul
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Video Player 11 23
Problem to refer to value 8 50
Expression Evaluater 3 27
Problem to Alipay 10 24
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question