CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    Post typeof(this) for a custom class

    Hi, I have a few custom classes and forms, and a Debug form that recieves reports from all the other classes about what is they are doing. Messages are added to a listview chronologically as they are reported and to a tree-view to sort them into categories, thus allowing to focus on one type of message (user activity, SQL queries, etc)
    My MainGUI creates the Debug form and reports to it, and every other Class/Form MainGUI creates reports to the MainGUI which then passes the report on.

    For each Class/Form, I have an event:
    Code:
    public event CodeFlowHandler CodeFlowInfo;
    private CodeFlowEventArgs codeFlowData = new CodeFlowEventArgs();
    public delegate void CodeFlowHandler(UsersManager sender, CodeFlowEventArgs e);
    // 'UsersManager' is the name of the class the eventHandler is being defined in
    and a method to report:
    Code:
    private void Report(string msg, CodeFlowEventArgs.Categorias tipo, bool supress)
    {
    	codeFlowData.Mensaje = msg;
    	codeFlowData.Categoria = tipo;
    	codeFlowData.SupressLast = supress; // useful for countdowns
    	if (CodeFlowInfo != null) CodeFlowInfo(this, codeFlowData);
    }
    What I'd like to do is just define a new 'Reporter' Class, so that I can just define that and have the event and the reporting encapsulated in it. The first problem I found so far is that the definition for the eventHandler includes the class type of the sender. What I would need to do is replace the "sender's" type for a generic declaration that will adapt to the class/form I instance the Reporter in.

    That means the definition for the eventHandler would have to look something like:
    Code:
    public delegate void CodeFlowHandler(typeof(this) sender, CodeFlowEventArgs e);
    But (obviously) that doesn't work!
    I tried:
    typeof(this) and typeof(base)
    Type.GetType(this) and Type.GetType(base)

    with no luck. Does anyone know how to get the type of a class? Thanks for the help!

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: typeof(this) for a custom class

    Quote Originally Posted by Nikel View Post
    ...What I would need to do is replace the "sender's" type for a generic declaration...
    Use generics: MSDN tutorial link
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    Re: typeof(this) for a custom class

    If only I had searched better =)
    Thanks!

    The new implementation is not fully generic but close enough. I guess some editing after each copy/paste is not so terrible!

    My new Reporter class definition now looks like this:
    Code:
    public class Reporter<sourceType>
    The class is completely independant of the object creating it. The one last thing I'd like to do is make its declaration also independant of where it is being instantiated.
    For example, the UsersManager class defines a Reporter, and then initializes it like this:
    Code:
    Reporter localReporter = new Reporter<UsersManager>(this);
    // the need for the 'this' parameter is because it has to have a reference to the Reporters 
    // parent (the sender) when it triggers the report event
    Is there a way of writing that in a more generic way? How can I get the type of the class I'm in?
    Code:
    Reporter localReporter = new Reporter<type of where this is being instantiated>(this);
    Can this be done? Not a very critical problem, but since I'm already asking... =)
    Thanks again for the help!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured