Class's Scope

J

JJ

Hi,

I am trying to understand the lifetime or scope of a
class in this project.
Here is the code that I am talking about:

private void PopulateCategoryCombo()
{

ListItem objListItem;

while (drSQL.Read())
{
objListItem = new ListItem(drSQL["CategoryName"].ToString
(),Convert.ToInt32(drSQL["CategoryID"]));

cbCategories.Items.Add(objListItem);
}

}

private void PopulateForm()
{

ListItem objListItem;

// Get Primary Key from Listbox
objListItem = (ListItem)
lstProducts.SelectedItem;

strSQL = "SELECT ProductID, " +

" ProductName, " +

" QuantityPerUnit, " +

" UnitPrice, " +

" UnitsInStock, " +

" UnitsOnOrder, " +

" ReorderLevel, " +

" Discontinued, " +

" SupplierID, " +

" CategoryID " +

"FROM Products " +

"WHERE ProductID = " + objListItem.ID;

}

Now these are fragments of code in these procedures. The
ListItem is a class in the sample project. In the first
proc. the class gets created and filled with Catagory Name
and CatID. Since it was first created in this procedure,
how come it didn't die as soon as the procedure was
finished? Because in the second procedure u can see it
gets recreated and Property ID of the class is called. The
ListItem class is defined as public class ListItem.

So from what I can tell is that the class persists through
the lifetime of the App, is this correct?

Thanks,
JJ
 
J

Joe Mayo [C# MVP]

JJ said:
Hi,

I am trying to understand the lifetime or scope of a
class in this project.
Here is the code that I am talking about:

private void PopulateCategoryCombo()
{

ListItem objListItem;

while (drSQL.Read())
{
objListItem = new ListItem(drSQL["CategoryName"].ToString
(),Convert.ToInt32(drSQL["CategoryID"]));

cbCategories.Items.Add(objListItem);
}

}

private void PopulateForm()
{

ListItem objListItem;

// Get Primary Key from Listbox
objListItem = (ListItem)
lstProducts.SelectedItem;

strSQL = "SELECT ProductID, " +

" ProductName, " +

" QuantityPerUnit, " +

" UnitPrice, " +

" UnitsInStock, " +

" UnitsOnOrder, " +

" ReorderLevel, " +

" Discontinued, " +

" SupplierID, " +

" CategoryID " +

"FROM Products " +

"WHERE ProductID = " + objListItem.ID;

}

Now these are fragments of code in these procedures. The
ListItem is a class in the sample project. In the first
proc. the class gets created and filled with Catagory Name
and CatID. Since it was first created in this procedure,
how come it didn't die as soon as the procedure was
finished? Because in the second procedure u can see it
gets recreated and Property ID of the class is called. The
ListItem class is defined as public class ListItem.

So from what I can tell is that the class persists through
the lifetime of the App, is this correct?

Hi JJ,

In the first method, the ListItem variable is used to instantiate a new
ListItem object. It holds a reference to that object. When the Add method
is called on cbCategories, the cbCategories control box holds a reference to
the same ListItem object that objListItem refers to. When the method exits,
the local variable, objListItem, goes out of scope. However, since
cbCategories belongs to the enclosing Form class, it exists as long as its
enclosing Form class exists. It follows that since cbCategories has a
reference to the ListItem object that was created in the first procedure,
that object is still active.

In the second method, there is a local ListItem class variable named
objListItem, which happens to be the same name as the local ListItem
variable in the first procedure. However, they are separate variables
because objListItem in the first procedure is out of scope. When you access
the SelectedItem from the lstProducts list box, objListItem references a
ListItem object that already existed in lstProducts. The objListItem
variable is referencing an object in a totally different control and there
is no way to tell from the code you provided whether it is the same object
referenced by the cbCategories combo box. That said, I doubt it.

The point is that the objListItem variables are local to the methods they
belong to and go out of scope when the method ends. If there is a reference
to the same object referred to by a local variable, that object will still
exist as long as there is an active reference (root) to it.

Joe
 
D

Daniel O'Connell [C# MVP]

JJ said:
Hi,

I am trying to understand the lifetime or scope of a
class in this project.
Here is the code that I am talking about:

private void PopulateCategoryCombo()
{

ListItem objListItem;

while (drSQL.Read())
{
objListItem = new ListItem(drSQL["CategoryName"].ToString
(),Convert.ToInt32(drSQL["CategoryID"]));

cbCategories.Items.Add(objListItem);

When this line is executed, a reference to your objListItem is placed
*OUTSIDE* of your method. By doing this you persist your list item until it
can no longer be reached by any reference, in this case probably by setting
all variables referencing cbCategories to null or by removing the object
from the cbCatagories.Items collection.
}

}

private void PopulateForm()
{

ListItem objListItem;

// Get Primary Key from Listbox
objListItem = (ListItem)
lstProducts.SelectedItem;

strSQL = "SELECT ProductID, " +

" ProductName, " +

" QuantityPerUnit, " +

" UnitPrice, " +

" UnitsInStock, " +

" UnitsOnOrder, " +

" ReorderLevel, " +

" Discontinued, " +

" SupplierID, " +

" CategoryID " +

"FROM Products " +

"WHERE ProductID = " + objListItem.ID;

}

Now these are fragments of code in these procedures. The
ListItem is a class in the sample project. In the first
proc. the class gets created and filled with Catagory Name
and CatID. Since it was first created in this procedure,
how come it didn't die as soon as the procedure was
finished? Because in the second procedure u can see it
gets recreated and Property ID of the class is called. The
ListItem class is defined as public class ListItem.

So from what I can tell is that the class persists through
the lifetime of the App, is this correct?

Not quite, as I mentioned above, the instance exists as long as a reference
exists somewhere.
 

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