Reputation: 19
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.
Upvotes: 0
Views: 67
Reputation: 11
Create your class library on another file, (a good name would be weapon models), Make sure your class is set to static public (you pretty much set your data to static most the time, and public so it is accessible). Now in your Main file declare it on the top with a using statement or start typing the class you want to initiate and press Ctrl period. Then select add a using statement. This will automatically select your library, then you can use it! Also another shortcut, if you type the letters prop then tab twice, it will create a property for your class. Just be sure to initialize it! Also this was done using Visual Studio, some of the shortcuts might not work on VS code.
Upvotes: 0
Reputation:
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}?");
// ...
}
}
}
How to choose between public, private and protected access modifier?
What is the difference between an interface and a class?
Upvotes: 1
Reputation: 906
A basic structure for your classes can be something like:
// abstract means you cannot directly create a Weapon.
// Is is intended to be a base for concrete weapons
public abstract class Weapon
{
public abstract string Name { get; set; }
}
// A sword is a concrete weapon
// The base class "Weapon" forces it to have a name
public class Sword : Weapon
{
public override string Name { get; set; } = "sword";
}
// our weaponshop with a list of available weapons
public class WeaponShop
{
public List<Weapon> Weapons { get; } = new List<Weapon>();
// Let's add a sword to our weapon list when we are created
public WeaponShop()
{
Weapons.Add(new Sword());
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
WeaponShop myWeaponShop = new WeaponShop();
Console.WriteLine("The first sword is: " + myWeaponShop.Weapons.OfType<Sword>().FirstOrDefault().Name);
}
}
Upvotes: 0