I have built a custom calendar control (like google calendar and outlook calendar)
I want to add the functionality to handle events(Add, remove, etc). I already have it, but I want it to feel as generic as possible.
How my code currently looks:
myCalendar.Events.Add(myEvent);
myCalendar.Draw //this adds all the events to the calendar
Example of what I think looks more generic:
myCalendar.Events.Add(myEvent);
myCalendar.Events.Remove(unwantedEvent);
myCalendar.Events.Draw
My question. Is the second code-snippet best practice or should I just go with a separate class for handling drawing and such. (which I currently have)
If the second snippet is better, how do I go about doing so?
Rough Example of one of the methods I've tried (to give you an idea of what I want to accomplish)
public _Events Events = new _Events();
public class _Events
{
public List<Event> Events = new List<Event>();
void Add(...)
{
//Add event to Events
}
void Clear()
{
...
}
}
public class Event
{
public String Title { get; set; }
}
The problem with the above code is that i want to be able to return a default value like so...
listOfEvents = myCalendar.Events
Disclaimer: I have tried in vain to find a solution, but couldn't find the right keywords to find it
I am not sure how to handle your code snippet, so I pulled out some code that I use to store custom objects that get rendered to a view. You can use code in the class to process how data gets returned when you use an accessor.
using System.ComponentModel;
public abstract class DiagramObject : INotifyPropertyChanged
{
//the actual variable
private string _name;
//how the variable is accessed and set
public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("Name");
}
}
//
public string AbbreviatedName
{
get
{
if (_name.Length>=1)
{
return _name.Substring(0, 1);
}
else
{
return "NA";
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
These are then made accessible to use for display by creating an observable collection and initializing. If you are using WPF, it can then be bound to a listbox.
using System.Collections.ObjectModel;
private ObservableCollection<DiagramObject> _objects;
public ObservableCollection<DiagramObject> Objects
{
get { return _nodes ?? (_nodes = new ObservableCollection<Node>()); }
}
public MainClass
{
_objects = new ObservableCollection<DiagramObject>(constructor that makes the objects);
}
I pulled a large amount of code for my project from https://github.com/High-Core/WPFSamples/tree/master/src/WPFSamples/Samples . This is being added to by the person who makes it and has a lot of really interesting work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With