Search code examples
c#eventsobjectunboxing

Can't access members of a class after unboxing


I am learning advanced C#.In the following Code i am trying to do Event handling

i get Error while accessing members of class sender after unboxing //Compiler is not letting me use these names

//    Console.WriteLine("Sender is {0} and message is {1}",obj.name,obj.messgae);

Why is that so? Is that what we call boxing and unboxing, if i am not getting confused.

In all examples i have done so far, there is event class inheriting EventArgs. What is need of that class .Although here i have not used this class instead i have used Eventargs directly(just made it).

      namespace ConsoleApplication5
    {
        class eventclass : EventArgs
        {
            string var;
        }
        delegate void GenerateEvent(object source, EventArgs e);
        class Sender
        {
            public void MyMethod()
            {
                Console.WriteLine("My method Called");

            }
            private string name;
            public string Name
            {
                get
                {
                    return name;
                }
                set
                {
                    name = value;
                }
            }

            private string message;
            public string Message
            {
                get
                {
                    return message;
                }
                set
                {
                    message = value;
                }
            }
            public event GenerateEvent OnAlert;

            public void Sendit()
            {
                Console.WriteLine("Sender: Sending Message Now");
                OnAlert(this, new EventArgs());
                Console.WriteLine("Sender: Message Sent");

            }
        }
        class Receiver
        {
            public static void ReceiveMsg(object sender, EventArgs e)
            {
                Console.WriteLine("Receiveing Message");
                object obj = new object();
                obj = sender as Sender;

                /*Here i get Error while accessing members of class sender after unboxing 
                //Compiler is not letting me use these names
                //    Console.WriteLine("Sender is {0} and message is {1}",obj.name,obj.messgae);

                */


                Console.WriteLine("Received" + obj.GetType());
            }

        }
        class Program
        {
            static void Main(string[] args)
            {
                Receiver r = new Receiver();
                Sender s = new Sender();
                s.OnAlert += new GenerateEvent(Receiver.ReceiveMsg);
                s.Sendit();

            }
        }
    }

kindly comment about my Coding Styles and ambiguities. Thanks in Advance


Solution

  • This is the problem:

    object obj=new object();
    obj=sender as Sender;
    

    The second line doesn't change the compile-time type of obj. It's still just object. (And your first line that creates a new object is completely pointless.) The compiler doesn't care what you've done with obj when you try to use obj.name - it cares about the compile-time type. You want:

    Sender obj = (Sender) sender;
    

    (See my recent blog post about why the cast is preferable over using as here.)

    You also want to use the public properties rather than the private fields:

    Console.WriteLine("Sender is {0} and message is {1}", obj.Name, obj.Message);
    

    Note that this casting is not unboxing, by the way - you have no value types involved (Sender is a class) so there's no boxing or unboxing involved.