'GamePlayers.GameAndPlayers.Game' does not contain a definition for 'ReferenceEqualsnumberPlayers'
using
System;namespace
GamePlayers{
public class GameAndPlayers{
public static void Main(){
string enteredGame; string enteredPlayers;Game aGame =
new Game();Console.Write("Enter name of the game ");
enteredGame = Console.ReadLine();
Console.Write("Enter the number of players for the game ");
enteredPlayers = Console.ReadLine();
numberPlayers = Convert.ToInt32(enteredPlayers);
Console.WriteLine("The total number of players for the game {0} is {1}",
aGame.GetGameName(),aGame.GetNumPlayers());
}
public class Game{
public int numberPlayers; public string gameName; public int GetNumPlayers(){
return numberPlayers;}
public string GetGameName(){
return gameName;}
public void SetNumPlayers(int num){
numberPlayers = num;
}
public void SetGameName(string name){
gameName = name;
}
}
}
}
Scott LyslePosted May 6, 2007, 3:46 AM
You did not declare the private member variable and you had a semi-colon after the property declaration, here is a revision:
public class time
{
// private member variable
private int mTimeLimit;
// public property declaration (note name is different with 'm' removed)
public int TimeLimit
{
get
{
return mTimeLimit;
}
set
{
mTimeLimit = value;
}
}
}
herbiePosted May 5, 2007, 9:30 AM
using
System;namespace
GamePlayers{
public class GameAndPlayers{
public static void Main(){
Game aGame =
new Game();Console.Write("Enter name of the game: ");
aGame.GameName = Console.ReadLine();
Console.Write("Enter the number of players for the game: ");
aGame.NumPlayers = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the time limit: ");
aTime.TimeLimit = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
Console.WriteLine("The total number of players for the game {0} is {1}.",
aGame.GameName, aGame.NumPlayers);
}
public class Game{
private int mNumPlayers; private string mGameName; public int NumPlayers{
get{
return mNumPlayers;}
set{
mNumPlayers =
value;}
}
public string GameName{
get{
return mGameName;}
set{
mGameName =
value;}
}
public class time{
public int mTimeLimit;{
get{
return mTimeLimit;}
set{
mTimeLimit =
value;}
}
}
herbiePosted May 5, 2007, 8:47 AM
Scott LyslePosted May 5, 2007, 4:40 AM
You are not assigning the values to the 'game' object; you are assigning them to two variables (a string and an int); since the values are never assigned to the object, when you try to reference them, they do not exist and you get the error.
You have also, in the Game class, created to public variables with public accessor methods that are not properties; typically one would create two private member variables and then create two public properties to provide read and/or write access to the private member variables. If you declare the member variables a public, there is not much of a need to provide a public accessor method to a public variable.
Here is a quick example of a simple rewrite:
using
System;using System.Text;
namespace
herbie{ public class Program
{ static void Main(string[] args)
{ Game aGame = new Game(); Console.WriteLine("Enter name of the game: ");
aGame.GameName = Console.ReadLine(); Console.Write("\nEnter the number of players for the game: \n");
aGame.NumPlayers = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("\nThe total number of players for the game {0} is {1}.",
aGame.GameName, aGame.NumPlayers); Console.WriteLine("\n\nPress any key to continue");
Console.ReadLine();
}
} public class Game
{ private int mNumPlayers;
private string mGameName; public int NumPlayers
{
get
{
return mNumPlayers;
}
set
{
mNumPlayers = value;
}
} public string GameName
{
get
{
return mGameName;
}
set
{
mGameName = value;
}
}
}
}