 |
| ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

September 4th, 2011, 08:25 PM
|
|
Authorized User
|
|
Join Date: Oct 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Class scope in code-behinds
Hi,
If I write a standalone class (ie resides in the App_Code folder) where do I create an instance of it in the code-behind so that it is accessible from every 'routine/procedure/ in the code-behind?
Here is a simplified version of what I've done:
ListedGame.cs (App_Code)
public class ListedGame
{
private string gameStatus;
public string GameStatus
{
get
{
return gameStatus;
}
set
{
GameStatus = value;
}
}
}
WaitingList.aspx.cs
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListedGame lg = new ListedGame();
lg.GameStatus = XXX;
I want to access this field in another location on the page:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (lg.GameStatus == "Live")
{
// e.Row.Cells[5].Visible = false;
}
}
I get the red squiggly and 'the name lg does exist in this context' as error.
I presume this is a question of scope.
Regards, Guy
|
|

September 5th, 2011, 03:19 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
The object you're declaring is only available within the method where you declare it. If you need to access it from multiple methods, you need to declare it at the class level (e.g. within "class WaitingList" but outside of any methods. This creates a class based variable that you can now access from both methods.
Cheers,
Imar
|
|

September 5th, 2011, 06:02 AM
|
|
Authorized User
|
|
Join Date: Oct 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
I wasn't sure about the correct way to declare it at class level.
Tried this: (outside all methods)
public ListedGame lg = new ListedGame();
squiggly vanished!
Ran page:
Error appeared in external class definition:
StackOverflow...make sure you do not have an infinite loop..
I fear I have approached this in the wrong way.....
regards, Guy
Last edited by guynorton; September 5th, 2011 at 06:06 AM..
|
|

September 5th, 2011, 06:11 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Can you post the full code for your page and the ListedGame class? I can't see a reason for a Stack Overflow exception in your current code.
Also, what is it you're using ListedGame for? Is this also the data item you're binding to the list view? It feels a bit odd to use a single class based variable to check items in the ItemDataBound and RowCreated events as these events fire multiple times.
If you can elaborate on what you're trying to accomplish, I may come up with a better implementation.
Cheers,
Imar
|
|

September 5th, 2011, 08:52 AM
|
|
Authorized User
|
|
Join Date: Oct 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
Here's all the relevant code:
I'm using a listview with embedded gridviews. The listview displays general game data. The gridview shows those players who are on the waiting list for each game. The class will have many more properties some of which will be calculated on the fly (int NumberOfSeatsAvailable, bool GuaranteedSeat, bool SeatIfNoMoreShows) which will be used to determine which rows, buttons are shown, and the formatting of these items. (The code I use to supply the value to the childGridView can be simplified if I rewrite it to supply the Id value from this class..) This seems ample justification for me to use my first proper class, and get into some good programming habits!
ListedGame.cs
Code:
using System;
using System.Collections.Generic;
using System.Web;
public class ListedGame
{
private string gameStatus;
public string GameStatus
{
get
{
return gameStatus;
}
set
{
GameStatus = value;
}
}
}
Brush.aspx.cs (This is the page's actual name. Brush is the name given to a poker pit supervisor)
Code:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class Brush : System.Web.UI.Page
{
public ListedGame lg = new ListedGame();
protected void Page_Load(object sender, EventArgs e)
{
// code
}
protected void InsertNewPlayer_Click(object sender, EventArgs e)
{
// code
}
// Insert Gridview for each Game type offered //
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
GridView gridChild = e.Item.FindControl("GridPlayers") as GridView;
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
DataRowView rowView = (DataRowView)dataItem.DataItem;
string AanbodId = rowView["Id"].ToString();
lg.GameStatus = rowView["Status"].ToString();
sqlPlayersOnList.SelectParameters[0].DefaultValue = AanbodId;
object data = sqlPlayersOnList.Select(DataSourceSelectArguments.Empty);
gridChild.DataSource = data;
gridChild.DataBind();
}
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (lg.GameStatus == "Live")
{
e.Row.Cells[5].Visible = false;
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// code
}
// Set registree physical status
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
// code
}
protected void UpdateExistingPlayer_Click(object sender, EventArgs e)
{
// code
}
regards, Guy
Last edited by guynorton; September 5th, 2011 at 08:56 AM..
|
|

September 5th, 2011, 09:02 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
I don't see a reason for a StackOverflow exception except for maybe this:
Code:
sqlPlayersOnList.SelectParameters[0].DefaultValue = catId;
object data = sqlPlayersOnList.Select(DataSourceSelectArguments.Empty);
Is sqlPlayersOnList used to fill the ListView as well? If so, this might cause an endless loop with the exception as the result.
Cheers,
Imar
|
|

September 5th, 2011, 10:05 AM
|
|
Authorized User
|
|
Join Date: Oct 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
sqlPlayersOnList is the name of the SqlDataSource used by the nested gridview. Before the introduction of the class it worked perfectly.
I commented out
lg.GameStatus = rowView["Status"].ToString();
and inserted the following line into the InsertPlayer method:
lg.GameStatus = "value";
I get exactly the same error!
An unhandled exception of type 'System.StackOverflowException' occurred in App_Code.t3htr-cv.dll
I have no idea what's going on and I can't think of any relevant details I may have omitted in my reporting back to you....
regards, Guy
|
|

September 5th, 2011, 11:36 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Oh, I see now. The joy of case-sensitive languages.... Take a look at this:
Code:
public class ListedGame
{
private string gameStatus;
public string GameStatus
{
get
{
return gameStatus;
}
set
{
GameStatus = value;
}
}
}
Notice how the setter assigns the value to GameStatus with a capital G which is the public property's name. This in turn causes the setter to be called, causing an endless loop and eventually a StackOverflowException. Change GameStatus = value to gameStatus = value and you should be OK.
This is also why I like prefixing local variables with an underscore. Makes these kind of issues easier to spot. Or better yet: use an auto implemented property:
Code:
public string GameStatus { get; set; }
Hope this helps,
Imar
|
|

September 5th, 2011, 12:24 PM
|
|
Authorized User
|
|
Join Date: Oct 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks Imar,
I had come to the conclusion that it was something painfully simple and lo and behold.....
I could have saved us all a lot of trouble if I'd used the debugging capabilities of VWD. I just forced myself to witness the first dozen iterations of an infinite loop, each iteration equivalent to a psychological slap in the face!
With regards to variable naming conventions in classes. I decided to use the cap/lowercase approach as this seemed to be the most widely adopted approach. I'm going for the underscore in the future!
Thanks and regards, Guy
|
|

September 5th, 2011, 12:47 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
each iteration equivalent to a psychological slap in the face!
|
Don't get yourself hurt.... ;-)
Quote:
|
I decided to use the cap/lowercase approach as this seemed to be the most widely adopted approach.
|
True, but I think the undercore way is just as common. Comes down mostly to personal preference.... You can also take a good look at IntelliSense which shows you different icons for different member types.
Cheers,
Imar
|
|
 |