Static Dictionary values cannot access from another class

DMabulage

New member
Joined
Nov 21, 2024
Messages
1
Programming Experience
Beginner
I have a class with a static dictionary; when I try to access the values of that dictate, it throws an error.

C#:
 public class ShortCodes
 {
     public static readonly Dictionary<string, string> PersonNameTypeCode = new Dictionary<string, string>
     {
         { "Legal", "LGL" },
         { "Maiden Name", "MDN" },
         { "Birth Name", "BTH" },
         { "Tribal Name", "TRB" },
         { "Preferred Name", "PRF" },
         { "Also Known As", "AKA" },
         { "Stage Name", "STG" },
         { "XFR", "XFR" }
     };
 }

C#:
 var e = ShortCodes.PersonNameTypeCode["Legal"];

1732184178152.png


1732184198595.png


I'm using this in a playwright project.
 
I'm not sure how getting a value by key could generate that error message, especially when the key you're using is "Legal" and the error message says it's "Cul-De-Sac". Something else is going on that you haven't told us. While it's not specified as a requirement here like it is at Stack Overflow, you should endeavour to provide a minimal, reproducible example so that we can see the behaviour you describe and debug the code for ourselves. You might at least show us that code in context and provide the full stack trace.
 
I suspect that the OP has other static dictionaries the same ShortCodes class. As I recall, when any static member of a class is first accessed, the static constructor for the class is invoked and all the static members of the class are initialized at that point.
 
I suspect that the OP has other static dictionaries the same ShortCodes class. As I recall, when any static member of a class is first accessed, the static constructor for the class is invoked and all the static members of the class are initialized at that point.

I suspect that you're right. There's probably another dictionary where "Cul-De-Sac" is a key and it's simply added twice.

For the record, if you get an error message that specifies a value like that, one of the very first things you should do is search for that value in your code. I suspect that doing so would have allowed you to fix your own problem. Being a beginner means that you don't know a lot of programming-specific stuff but you can still use the very same logic that you have applied to non-programming problems your whole life.
 
Back
Top Bottom