Hey yall C# coding nub here. I am trying to convert a simple program I made that rolls a D&D character from Python into C#.

I would like to reference a dictionary in a CharGen class that I have made with a CharRoller method that rolls a 4 dice 6 times, so I can append the output of the dice roll as a value of a key. I have tinkered around with it and I can’t figure out how to reference the stats dictionary outside of the CharStats() object and into CharRoller() .Any help logically, emotionally, or metaphysically would be greatly appreciated. Thanks and stay safe yall.
I’m not a huge fan of magic strings. The “key” in your dictionary qualifies as a magic string because it uses a “hard-coded” essentially random block of text (meaningful to you but no one else) to indicate a single value.
So instead I usually recommend an enumeration. Like so:
There. Now we’ve defined a set of names we can use to store information.
But we’ll need a class we can use to generate our character. In this case I chose to make the class and the method both static. You do not need to do this, if the way you choose to create instances of classes and call your methods don’t require it, but for this very limited example, this is how I wrote it. Just remember it’s not the *only* way… just one way.
Also, there’s extra word-wrapping because of reddit formatting.
What does a call to CharacterGenerator.CreateStats() do?
Instantiates a new instance of the Random class for us to use.
Creates a list of Stat keys (from our enumeration) that drives how our output is created.
Instantiates a new Dictionary<Stat, int> that is initially empty. We’ll add to it in our loop.
Loop over every key, roll the {6}-sided dice {4} times (both of those values are optional input parameters) for each of the total score of a stat, then add the current stat and the score to our dictionary.
Return our dictionary back to the caller.
Output… so what does the output of the above do for us? We could call it like this:
And the output would be:
The numbers obviously are random and with enough rolls we could see that the minimum would be 4 (all 1’s) and maximum 24 (all 6’s)
You could consider moving the “dice rolling” into a separate method or class if fetching combinations of dice rolls is something the application does often. This decouples the “stat creation” from “dice rolling”. Generally you do not want one method doing two (or more) things like this small example does.


Also a better data structure than a dictionary for this would be to use a set of classes. A dictionary is good for when you don’t know how many items might be in the list… 1 or 1000… but for something like stats, you’ll always have those six. Any list structure is probably overkill. A single class with those six properties might make more sense than a list… or it might not. Your application–your decision on what goes where.
This is seriously the most informative comment I’ve read while learning to program, thanks.
you’ll always have those six
Hey, the PM was looking for you! She had a new Jira card on her hand.
But seriously, this is a good answer.
If you want to reference your dictionary in your class, you need either a private field or a property. You can then set this variable by creating a constructor and initializing the value there
Then if you want to modify the value of a specific key, you would do for example: stats[“Intelligence(int)] += whatever value you want to add.
For code clarity purposes, I recommend declaring all your strings in a private const string variable so you don’t have to retype it every time, and change all of them if you change one
can’t figure out how to reference the stats dictionary
Read this: https://docs.microsoft.com/en-us/cpp/c-language/scope-and-visibility?view=msvc-160
The root cause answer is simple. In your screenshot, CharStats() is NOT an object as you say it is, but it’s a method. The dictionary stats you declare inside there is just a local variable. Try to make CharStats a class instead — or just make the stats dictionary a class field of CharGen and get rid of CharStats() completely
C# devs
null reference exceptions

source