views:

122

answers:

5

I need to pass a list of Days (number and name) to an view!

Whats the best way to do this?

I was thinking of creating a generic collection, but not sure how? or an array?

A: 

For something like this you can use a dictionary... simple key/value pairs

Dim d as New Dictionary(Of Integer, String)
d.Add(0,"string2")
'   etc
Jason
A: 

If I only had those 2 options, I'd use an Array.

Because an array is immutable. You can't accidentally "add" or "removes" something which is what the view should be. It's kind of a defensive programming.

And you can still use foreach just fine.... so why goes Collection? no need at all.

chakrit
Arrays are mutable. You can use List.AsReadOnly to get a readonly view of a list to send elsewhere.
Ben Lings
@Ben Lings An array is immutable when used in a *normal* way. I didn't say that objects contained in an array aren't immutable. And the author asks for either Collection *or* an array... as I've said in the first sentence....
chakrit
Arrays are constant length, but have mutable elements.
Ben Lings
See http://blogs.msdn.com/ericlippert/archive/2008/09/22/arrays-considered-somewhat-harmful.aspx
Ben Lings
@Ben Lings and did I said otherwise?
chakrit
A: 

You really ought to use an IEnumerable<T>. Add a List<T> to the ViewData and then cast it as an IEnumerable<T> on the other side.

Andrew Hare
+1  A: 
Dictionary<string, int> days = new Dictionary<string, int>();

days.Add("Sunday", 1);

...

Mark
+1  A: 

Why not an enum?

enum days { Monday = 1, Tuesday = 2 ... }

there is an enum in the framework itself for that: DayOfWeek
Colin