views:

29

answers:

1

I don't understand how to loop over a static dictionary contained in a static class from my aspx page. I have this for the static class

public static class ErrorCode  

{
    public static IDictionary<int, string> ErrorCodeDic;

    static ErrorCode()
    {
        ErrorCodeDic = new Dictionary<int, string>()
        { 
            {1, "a problem"},
            {2, "b problem"}
        };
    }
}

MORE SPECIFIC I can get it to work by spelling it out like this in the aspx part

foreach( System.Collections.generic.KeyValuePair<int, string> kvp in MyLibrary.Dictionaries.ErrorCode.ErrorCodeDic) 

But I thought I could shorthand it by declaring variables in the code behind?

Public KeyValuePair<int, string> error;
Public ErrorCode.ErrorCodeDic ErrorCodes; OR
Public ErrorCode.ErrorCodeDic ErrorCodes = ErrorCode.ErrorCodeDic; "

I get build errors "The type name 'ErrorCodeDic' does not exist in the type ErrorCode.

And then in the aspx page use

foreach( error in ErrorCodes)
+3  A: 

You can loop over all pairs like this:

foreach( KeyValuePair<int, string> kvp in ErrorCode.ErrorCodeDic)
{
  Response.Write(string.Format("Key = {0}, Value = {1}", kvp.Key, kvp.Value));
}

For your updated case, in the code-behind:

public IDictionary<int, string> ErrorCodes = MyLibrary.Dictionaries.ErrorCode.ErrorCodeDic;

in the aspx:

foreach(var error in ErrorCodes) { }

Alternatively, nothing in the codebehind, and this in the aspx:

<%@ Import Namespace="MyLibrary.Dictionaries" %>
....Content...
<% foreach(var error in ErrorCode.ErrorCodeDic) { %>
  .. something ..
<% } %>
Nick Craver
I see what you mean, I was trying to do something different. is it possible to define the keyvaluepair kvp outside of the foreach and just do what I describe in my "MORE SPECIFIC"
Breadtruck
@Breadtruck - You can do `foreach(int code in ErrorCode.ErrorCodeDic.Keys) {`... is that what you mean?
Nick Craver
@Breaktruck - Still not sure I understand, you can add a reference yes, like this: `var dict = MyLibrary.Dictionaries.ErrorCode.ErrorCodeDic;` then do: `foreach(var kvp in dict) {....`
Nick Craver
I made the MORE SPECIFIC even more specific. I think it will be clear now, at least I hope so!
Breadtruck
Hmm I think I understand, but what about the var kvp part?
Breadtruck
@Breadtruck - Updated the answer to give you some more options, you van use `var` in the later editions of .Net, no need to specify the variable type...let the compiler figure it out if you're really striving to keep it brief. Here's more reading on var: http://msdn.microsoft.com/en-us/library/bb383973.aspx
Nick Craver
When I try to create the reference like you suggested "Public ErrorCode.ErrorCodeDic ErrorCodes = MyLibrary.Dictionaries.ErrorCode.ErrorCodeDic;" I get build errors "The type name 'ErrorCodeDic' does not exist in the type ErrorCode.
Breadtruck
@Breadtruck - Wow I need sleep...updated, should be `IDictionary<int, string>` for the type.
Nick Craver
Thanks that did the trick!!
Breadtruck