从C#中的主类以外的类访问字典

huangapple go评论95阅读模式
英文:

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

  1. namespace Game
  2. {
  3. public class building
  4. {
  5. public int price { get; }
  6. public bool owned { get; set; }
  7. public string? name { get; }
  8. public int owner { get; set; }
  9. public building(string name, int price, bool owned, int owner)
  10. {
  11. this.name = name;
  12. this.price = price;
  13. this.owned = owned;
  14. this.owner = owner;
  15. }
  16. }
  17. public class buildingDict
  18. {
  19. public static Dictionary<string, building> InitializeBuildings()
  20. {
  21. building wheatfield = new building("wheatfield", 4, false, 1);
  22. var buildings = new Dictionary<string, building>();
  23. buildings.Add("wheatfield", wheatfield);
  24. return buildings;
  25. }
  26. }
  27. }

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()

  1. namespace Game
  2. {
  3. static void Main()
  4. {
  5. var ownedbuildings = buildingDict.InitializeBuildings();
  6. Console.WriteLine($"There are {ownedbuildings.Count} buildings for sale");
  7. Console.WriteLine($"{ownedbuildings["wheatfield"].name} is sold at ${ownedbuildings["wheatfield"].price}");
  8. }
  9. }

Which returns

  1. There are 1 buildings for sale
  2. 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().

  1. namespace Game
  2. {
  3. public class player
  4. {
  5. var OwnedBuildings = buildingDict.InitializeBuildings();
  6. public void purchasebuilding()
  7. {
  8. //do something with the building dictionary.
  9. }
  10. }
  11. }

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

  1. namespace Game
  2. {
  3. public class building
  4. {
  5. public int price { get; }
  6. public bool owned { get; set; }
  7. public string? name { get; }
  8. public int owner { get; set; }
  9. public building(string name, int price, bool owned, int owner)
  10. {
  11. this.name = name;
  12. this.price = price;
  13. this.owned = owned;
  14. this.owner = owner;
  15. }
  16. }
  17. public class buildingDict
  18. {
  19. public static Dictionary&lt;string, building&gt; InitializeBuildings()
  20. {
  21. building wheatfield = new building(&quot;wheatfield&quot;, 4, false, 1);
  22. var buildings = new Dictionary&lt;string, building&gt;();
  23. buildings.add(&quot;wheatfield&quot;, wheatfield);
  24. return buildings;
  25. }
  26. }
  27. }

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()

  1. namespace Game
  2. {
  3. static void Main()
  4. {
  5. var ownedbuildings = buildingDict.InitializeBuildings();
  6. Console.WriteLine($&quot;There are {ownedbuildings.Count} buildings for sale&quot;);
  7. Console.WriteLine($&quot;{ownedbuildings[&quot;wheatfield&quot;].name} is sold at ${ownedbuildings[&quot;wheatfield&quot;].price}&quot;);
  8. }
  9. }

Which returns

  1. There are 1 buildings for sale
  2. 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().

  1. namespace Game
  2. {
  3. public class player
  4. {
  5. var OwnedBuildings = buildingDict.InitializeBuildings();
  6. public void purchasebuilding()
  7. {
  8. \\do something with the building dictionary.
  9. }
  10. }
  11. }

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

你应该为玩家添加一个类构造函数(就像你为建筑类所做的那样),接受一个字典对象,而不是尝试引用外部变量作为类属性。

然后,你可以这样做:

  1. static void Main()
  2. {
  3. var ownedbuildings = buildingDict.InitializeBuildings();
  4. var p = new player(ownedbuildings);
  5. }

这可能不是最佳的设计,但它允许你在类之间传递引用。

英文:

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

  1. static void Main()
  2. {
  3. var ownedbuildings = buildingDict.InitializeBuildings();
  4. var p = new player(ownedbuildings);

This may not be the best design, but it allows you to pass references between classes.

答案2

得分: 1

如果意图是使每个 player 拥有他们自己的、独立的建筑物集合,但具有相同的默认初始设置,那么使用以下代码:

  1. public class player
  2. {
  3. private Dictionary<string, building> OwnedBuildings = buildingDict.InitializeBuildings();
  4. public void purchasebuilding()
  5. {
  6. // 使用建筑物字典执行某些操作。
  7. }
  8. }
英文:

If the intention is for each player to have their OWN, SEPARATE collection of buildings, but with the same default, initial setup, then use:

  1. public class player
  2. {
  3. private Dictionary&lt;string, building&gt; OwnedBuildings = buildingDict.InitializeBuildings();
  4. public void purchasebuilding()
  5. {
  6. //do something with the building dictionary.
  7. }
  8. }

答案3

得分: 1

你不能在类的内部,即类作用域内,仅仅使用 "var" 关键字,就像你不能在类的内部放置执行操作的任何代码一样。这只能在构造函数、方法或块作用域中完成。 "Main" 不是一个类,它是 Program 类内的一个方法。

在你的情况下,通常使用 "private" 而不是 var,而且通常与 "readonly" 关键字一起使用。

  1. public class player
  2. {
  3. private readonly Dictionary<string, building> _ownedbuildings = buildingDict.InitializeBuildings();
  4. public void purchasebuilding()
  5. {
  6. Console.WriteLine($"There are {_ownedbuildings.Count} buildings for sale");
  7. Console.WriteLine($"{_ownedbuildings["wheatfield"].name} is sold at ${_ownedbuildings["wheatfield"].price}");
  8. }
  9. }

但以下代码也可以正常工作:

  1. public class player
  2. {
  3. public void purchasebuilding()
  4. {
  5. var _ownedbuildings = buildingDict.InitializeBuildings();
  6. Console.WriteLine($"There are {_ownedbuildings.Count} buildings for sale");
  7. Console.WriteLine($"{_ownedbuildings["wheatfield"].name} is sold at ${_ownedbuildings["wheatfield"].price}");
  8. }
  9. }
英文:

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

  1. public class player
  2. {
  3. private readonly Dictionary&lt;string, building&gt; _ownedbuildings = buildingDict.InitializeBuildings();
  4. public void purchasebuilding()
  5. {
  6. Console.WriteLine($&quot;There are {_ownedbuildings.Count} buildings for sale&quot;);
  7. Console.WriteLine($&quot;{_ownedbuildings[&quot;wheatfield&quot;].name} is sold at ${_ownedbuildings[&quot;wheatfield&quot;].price}&quot;);
  8. }
  9. }

but this will be working

  1. public class player
  2. {
  3. public void purchasebuilding()
  4. {
  5. var _ownedbuildings = buildingDict.InitializeBuildings();
  6. Console.WriteLine($&quot;There are {_ownedbuildings.Count} buildings for sale&quot;);
  7. Console.WriteLine($&quot;{_ownedbuildings[&quot;wheatfield&quot;].name} is sold at ${_ownedbuildings[&quot;wheatfield&quot;].price}&quot;);
  8. }
  9. }

huangapple
  • 本文由 发表于 2023年2月24日 08:40:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551655.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定