I am very new/ a beginner to coding. I'm using c# 6.0 in Microsoft Visual studio 2022.
I'm trying to create some sort of search function, but perhaps there is a better way to do it.
I created a class(didn't include all of the code bc of space) and gave it
public void Attributes()
{
Console.WriteLine(affinity + ", " + aoe + ", " + effect + ", " + pointOfOrigin);
}
and assigned variables in the main program/code like this
Spell FireBall = new Spell("Fire", "Sphere", "Explosion", "Thrown");
What I want to happen is to type the spell name(ex: Fireball) and then have it return the: Fire, Sphere, Explosion, Thrown.
I know that if you type FireBall.Attributes(); it will return the attributes, but I cannot figure out how to take user input to call the attributes. My first guess is that it has something to do with Console.ReadLine and assigning a variable to it but everything I've tried just turned errors.
As I said I'm a beginner and any help is much appreciated. I also apologize if I'm not being specific enough or used the wrong terms, as I'm still learning all of the vocabulary for the different stuff. I'm having a lot of fun learning, so please be nice.
From what I can gather, what you're trying to do is have the user input a keyword (a name in this instance) and return an object associated with that name, then print its attributes (this part you've already done by creating the
Attrbiutes()
method).There are two main ways of doing this, by reflection (hard, slow, ridiculous), and by a dictionary (easy, FAST, made for this).
I will not get into the reflection method, as it requires a deep understanding of the language that I cannot give you in a Stackoverflow answer.
Here's the Dictionary method:
and here's the dotnet fiddle: https://dotnetfiddle.net/iB8w3a
Enter your input in the bottom, then press enter input it.
