Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

C# Accessing Class Data, Beginner Question

I am trying to make a database of weapons and have multiple attributes assigned to each weapon, such as name, attack value, buy value, sell value, etc.

namespace Testing
{ 
    public class Weapon
    {
        public string name;
        //other attributes would go here too
    }
    
    class Program
    {
        static void WeaponBuilder()
        {
            Weapon bSword = new Weapon();
            bSword.name = "Bronze Sword";
            //many other weapons would be built here
            Console.WriteLine(bSword.name); //works fine
        }

        static void Main(string[] args)
        {     
            WeaponBuilder();
            Console.WriteLine(bSword.name); //error: bSword does not exist in the current context
            WeaponShop();
            Console.ReadLine();
        }

        static void WeaponShop()
        {
            Console.WriteLine("Buy " + bSword.name + "?"); //error: bSword does not exist in the current context
        }
    }
}

I need to be able to access the weapon's data outside of where it was constructed. Any help is appreciated. I know this is a noob question and I apologize.

question from:https://stackoverflow.com/questions/65845295/c-sharp-accessing-class-data-beginner-question

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can put the data at the class level instead of an ephemeral local scoped variable:

class Program
{
    static public Weapon bSword { get; } = new Weapon();

    static void InitializeWeapon()
    {
        BSword.name = "Bronze Sword";
        Console.WriteLine(bSword.name); //works fine
    }

    static void Main(string[] args)
    {     
        InitializeWeapon();
        Console.WriteLine(BSword.name);
        WeaponShop();
        Console.ReadLine();
    }
  ...
}

Therefore it will be accessible inside the class and from outside in read-only mode here, as a composite.

It seems you want to manage several weapons, so you can for example use a List:

public class Weapon
{
  public string Name { get; set; }
}

class Program
{

  static public List<Weapon> Weapons { get; } = new List<Weapon>();

  static void InitializeWeapons()
  {
    Weapons.Add(new Weapon { Name = "Bronze Sword" });
    Weapons.Add(new Weapon { Name = "Silver Sword" });
    foreach ( var weapon in Weapons )
      Console.WriteLine(weapon.Name);
  }

  static void Main(string[] args)
  {
    InitializeWeapons();
    ShopWeapon();
    Console.ReadLine();
  }

  static void ShopWeapon()
  {
    foreach ( var weapon in Weapons )
    {
      Console.WriteLine($"Buy {weapon.Name}?");
      // ...
    }
  }
}

What Are OOP Concepts?

What is abstraction in C#?

How to choose between public, private and protected access modifier?

What is polymorphism?

What is the difference between an interface and a class?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...