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)
Breadtruck
  • 1,943
  • 5
  • 25
  • 39

1 Answers1

3

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
  • 623,446
  • 136
  • 1,297
  • 1,155
  • 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 Mar 06 '10 at 04:13
  • @Breadtruck - You can do `foreach(int code in ErrorCode.ErrorCodeDic.Keys) {`... is that what you mean? – Nick Craver Mar 06 '10 at 04:16
  • @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 Mar 06 '10 at 04:27
  • I made the MORE SPECIFIC even more specific. I think it will be clear now, at least I hope so! – Breadtruck Mar 06 '10 at 04:28
  • Hmm I think I understand, but what about the var kvp part? – Breadtruck Mar 06 '10 at 04:31
  • @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 Mar 06 '10 at 04:35
  • 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 Mar 06 '10 at 04:43
  • @Breadtruck - Wow I need sleep...updated, should be `IDictionary` for the type. – Nick Craver Mar 06 '10 at 04:45