Error: Not all code paths return a value - how do I fix it?

O

Oberon

How do I deal with this? I am getting an error for each get in the
Game class (see code below).

In the simplified example below I have reduced this to just 3 fields,
one which can be NULL. I have added 3 records to the table and ran the
program but it fails with the error above.

The application is supposed to create the hashtable of records as a
static feature which will be permanently available to my application.
To demonstrate that the HashTable is actually there I display the
contents in an ASP.NET page called Games.

When I run my application I get the error below:

= = = = = = = = = =
C:\Inetpub\wwwroot\ChatMark1\Game.cs(40):
'ChatMark1.Game.GameID.get': not all code paths return a value

= = = = = = = = = =
public class Global : System.Web.HttpApplication
{

public static string gameConnection = "packet size=4096;user
id=sa;pwd=monkey;data source=ASROCK;persist security
info=False;initial catalog=ChatMark1";
public static GameHashTable games;
private System.ComponentModel.IContainer components = null;

public Global()
{
InitializeComponent();
}

protected void Application_Start(Object sender, EventArgs e)
{
Global.GameLoad();
}

public static string GameConnection
{
get
{
return gameConnection;
}
}

public static void GameLoad()
{
SqlConnection connGames = new SqlConnection(gameConnection);
connGames.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM Games",
connGames);
SqlDataReader reader;
reader = cmd.ExecuteReader();

Game game;
games = new GameHashTable();
while (reader.Read())
{
game = new Game((int)reader[0], (DateTime)reader[1],
(string)reader[2]);
games.Add(game);
}
connGames.Close();
}

public static GameHashTable Games
{
get
{
return games;
}
}

}


= = = = = = = = = =
// Games.aspx.cs.

public class Games : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgGames;

private void Page_Load(object sender, System.EventArgs e)
{
dgGames.DataSource = Global.Games;
dgGames.DataBind();
}
}



= = = = = = = = = =
public class GameHashTable : IEnumerable
{
Hashtable _games;

public GameHashTable()
{
// Contructor
_games = new Hashtable();
}
public IEnumerator GetEnumerator()
{
return _games.Values.GetEnumerator();
}
public Game this[int GameID]
{
get {return (Game) _games[GameID];}
set {Add(value);}
}
public void Add(Game Item)
{
if (Item == null)
throw new ArgumentException("Game can not be null");
_games.Add(Item.GameID, Item);

}
public void Remove(Game Item)
{
_games.Remove(Item.GameID);
}
}


= = = = = = = = = =

public class Game
{
private int _gameID;
private DateTime _playTime;
private string _image;

public Game(
int initialGameID,
DateTime initialPlayTime,
string initialImage )
{
GameID = initialGameID;
PlayTime = initialPlayTime;
Image = initialImage;
}
public int GameID
{
get {GameID = _gameID;}
set {_gameID = value;}
}
public DateTime PlayTime
{
get {PlayTime = _playTime;}
set {_playTime = value;}
}
public string Image
{
get {Image = _image;}
set {_image = value;}
}

}

= = = = = = = = = =

My Games Table in the ChatMark1 database.

CREATE TABLE Games (
gameID int IDENTITY (1, 1) NOT NULL ,
playTime datetime NOT NULL DEFAULT (getdate()),
image varchar (50) NULL ,
CONSTRAINT PK_Games PRIMARY KEY CLUSTERED (gameID) ON PRIMARY
) ON PRIMARY
 
A

Andy Walldorff

Oberon said:
How do I deal with this? I am getting an error for each get in the
Game class (see code below).

In the simplified example below I have reduced this to just 3 fields,
one which can be NULL. I have added 3 records to the table and ran the
program but it fails with the error above.

The application is supposed to create the hashtable of records as a
static feature which will be permanently available to my application.
To demonstrate that the HashTable is actually there I display the
contents in an ASP.NET page called Games.

When I run my application I get the error below:

= = = = = = = = = =
C:\Inetpub\wwwroot\ChatMark1\Game.cs(40):
'ChatMark1.Game.GameID.get': not all code paths return a value

