Home > other >  How do I get readline to return the fields of a Class?
How do I get readline to return the fields of a Class?

Time:05-27

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.

CodePudding user response:

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:

using System;
using System.Collections.Generic;

//=== Spell class, this obviously isn't yours I'm just using this as an example
public class Spell {
    string affinity, aoe, effect, pointOfOrigin;
    
    public Spell(string affinity, string aoe, string effect, string pointOfOrigin) {
        this.affinity = affinity;
        this.aoe = aoe;
        this.effect = effect;
        this.pointOfOrigin = pointOfOrigin; 
    }
    
    public void Attributes()
    {
        Console.WriteLine(affinity   ", "   aoe   ", "   effect   ", "   pointOfOrigin);
    }
}

public class Program
{
    public static void Main()
    {
        //=== Create a dictionary of type Dictionary<string, Spell>. This means the dictionary has a string key and a Spell value for that key.
        Dictionary<string, Spell> Spells = new Dictionary<string, Spell>();
        
        //=== Add items to the dictionary, instead of setting a variable with its name, set the key to its name.
        // In this instance we're using names that are lowercase so we can do a case-insensitive search
        Spells["fireball"] = new Spell("Fire", "Sphere", "Explosion", "Thrown");
        
        Spells["freeze"] = new Spell("Ice", "Sphere", "Freeze", "Thrown");
        
        //=== Now here's where we ask the user what spell they'd like to print
        string input = Console.ReadLine().Trim().ToLower(); //=== Here we trim (remove whitespace from the start and end) and convert the input to lowercase.
        
        //=== Then we search for the spell in the dictionary
        if(Spells.ContainsKey(input)) {
            //=== If we get here, we found the spell
            Spell spell = Spells[input];
            
            //=== Print its attributes
            spell.Attributes();
        } else {
            //=== The spell was not found
            Console.WriteLine("Spell not found!");
        }
    }
}

and here's the dotnet fiddle: Searching for Fireball Search found Fireball

CodePudding user response:

If you want to print out an object properties, You can use GetProperties() method. See if this example helps You. Ps: Result is the Spell object for You.

  var obj = new Result()
            {
                displayName = "Display Name",
                id = "1234",
                name = "Name",
                slug = "",
                 imageUrl = "url"
            };
            var props = obj.GetType().GetProperties();
            var sb = new StringBuilder();
            foreach (var p in props)
            {
                sb.AppendLine(p.Name   ", "   p.GetValue(obj, null));
            }
            Console.Write( sb.ToString());

enter image description here

  • Related