2d arrays

  • Thread starter Thread starter David Sobey
  • Start date Start date
D

David Sobey

basic question, confused by the type system of c#:

i'm making a calendar prog, with an array of days, each day being an array
of events. I want something like:

public struct Event
{
string name;
string UID;
}
private ArrayList events;
private ArrayList (events) days;

I'm aware this is kinda psuedo-code, so how should i really code this? If
necessary, the days can be a 31 sized static array.

cheers
dave
 
David Sobey said:
basic question, confused by the type system of c#:

i'm making a calendar prog, with an array of days, each day being an array
of events. I want something like:

public struct Event
{
string name;
string UID;
}
private ArrayList events;
private ArrayList (events) days;

I'm aware this is kinda psuedo-code, so how should i really code this? If
necessary, the days can be a 31 sized static array.

Well, there are a number of choices. You could simply use an Event[12,31]
array or an array of ArrayLists or an ArrayList of ArrayLists, or an
ArrayList of arrays. Or you could use a hashtable. All depending on how you
want to access teh information.

Can you provide more information about what exactly you want to achieve?
 
Hi,

With several reasons it is strongly advice not to use arraylist to store
value types (at least in c# for sure).. please read about object boxing /
unboxing..

Nirosh.
 
i'm basically just trying to store a list of appointments for a user, so i
can send them to a monthly calendar. number of events/appointments per day
is arbitrary of course, but i suppose i could set number of days to 31. I
think an array of arrayLists is best but not sure how to declare and
instantiate it. Is this right:

public struct Event
{
string name;
string UID;
}
ArrayList day;
day[] month;

....

days = new ArrayList();
month = new day[31];

i should just try it huh? gotta have dinner first... :)

thanks for your help
dave
 
how on earth to i declare and define a static array of ArrayLists? And how
would i pass this to a function. I simply cannot get my code to work:

public struct Event
{
string name;
string UID;
}
ArrayList[] month;

....
public void SetEvents(ArrayList[] list)
{
month = new ArrayList[31];
month = list.Clone();
}

It INSISTS that month is an ArrayList, which it is NOT. It's supposed to be
a static array.
 
I really think your best bet would be to look at Hashtables, keyed by
DateTime, pointing maybe to ArrayLists of appointments.

As for your ArrayList[] concept, the ArrayList[] itself stands for an array
of ArrayLists, or more precisely, ArrayList references. You would have to
initialize every one of the members, like:

ArrayList[] month = new ArrayList[31];

for (int day = 0; day < 31; day++)
month[day] = new ArrayList();

But as I don't think you'd like to instantiate thousands of ArrayLists in
your calendar, I'd recommend something like the following:

Hashtable calendar = new Hashtable();

ArrayList GetAppointments(DateTime date)
{
date = date.Date; // strip the time part, if any...

ArrayList appointments = calendar[date] as ArrayList;

if (appointments == null)
{
appointments = new ArrayList();
calendar[date] = appointments;
}

return appointments;
}

If you need any more help, you can also contact me directly (remove the blah
from my address)

HTH,
Stefan
 
David Sobey said:
how on earth to i declare and define a static array of ArrayLists? And how
would i pass this to a function. I simply cannot get my code to work:

public struct Event
{
string name;
string UID;
}
ArrayList[] month;

...
public void SetEvents(ArrayList[] list)
{
month = new ArrayList[31];
month = list.Clone();
}

It INSISTS that month is an ArrayList, which it is NOT. It's supposed to
be a static array.

Month is an array, in this particualar circumstance. The line
ArrayList[] month;
Creates a variable named month that is typed an array of the class
ArrayList. I don't quite understand what you mean by it insisting that month
is an ArrayList. Every test I can come up with doesn't seem to fail. Infact
the only problem I can find with your code is that the call to list.Clone()
needs to be cast to ArrayList[].

Could you provide a short but complete[1] example of the issue?

1. http://www.yoda.arachsys.com/csharp/complete.html
 
actually i think you're right, just the way i was using was a bit ignorant.
thanks for your help though. i've been programming haskell lately so it's a
bit tricky, seeing the type system is so different.

cheers
dave

--
"Aristotle said that some people were
only fit to be slaves. I do not
contradict him. But I reject slavery
becase I see no people fit to be masters"
C.S Lewis
 
David Sobey said:
actually i think you're right, just the way i was using was a bit
ignorant. thanks for your help though. i've been programming haskell
lately so it's a bit tricky, seeing the type system is so different.

Yes, I can certainly see that,;). Fundamentally different type systems can
be hard to overcome.

I hope you've been able to fix your problem.
 
"Aristotle said that some people were
only fit to be slaves. I do not
contradict him. But I reject slavery
becase I see no people fit to be masters"
C.S Lewis

I knew Aristotle was a brainy guy but 'gawd!', this is too much!.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
Back
Top