I have a classes, Initialize
and a file Objects
with a player
class and constructor. The initialize
class is as follows:
using System;
namespace ConsoleTagGame{
public class initialize{
public static void Main(string[] args)
{
Objects.Player player1 = new Objects.Player("test player", false, false);
Console.WriteLine(player1.name);
}
}
}
and the objects
file is:
using System;
namespace ConsoleTagGame/*.Objects*/{
// Actual player object
public class Player{
// player properties
string Name { get; set; }
bool IsAlive { get; set; }
bool IsIt { get; set; }
// constructor
public Player(string name, bool isAlive, bool isIt){
this.Name = name;
this.IsAlive = isAlive;
this.IsIt = isIt;
}
}
}
(Note that I have tried removing Object.
from declaring the variable, still gives essentially same results)
Whenever I try and run it, I get the error The type or namespace name "Objects" could not be found. Are you missing an assembly reference?
The type or namespace name "Objects" could not be found. Are you missing an assembly reference?
(CS0246 for reference). Im not sure if this is because im not running an IDE and am just using a text editor & terminal, or something else.
The first detail is to note that when a property is used by another class that does not directly inherit, it is necessary to use the "public" directive.
namespace ConsoleTagGame.Objects
{
Actual player object
public class Player
{
// player properties
public string Name { get; set; }
public bool IsAlive { get; set; }
public bool IsIt { get; set; }
// constructor
public Player(string name, bool isAlive, bool isIt)
{
this.Name = name;
this.IsAlive = isAlive;
this.IsIt = isIt;
}
}
}
Another point is that in the initialize class you are referencing the Player class by the path Objects.Player, whereas in the Player class its namespace is ConsoleTagGame instead of ConsoleTagGame.Objects, as in the example above.
Finally the initialize class would look like this:
using ConsoleTagGame.Objects;
using System;
namespace ConsoleTagGame
{
public class initialize
{
public static void Main(string[] args)
{
Player player1 = new Player("test player", false, false);
Console.WriteLine(player1.Name);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.