5 Replies - 448 Views - Last Post: 26 June 2012 - 06:05 PM Rate Topic: -----

#1 navy4422  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-May 12

Event Calendar - storing and accessing events

Posted 25 June 2012 - 01:16 AM

Hi all,

I'm currently working on a calender program in which I want to be able to add and view events in.
I have the calender portion for the most part done and now I'm looking into events and I have an issue in this part.

I was wondering if anyone had a better idea than me for storing and accessing events (The program is done on winform)

My idea was to use arrays or lists to store parts of the event (Event title, time, day, month, year, and description of the event). and then to save each part to a textfile with a special character inbetween each like an \ or something similar and to read and split into the array or list. I would keep the lists sorted by time then day then month then year and save it to the textfile after updates to ensure no data lost if the user exits. I will also keep a back of the previous textfile so if something happens to the original then no data lost.

For calender access I have a day view, week view, and month view so based on which one is trying to access the events I plan to start at for example, find the first index of the year then the first index of the month it is looking at and then to find the first index of the day in question then to display all of the days data into the appropriate hourly text boxes.

Is there a better way to do this? I'm worried that this isn't the most efficient way to do this.

Any suggestions are appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Event Calendar - storing and accessing events

#2 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 313
  • View blog
  • Posts: 490
  • Joined: 13-June 12

Re: Event Calendar - storing and accessing events

Posted 25 June 2012 - 01:59 AM

I would suggest you create a CalendarEntry class and serialise a List<CalenderObject> to a local XML file. That way you can deal with each entry as an object.
Was This Post Helpful? 2
  • +
  • -

#3 navy4422  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-May 12

Re: Event Calendar - storing and accessing events

Posted 25 June 2012 - 08:01 PM

View PostMrShoes, on 25 June 2012 - 01:59 AM, said:

I would suggest you create a CalendarEntry class and serialise a List<CalenderObject> to a local XML file. That way you can deal with each entry as an object.


Okay so I've never done anything like this but I kinda understand I made some outline code is this what I should be doing.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Live_Ad
{
    class CalenderData
    {
        int _eventID;
        string _date;
        string _time;
        string _title;
        string _description;

        private void addEvent(string title, string date, string time, string description)
        {
            //Code to add an event
        }

        private void editEvent( int eventID, string title, string date, string time, string description)
        {

        }

        private void deleteEvent(int eventID)
        {

        }

        private void saveData()
        {

        }

        private void loadData()
        {

        }

        public string Date
        {

        }

        public string Title
        {

        }

        public string Time
        {

        }

        public string Description
        {

        }

    }

    
}


Was This Post Helpful? 0
  • +
  • -

#4 navy4422  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-May 12

Re: Event Calendar - storing and accessing events

Posted 25 June 2012 - 08:26 PM

Also I haven't done anything with xml's before how would I go about saving and loading from them
Was This Post Helpful? 0
  • +
  • -

#5 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 313
  • View blog
  • Posts: 490
  • Joined: 13-June 12

Re: Event Calendar - storing and accessing events

Posted 26 June 2012 - 04:03 AM

You're on the way. Personally, I would make a simple CalenderEntry object like this:
class CalendarEntry
{
	private int EventID;
	private DateTime EventDate;
	private string Title;
	private string Description;
	
	//In place of your addEvent() method, this will call when 
	//you add a method to the List<CalendarEntry> we will create later
	public CalendarEntry(int id, string title, string desc)
	{
		EventID	= id;
		Title = title;
		Description = desc;
		EventDate = DateTime.Now;
	}
	
	//Edits the CalendarEntry, you will have selected this so do not
	//need the EventID to be passed (unless you want to change it)
	public void Edit(string newtitle, string newdesc)
	{
		Title = newtitle;
		Description = newdesc;
	}
}



That's your basic CalendarEvent object. Now, to control them, we need to create an object that will hold all the instances of CalenderEntry our users create. We will save and load the list, not each CalendarEntry: faster, easier, and provides us a way to convert back into object form using XML serialization. This is where you might need to change things based on how your application is being designed.

class CalendarList
{
	[XmlElement("CalList")]
	private List<CalendarEntry> CalList;

	private int CreateID()
	{
		int newID = 0;
		//Whatever logic you use to generate the ID...
		return newID;
	}

	//Assuming you are using Windows Forms here...
	private void AddEntry()
	{
		int newID = CreateID();
		string nm = tbName.Text;//Assuming you have a textbox that holds the name...
		string desc = tbDesc.Text;//As above...
		CalList.Add(new CalendarEntry(newID, nm, desc);
	}

	private void Edit()
	{
		//As simple-looking as it gets
		int searchID = tbEditID.Text;
		string newTitle = tbTitle.Text;
		string newDesc = tbDesc.Text;

		foreach(CalendarEntry entry in CalList)
		{
			if(entry.EventID == searchID)
				entry.Edit(newTitle, newDesc);
		}
	}
	
	private void Delete()
	{	
		int searchID = tbDelID.Text;
		for(int i = 0; i < CalList.Length; i++)
		{
			if(CalList[i].EventID == searchID)
				CalList.Remove(CalList[i]);
		}
	}

	private void Save()
	{
		XmlSerializer xmlSer = new XmlSerializer();
		TextWriter textWrit = new StreamWriter(@"C:\Calendar.xml");
		xmlSer.Serialize(textWrit, CalList);
		textWrit.Close();
	}

	private void Load()
	{
		//Check file exists before doing this.
		XmlSerializer xmlDeser = new XmlSerializer(typeof(List<CalandarEntry>));
		TextReader textRead = new StreamReader(@"C:\Calendar.xml");
		CalList = (List<CalendarEntry>)xmlDeser.Deserialize(textRead);
		textRead.Close();
	}	
}



Just as a note, you do not need 2 members for date and time. The DateTime object stores both, and you can access them seperately, such as CalenderEntry.EventDate.Time member.

Note as well, this is just basic code (written at work without Visual Studio) so I haven't included any references (usings) and you will need to test files exist before you deserialize from xml, but this should be more than enough to get you going. You might also need to tag elements as being XMLSerializable. This is a good basic tutorial about XML serialization.

Let me know if you need any more help.
Was This Post Helpful? 1
  • +
  • -

#6 navy4422  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-May 12

Re: Event Calendar - storing and accessing events

Posted 26 June 2012 - 06:05 PM

Thanks I'll look at it all in a bit I've been streaming over so many xml tutorials and none of them seemed to help me and most just confused me more. Hopefully this can get me somewhere.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1