DateRangeEventArgs

Can't construct new eventargs

Page 1 of 1

2 Replies - 1628 Views - Last Post: 14 January 2008 - 01:09 PM Rate Topic: -----

#1 trabelsy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-January 08

DateRangeEventArgs

Posted 14 January 2008 - 11:06 AM

hello
i'm working on a windows control, i inserted a toolstrip which one of his subitems is a monthcalendar control.
these monthcalendar controls can be added programatically, so i need an identifier to be sent along with the DateRangeEventArgs
i had no problem doing this in other eventargs, because most of them had an empty constructor, but the DateRangeEventArgs want me to input Start and End arguments in the base constructor.

can someone show me how to inherit from the DateRangeEventArgs class without giving these arguments?
public class MyDateEventArgs : EventArgs
{
	MyDateEventArgs() :
	   base()
{...}



Is This A Good Question/Topic? 0
  • +

Replies To: DateRangeEventArgs

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4404
  • View blog
  • Posts: 12,260
  • Joined: 18-April 07

Re: DateRangeEventArgs

Posted 14 January 2008 - 12:02 PM

Well you could setup something like this, but it has one bad side effect... it will have unsetable start and end properties. These properties are read only by default and since you don't set them using the constructor of your new class inherited from DateRangeEventArgs you won't get access to set them later. However it will allow you to inherit from DateRangeEventArgs and supply an identifier property.

Normally when you inherit you will want to take an existing constructor, override it to provide more detail with the addition of other arguments... so your class would take something like 3 parameters, 2 of which it gives to the base class and 1 it would give to a private variable. But as for an empty constructor for a class that inherits from a base that doesn't have one, you run into these types of problems with read only properties.

But here is a solution if you just need a custom class and don't plan on using the start/end properties or want all instances to be set to some hardcoded value.

public class MyDateRangeArgs : DateRangeEventArgs
{
	 // Private member variable
	 private string ident;

	 // Public constructor, which passes empty dateTimes to DateRangeEventArgs
	 public MyDateRangeArgs() : base(new DateTime(), new DateTime())
	 {
		  // Do stuff here
	 }

	 // Property for accessing an identifier
	 public string identifier
	 {
		  get { return ident; }
		  set { ident = value; }
	 }
}



This will allow you to define an object using an empty constructor and empty start, end times and a new property called "identifier" which you can set to a name for the instance etc.

Now if you want you could have a constructor that takes one parameter, a DateRangeEventArgs and copy over the start and end times like this...

public class MyDateRangeArgs : DateRangeEventArgs
{
	 // Private member variable
	 private string ident;

	 // Public constructor, which passes empty dateTimes to DateRangeEventArgs
	 public MyDateRangeArgs(DateRangeEventArgs d) : base(d.Start, d.End)
	 {
		  // Do stuff here
	 }

	 // Property for accessing an identifier
	 public string identifier
	 {
		  get { return ident; }
		  set { ident = value; }
	 }
}



This version of the class will allow you to construct your class taking in a DateRangeEventArgs object for the constructor and use it to set the start and end properties. This is great for events like the DateChanged event because it will allow you to "wrap" the DateRangeEventArgs parameter, provide it an identifier and use it as you would any other event args.

Example...

private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
	 // Create our object and pass in the DateRangeEventArgs passed to this event.
	 MyDateRangeArgs calendar1 = new MyDateRangeArgs(e);

	 // Set the identifier property with our name
	 calendar1.identifier = "Calendar1";

	 // Show a messagebox showing the name and its start and end properties (same as those used in the event)
	 MessageBox.Show("Identifier: " + calendar1.identifier + " start: " + calendar1.Start + " End: " + calendar1.End);

}



I hope you get the idea. Enjoy!

"At DIC we be DateRangeEventArg changing code ninjas!" :snap:

This post has been edited by Martyr2: 14 January 2008 - 12:02 PM

Was This Post Helpful? 0
  • +
  • -

#3 trabelsy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-January 08

Re: DateRangeEventArgs

Posted 14 January 2008 - 01:09 PM

10x a lot dude!
i'll try this at the company tomorrow...
10x for the effort...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1