= = = = = = = = = =
public class Global : System.Web.HttpApplication
{

public static string gameConnection = "packet size=4096;user
id=sa;pwd=monkey;data source=ASROCK;persist security
info=False;initial catalog=ChatMark1";
public static GameHashTable games;
private System.ComponentModel.IContainer components = null;

public Global()
{
InitializeComponent();
}

protected void Application_Start(Object sender, EventArgs e)
{
Global.GameLoad();
}

public static string GameConnection
{
get
{
return gameConnection;
}
}

public static void GameLoad()
{
SqlConnection connGames = new SqlConnection(gameConnection);
connGames.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM Games",
connGames);
SqlDataReader reader;
reader = cmd.ExecuteReader();

Game game;
games = new GameHashTable();
while (reader.Read())
{
game = new Game((int)reader[0], (DateTime)reader[1],
(string)reader[2]);
games.Add(game);
}
connGames.Close();
}

public static GameHashTable Games
{
get
{
return games;
}
}

}


= = = = = = = = = =
// Games.aspx.cs.

public class Games : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgGames;

private void Page_Load(object sender, System.EventArgs e)
{
dgGames.DataSource = Global.Games;
dgGames.DataBind();
}
}



= = = = = = = = = =
public class GameHashTable : IEnumerable
{
Hashtable _games;

public GameHashTable()
{
// Contructor
_games = new Hashtable();
}
public IEnumerator GetEnumerator()
{
return _games.Values.GetEnumerator();
}
public Game this[int GameID]
{
get {return (Game) _games[GameID];}
set {Add(value);}
}
public void Add(Game Item)
{
if (Item == null)
throw new ArgumentException("Game can not be null");
_games.Add(Item.GameID, Item);

}
public void Remove(Game Item)
{
_games.Remove(Item.GameID);
}
}


= = = = = = = = = =

public class Game
{
private int _gameID;
private DateTime _playTime;
private string _image;

public Game(
int initialGameID,
DateTime initialPlayTime,
string initialImage )
{
GameID = initialGameID;
PlayTime = initialPlayTime;
Image = initialImage;
}
public int GameID
{
get {GameID = _gameID;}
set {_gameID = value;}
}
public DateTime PlayTime
{
get {PlayTime = _playTime;}
set {_playTime = value;}
}
public string Image
{
get {Image = _image;}
set {_image = value;}
}

}

= = = = = = = = = =

My Games Table in the ChatMark1 database.

CREATE TABLE Games (
gameID int IDENTITY (1, 1) NOT NULL ,
playTime datetime NOT NULL DEFAULT (getdate()),
image varchar (50) NULL ,
CONSTRAINT PK_Games PRIMARY KEY CLUSTERED (gameID) ON PRIMARY
) ON PRIMARY

The best place to start is exactly where it says the problem is:
C:\Inetpub\wwwroot\ChatMark1\Game.cs on line 40. Also, from within the IDE
you can double click on the error and it will take you there. However, the
bottom line is that you need to return a value from your get'ers, for
example:

public int GameID
{
get {return _gameID;}
set {_gameID = value;}
}
 
J

Joanna Carter \(TeamB\)

"Oberon" <[email protected]> a écrit dans le message de (e-mail address removed)...

How do I deal with this? I am getting an error for each get in the
Game class (see code below).
public class Game
{
private int _gameID;
private DateTime _playTime;
private string _image;

public Game(
int initialGameID,
DateTime initialPlayTime,
string initialImage )
{
GameID = initialGameID;
PlayTime = initialPlayTime;
Image = initialImage;
}
public int GameID
{
get {GameID = _gameID;}
set {_gameID = value;}
}
public DateTime PlayTime
{
get {PlayTime = _playTime;}
set {_playTime = value;}
}
public string Image
{
get {Image = _image;}
set {_image = value;}
}

}

You have declared you properties incorrectly; for some reason you ar trying
to set the property in the getter !!!

public int GameID
{
get {return _gameID;}
set {_gameID = value;}
}

public DateTime PlayTime
{
get {return _playTime;}
set {_playTime = value;}
}

