Studying Events args issue..

techker

New member
Joined
Jan 16, 2016
Messages
4
Programming Experience
1-3
Hey guy's i just came out of school and in my internship..i have a little project to test the events args and polymorphisme..Here are the req:

need to create and abstract class CustomEventArgs:Event Args
with public virtual string Hello

then he says i need to create a class Custom1:event args
in it override string Hello = Hello welcome..

the custom2
override hello = Hello Welcome Home

custom3
override hello = Welcome to main

so then i need to create 3 buttons with an event handler that writes a label with the text of the override..

so im confused on how to call the class custom to write tto label?

i got
C#:
namespace Work1
{
    public partial class Form1 : Form
    {
        public delegate void EventHandler();


        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
           


        }
        
       public  abstract class CustomEventsArgs : EventArgs
        {


            
            public abstract  string Message();


        }
       class Custom1 : EventArgs
        {
            public string Message { get; set; }


            public override string ToString()
            {
                return Message="Welcome";
            }
          




        }


        class Custom2 : EventArgs
        {
            public string Message { get; set; }


            public override string ToString()
            {
                return Message = "Welcome2";
            }






        }
        class Custom3 : EventArgs
        {
            public string Message { get; set; }


            public override string ToString()
            {
                return Message = "Welcome3";
            }






        }


        private void button1_Click(object sender, EventArgs e)
        {
            call event handler...
        }

}
}
 
I don't think that you've actually conveyed the correct instructions to us.
need to create and abstract class CustomEventArgs:Event Args
with public virtual string Hello
Firstly, is the member actually supposed to be named Hello? If so, why do you have a member named Message but no member named Hello? My guess is that the member is actually supposed to be named Message and return the value "Hello". So, either you have given us incorrect information or you have obviously not done as instructed.

Secondly, the instructions state that the member is supposed to be virtual and you have declared it abstract. The fact that it appears that it's supposed to be virtual is reason to believe that the member is supposed to be named Message and return a value of "Hello".

Also, I'm not sure whether you have again given us incorrect information but it doesn't make sense for Custom1, Custom2 and Custom3 to inherit EventArgs directly. The only way this whole exercise makes sense is if those three classes inherit CustomEventArgs. By inheriting that class, they can then override the Message member of that class.
 
I don't think that you've actually conveyed the correct instructions to us.Firstly, is the member actually supposed to be named Hello? If so, why do you have a member named Message but no member named Hello? My guess is that the member is actually supposed to be named Message and return the value "Hello". So, either you have given us incorrect information or you have obviously not done as instructed.

Secondly, the instructions state that the member is supposed to be virtual and you have declared it abstract. The fact that it appears that it's supposed to be virtual is reason to believe that the member is supposed to be named Message and return a value of "Hello".

Also, I'm not sure whether you have again given us incorrect information but it doesn't make sense for Custom1, Custom2 and Custom3 to inherit EventArgs directly. The only way this whole exercise makes sense is if those three classes inherit CustomEventArgs. By inheriting that class, they can then override the Message member of that class.


Thx for the reply..i have been looking up eventargs all night..
So the goal is to use events.
The 3 buttons change the text as stated

Like you said yes the goal is to override the message on press..

Thats the part i cant seem to get..how can i when i pres custom1 it changes the txt to it..
 
how can i when i pres custom1 it changes the txt to it..

That's a completely nonsensical statement. If that's what you're trying to do then it's no wonder you can't figure out how to do it. If you can't provide a coherent description of what you're trying to achieve then you're obviously not going to be able to achieve it and neither are we.

So, the idea here is to create multiple custom EventArgs types. EventArgs and anything derived from it are used to pass data from an object raising an event to an object handling that event. An object of that type is intended to be the second argument to an event handler. The source creates the object, populates it with the appropriate data and then raises the event. The target handles the event and gets the data from the object in the event handler. It's that simple.

In your case, you define CustomEventArgs to inherit EventArgs and then Custom1, Custom2 and Custom3 to inherit CustomEventArgs. When you want to raise an event, create an object of the appropriate type and raise the event. Presumably you are supposed to handle three different events and use the CustomEventArgs type for the second parameter in each case. You can do that and get the Message member value from it and it will be a different value in each case because, while the reference is type CustomEventArgs, the objects will be types Custom1, Custom2 and Custom3 respectively.
 
That's a completely nonsensical statement. If that's what you're trying to do then it's no wonder you can't figure out how to do it. If you can't provide a coherent description of what you're trying to achieve then you're obviously not going to be able to achieve it and neither are we.

So, the idea here is to create multiple custom EventArgs types. EventArgs and anything derived from it are used to pass data from an object raising an event to an object handling that event. An object of that type is intended to be the second argument to an event handler. The source creates the object, populates it with the appropriate data and then raises the event. The target handles the event and gets the data from the object in the event handler. It's that simple.

In your case, you define CustomEventArgs to inherit EventArgs and then Custom1, Custom2 and Custom3 to inherit CustomEventArgs. When you want to raise an event, create an object of the appropriate type and raise the event. Presumably you are supposed to handle three different events and use the CustomEventArgs type for the second parameter in each case. You can do that and get the Message member value from it and it will be a different value in each case because, while the reference is type CustomEventArgs, the objects will be types Custom1, Custom2 and Custom3 respectively.


like i said sorry for the confused answers.im just really confused on custom args.is there a small snip of code that i could see?some tutorial?but your description is right.
 
It's simple inheritance. Anything on inheritance is relevant. Forget the fact that this has anything to do with events for a moment. Just think of EventArgs as a standard base class. CustomEventArgs inherits that base class and adds a member with a default implementation. Custom1, Custom2 and Custom3 inherit CustomEventArgs and override that default implementation with their own custom implementations. That's nothing specific to do with events and is just plain inheritance. Now, you can pass a Custom1, Custom2 or Custom3 object wherever an object of type CustomEventArgs is expected because objects of those types ARE CustomEventArgs objects, just as cats, dogs and fish are all animals. If you have a reference to a CustomEventArgs object, such as in an event handler, then you can access the aforementioned member and you will get the value determined by the implementation in the actual object, e.g. even though the reference may be CustomEventArgs, if the object is type Custom1 then you'll get the value from the overridden member in Custom1 rather than the default implementation in CustomEventArgs. If that's still confusing then you need to do some reading and exercises on inheritance.
 
It's simple inheritance. Anything on inheritance is relevant. Forget the fact that this has anything to do with events for a moment. Just think of EventArgs as a standard base class. CustomEventArgs inherits that base class and adds a member with a default implementation. Custom1, Custom2 and Custom3 inherit CustomEventArgs and override that default implementation with their own custom implementations. That's nothing specific to do with events and is just plain inheritance. Now, you can pass a Custom1, Custom2 or Custom3 object wherever an object of type CustomEventArgs is expected because objects of those types ARE CustomEventArgs objects, just as cats, dogs and fish are all animals. If you have a reference to a CustomEventArgs object, such as in an event handler, then you can access the aforementioned member and you will get the value determined by the implementation in the actual object, e.g. even though the reference may be CustomEventArgs, if the object is type Custom1 then you'll get the value from the overridden member in Custom1 rather than the default implementation in CustomEventArgs. If that's still confusing then you need to do some reading and exercises on inheritance.

thx for the reply.i will look it up thx.
 
Back
Top Bottom