ASP.net - Scope Issue?

ComputerNovice

Baseband Member
Messages
22
I've created an array at the top of my C# class, in the code behind the page:

List<Server> ServerArray = new List<Server>();



When I attempt to run the Insert button click method, a new server object is successfully added to the ArrayList and is, afterwards, displayed in the Listbox. See below:

ServerArray.Add( new Server(strserverName, strSerialNum));

int index = ServerArray.Count - 1;
ServerListBox.Items.Add(ServerArray[index].MName + ServerArray[index].MSerialNumber.ToUpper());


My problem is that once I click the Insert button again, to add another server, it appears as though the ArrayList clears itself of the previous reference and starts all over from an index of 0 again, while the Listbox retains the previous entry and displays the additional server.

Is the scope of the Insert button Click Method causing the ArrayList to clear itself, once that method ends? I thought, that since the ArrayList has global scope, the references would remain.
 
Is the ServerArray or ListBox DataBound to something?

Is the page posting back when you click Insert or is it just refreshing that control?

Could you post code snippets of your Class (and maybe even the page-side declarations of your insert/listbox controls)? Use the
Code:
 tag to enclose it in white-space friendly element in the reply.
 
Thanks for your reply. No, they are not databound.

Here is my Server class:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


public class Server
{
    private string mName;
    private string mSerialNumber;
    
    public Server(string ServerName, string SerialNumber)
    {   MName = ServerName;
        MSerialNumber = SerialNumber;
     }

    public string MName
    {
        get
        { 
            return mName; 
        }
        set
        {
            mName = value;
        }
    }

    public string MSerialNumber
    {
        get 
        {
            return mSerialNumber;
        }
        set
        {
            mSerialNumber = value;
        }
    }

}


The Insert button control:

Code:
<asp:Button ID="btnInsert" runat="server" OnClick="btnInsert_Click" Text="Insert" 
                        Width="50px" />

The Listbox control:

Code:
<asp:ListBox ID="ServerListBox" runat="server" Height="202px" 
                         Width="677px">
                    </asp:ListBox>
 
You're not by chance clearing the ServerArray list somewhere, are you?

Have you stepped through your code one line at a time through the debugger and seeing if the ServerArray still contains the data?
 
I've only done it within the insert click method. But I may have to do that on a larger scope, next.

But I haven't cleared the Array, purposely, anywhere else in the code.
 
I've only done it within the insert click method. But I may have to do that on a larger scope, next.

But I haven't cleared the Array, purposely, anywhere else in the code.

I'd say step through and see if it's getting cleared somewhere else somehow.

Also, just a tip (not really necessary), but a suggestion for your Server class. Since you're using primitive data type (string), you don't need both class fields AND properties, unless you're using them for something else.

You could just do empty gets/sets, and then you still have your data stored in the class instance, as such:

Code:
    public string MName
    {
        get;
        set;
    }

    public string MSerialNumber
    {
        get;
        set;
    }

Just makes for less code :).
 
Back
Top Bottom