public string Image
{
get {return _image;}
set {_image = value;}
}

Joanna
 
O

Oberon

Oh dear,

That's just the kind of error beginners make isn't it?

public int GameID
{
get {GameID = _gameID;}
set {_gameID = value;}
}

In my mind I thought that 'GameID = _gameID' would set the property to
the value of the hidden field. Silly me.

Thanks.


Oberon said:
How do I deal with this? I am getting an error for each get in the
Game class (see code below).

In the simplified example below I have reduced this to just 3 fields,
one which can be NULL. I have added 3 records to the table and ran the
program but it fails with the error above.

The application is supposed to create the hashtable of records as a
static feature which will be permanently available to my application.
To demonstrate that the HashTable is actually there I display the
contents in an ASP.NET page called Games.

When I run my application I get the error below:

= = = = = = = = = =
C:\Inetpub\wwwroot\ChatMark1\Game.cs(40):
'ChatMark1.Game.GameID.get': not all code paths return a value

= = = = = = = = = =
public class Global : System.Web.HttpApplication
{

public static string gameConnection = "packet size=4096;user
id=sa;pwd=monkey;data source=ASROCK;persist security
info=False;initial catalog=ChatMark1";
public static GameHashTable games;
private System.ComponentModel.IContainer components = null;

public Global()
{
InitializeComponent();
}

protected void Application_Start(Object sender, EventArgs e)
{
Global.GameLoad();
}

public static string GameConnection
{
get
{
return gameConnection;
}
}

public static void GameLoad()
{
SqlConnection connGames = new SqlConnection(gameConnection);
connGames.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM Games",
connGames);
SqlDataReader reader;
reader = cmd.ExecuteReader();

Game game;
games = new GameHashTable();
while (reader.Read())
{
game = new Game((int)reader[0], (DateTime)reader[1],
(string)reader[2]);
games.Add(game);
}
connGames.Close();
}

public static GameHashTable Games
{
get
{
return games;
}
}

}


= = = = = = = = = =
// Games.aspx.cs.

public class Games : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgGames;

private void Page_Load(object sender, System.EventArgs e)
{
dgGames.DataSource = Global.Games;
dgGames.DataBind();
}
}



= = = = = = = = = =
public class GameHashTable : IEnumerable
{
Hashtable _games;

public GameHashTable()
{
// Contructor
_games = new Hashtable();
}
public IEnumerator GetEnumerator()
{
return _games.Values.GetEnumerator();
}
public Game this[int GameID]
{
get {return (Game) _games[GameID];}
set {Add(value);}
}
public void Add(Game Item)
{
if (Item == null)
throw new ArgumentException("Game can not be null");
_games.Add(Item.GameID, Item);

}
public void Remove(Game Item)
{
_games.Remove(Item.GameID);
}
}


= = = = = = = = = =

public class Game
{
private int _gameID;
private DateTime _playTime;
private string _image;

public Game(
int initialGameID,
DateTime initialPlayTime,
string initialImage )
{
GameID = initialGameID;
PlayTime = initialPlayTime;
Image = initialImage;
}
public int GameID
{
get {GameID = _gameID;}
set {_gameID = value;}
}
public DateTime PlayTime
{
get {PlayTime = _playTime;}
set {_playTime = value;}
}
public string Image
{
get {Image = _image;}
set {_image = value;}
}

}

= = = = = = = = = =

My Games Table in the ChatMark1 database.

CREATE TABLE Games (
gameID int IDENTITY (1, 1) NOT NULL ,
playTime datetime NOT NULL DEFAULT (getdate()),
image varchar (50) NULL ,
CONSTRAINT PK_Games PRIMARY KEY CLUSTERED (gameID) ON PRIMARY
) ON PRIMARY

The best place to start is exactly where it says the problem is:
C:\Inetpub\wwwroot\ChatMark1\Game.cs on line 40. Also, from within the IDE
you can double click on the error and it will take you there. However, the
bottom line is that you need to return a value from your get'ers, for
example:

public int GameID
{
get {return _gameID;}
set {_gameID = value;}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top