Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 4th, 2011, 08:25 PM
Authorized User
Points: 160, Level: 3
Points: 160, Level: 3 Points: 160, Level: 3 Points: 160, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2004
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
  #2 (permalink)  
Old September 5th, 2011, 03:19 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #3 (permalink)  
Old September 5th, 2011, 06:02 AM
Authorized User
Points: 160, Level: 3
Points: 160, Level: 3 Points: 160, Level: 3 Points: 160, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2004
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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..
  #4 (permalink)  
Old September 5th, 2011, 06:11 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #5 (permalink)  
Old September 5th, 2011, 08:52 AM
Authorized User
Points: 160, Level: 3
Points: 160, Level: 3 Points: 160, Level: 3 Points: 160, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2004
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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..
  #6 (permalink)  
Old September 5th, 2011, 09:02 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #7 (permalink)  
Old September 5th, 2011, 10:05 AM
Authorized User
Points: 160, Level: 3
Points: 160, Level: 3 Points: 160, Level: 3 Points: 160, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2004
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
  #8 (permalink)  
Old September 5th, 2011, 11:36 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #9 (permalink)  
Old September 5th, 2011, 12:24 PM
Authorized User
Points: 160, Level: 3
Points: 160, Level: 3 Points: 160, Level: 3 Points: 160, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2004
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
  #10 (permalink)  
Old September 5th, 2011, 12:47 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use a simple DBML or this code (base class)? eramgarden LINQ 1 March 9th, 2012 11:52 AM
full source code for the class AntiXssEncoder plagoon BOOK: Professional ASP.NET MVC 2 2 January 7th, 2011 09:28 AM
Ch14;get Class not register error with Code Example clay5050 BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 5 December 23rd, 2009 07:17 AM
Base Class for Code-Behind Problem groupmatch BOOK: ASP.NET Website Programming Problem-Design-Solution 3 September 8th, 2004 05:06 AM
Need help creating reusable code in a class. jbenson001 ASP.NET 1.x and 2.0 Application Design 1 November 21st, 2003 01:07 PM



All times are GMT -4. The time now is 08:57 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.