英文:
Accessing a Dictionary from a Class other than the Main() Class in C#
问题
I've just started working with C# in January and am working on a simple game for a project. I have the Program.cs class which contains the Main() class, player.cs which contains the majority of actions that a player can take, and building.cs which holds the building a player can purchase.
building.cs
namespace Game
{
public class building
{
public int price { get; }
public bool owned { get; set; }
public string? name { get; }
public int owner { get; set; }
public building(string name, int price, bool owned, int owner)
{
this.name = name;
this.price = price;
this.owned = owned;
this.owner = owner;
}
}
public class buildingDict
{
public static Dictionary<string, building> InitializeBuildings()
{
building wheatfield = new building("wheatfield", 4, false, 1);
var buildings = new Dictionary<string, building>();
buildings.Add("wheatfield", wheatfield);
return buildings;
}
}
}
Now, I would like to be able to access this from my player.cs class, but it doesn't seem possible.
I can access it from Program.cs Main()
namespace Game
{
static void Main()
{
var ownedbuildings = buildingDict.InitializeBuildings();
Console.WriteLine($"There are {ownedbuildings.Count} buildings for sale");
Console.WriteLine($"{ownedbuildings["wheatfield"].name} is sold at ${ownedbuildings["wheatfield"].price}");
}
}
Which returns
There are 1 buildings for sale
wheatfield is sold at $4
But when I attempt to access it from Player.cs I get errors and I'm not sure why or if it is even possible to access a Dictionary from a separate class that isn't Main().
namespace Game
{
public class player
{
var OwnedBuildings = buildingDict.InitializeBuildings();
public void purchasebuilding()
{
//do something with the building dictionary.
}
}
}
The error I get is on the var
keyword which states, "The contextual keyword 'var' may only appear with a local variable declaration or in script code".
I would like to keep all the logic for what players can do to the player class solely and leave the mechanics of the game for the Main().
英文:
I've just started working with C# in January and am working on a simple game for a project. I have the Program.cs class which contains the Main() class, player.cs which contains the majority of actions that a player can take, and building.cs which holds the building a player can purchase.
building.cs
namespace Game
{
public class building
{
public int price { get; }
public bool owned { get; set; }
public string? name { get; }
public int owner { get; set; }
public building(string name, int price, bool owned, int owner)
{
this.name = name;
this.price = price;
this.owned = owned;
this.owner = owner;
}
}
public class buildingDict
{
public static Dictionary<string, building> InitializeBuildings()
{
building wheatfield = new building("wheatfield", 4, false, 1);
var buildings = new Dictionary<string, building>();
buildings.add("wheatfield", wheatfield);
return buildings;
}
}
}
Now, I would like to be able to access this from my player.cs class, but it doesn't seem possible.
I can access it from Progam.cs Main()
namespace Game
{
static void Main()
{
var ownedbuildings = buildingDict.InitializeBuildings();
Console.WriteLine($"There are {ownedbuildings.Count} buildings for sale");
Console.WriteLine($"{ownedbuildings["wheatfield"].name} is sold at ${ownedbuildings["wheatfield"].price}");
}
}
Which returns
There are 1 buildings for sale
wheatfield is sold at $4
But when I attempt to access it from Player.cs I get errors and I'm not sure why or if it is even possible to access a Dictionary from a separate class that isn't Main().
namespace Game
{
public class player
{
var OwnedBuildings = buildingDict.InitializeBuildings();
public void purchasebuilding()
{
\\do something with the building dictionary.
}
}
}
The error I get is on the var
keyword which states, "The contextual keyword 'var' may only appear with a local variable declaration or in script code".
I would like to keep all the logic for what players can do to the player class solely and leave the mechanics of the game for the Main().
答案1
得分: 1
你应该为玩家添加一个类构造函数(就像你为建筑类所做的那样),接受一个字典对象,而不是尝试引用外部变量作为类属性。
然后,你可以这样做:
static void Main()
{
var ownedbuildings = buildingDict.InitializeBuildings();
var p = new player(ownedbuildings);
}
这可能不是最佳的设计,但它允许你在类之间传递引用。
英文:
You should add a class constructor (like you did for building class) for the player that accepts just a dictionary object rather than trying to reference an external variable as a class property.
Then you could do
static void Main()
{
var ownedbuildings = buildingDict.InitializeBuildings();
var p = new player(ownedbuildings);
This may not be the best design, but it allows you to pass references between classes.
答案2
得分: 1
如果意图是使每个 player
拥有他们自己的、独立的建筑物集合,但具有相同的默认初始设置,那么使用以下代码:
public class player
{
private Dictionary<string, building> OwnedBuildings = buildingDict.InitializeBuildings();
public void purchasebuilding()
{
// 使用建筑物字典执行某些操作。
}
}
英文:
If the intention is for each player
to have their OWN, SEPARATE collection of buildings, but with the same default, initial setup, then use:
public class player
{
private Dictionary<string, building> OwnedBuildings = buildingDict.InitializeBuildings();
public void purchasebuilding()
{
//do something with the building dictionary.
}
}
答案3
得分: 1
你不能在类的内部,即类作用域内,仅仅使用 "var" 关键字,就像你不能在类的内部放置执行操作的任何代码一样。这只能在构造函数、方法或块作用域中完成。 "Main" 不是一个类,它是 Program 类内的一个方法。
在你的情况下,通常使用 "private" 而不是 var,而且通常与 "readonly" 关键字一起使用。
public class player
{
private readonly Dictionary<string, building> _ownedbuildings = buildingDict.InitializeBuildings();
public void purchasebuilding()
{
Console.WriteLine($"There are {_ownedbuildings.Count} buildings for sale");
Console.WriteLine($"{_ownedbuildings["wheatfield"].name} is sold at ${_ownedbuildings["wheatfield"].price}");
}
}
但以下代码也可以正常工作:
public class player
{
public void purchasebuilding()
{
var _ownedbuildings = buildingDict.InitializeBuildings();
Console.WriteLine($"There are {_ownedbuildings.Count} buildings for sale");
Console.WriteLine($"{_ownedbuildings["wheatfield"].name} is sold at ${_ownedbuildings["wheatfield"].price}");
}
}
英文:
You can not use "var" just inside of a class, in a class scope. The same way as you can not put any code that is doing something inside of a class too. It can be only done in a constructor, method or block scope. "Main" is not a class, it is a method inside of the Program class.
In your case usually used "private" instead of var, also it is usually used with readonly word
public class player
{
private readonly Dictionary<string, building> _ownedbuildings = buildingDict.InitializeBuildings();
public void purchasebuilding()
{
Console.WriteLine($"There are {_ownedbuildings.Count} buildings for sale");
Console.WriteLine($"{_ownedbuildings["wheatfield"].name} is sold at ${_ownedbuildings["wheatfield"].price}");
}
}
but this will be working
public class player
{
public void purchasebuilding()
{
var _ownedbuildings = buildingDict.InitializeBuildings();
Console.WriteLine($"There are {_ownedbuildings.Count} buildings for sale");
Console.WriteLine($"{_ownedbuildings["wheatfield"].name} is sold at ${_ownedbuildings["wheatfield"].price}");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论