473,803 Members | 2,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class's Scope

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 PopulateCategor yCombo()
{

ListItem objListItem;

while (drSQL.Read())
{
objListItem = new ListItem(drSQL["CategoryNa me"].ToString
(),Convert.ToIn t32(drSQL["CategoryID "]));

cbCategories.It ems.Add(objList Item);
}

}

private void PopulateForm()
{

ListItem objListItem;

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

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

Nov 15 '05 #1
2 1608
"JJ" <an*******@disc ussions.microso ft.com> wrote in message
news:85******** *************** *****@phx.gbl.. .
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 PopulateCategor yCombo()
{

ListItem objListItem;

while (drSQL.Read())
{
objListItem = new ListItem(drSQL["CategoryNa me"].ToString
(),Convert.ToIn t32(drSQL["CategoryID "]));

cbCategories.It ems.Add(objList Item);
}

}

private void PopulateForm()
{

ListItem objListItem;

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

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
--
http://www.csharp-station.com
Nov 15 '05 #2

"JJ" <an*******@disc ussions.microso ft.com> wrote in message
news:85******** *************** *****@phx.gbl.. .
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 PopulateCategor yCombo()
{

ListItem objListItem;

while (drSQL.Read())
{
objListItem = new ListItem(drSQL["CategoryNa me"].ToString
(),Convert.ToIn t32(drSQL["CategoryID "]));

cbCategories.It ems.Add(objList Item);
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.It ems collection.
}

}

private void PopulateForm()
{

ListItem objListItem;

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

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.
Thanks,
JJ

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2212
by: Jerry | last post by:
My "main" class is getting a bit long...Is it possble to split a class definition into several files and then import the pieces to get the whole definition? Jerry
6
3198
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
30
2280
by: Neil Zanella | last post by:
Hello, Suppose I have some method: Foo::foo() { static int x; int y; /* ... */ }
9
2452
by: Steven T. Hatton | last post by:
It was once suggested to me that I could accomplish much the same thing that modules would accomplish (if C++ had modules) by writing my entire program - except for main() - inside of a class. When it was suggested, I didn't take it very seriously, but I have recently begun wondering if it is an idea worth considering. I'm now trying to think of what fundamental differences might exist between namespace scope, and class scope. One that...
6
2501
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
7
2118
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9 ----------------------------------------------------------------------------- This is a consortium of ideas from another thread on topic ----------------------------------------------------------------------------- One of the big issues of organizing items within a class, is there are many...
5
2037
by: Steven T. Hatton | last post by:
If find the following excerpt from the Standard a bit confusing: <quote> 3.3.6 - Class scope -1- The following rules describe the scope of names declared in classes. 1) The potential scope of a name declared in a class consists not only of the declarative region following the name's declarator, but also of all function bodies, default arguments, and constructor ctor-initializers in that class (including such things in nested classes).
15
7874
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
200
by: Maric Michaud | last post by:
Le Tuesday 12 August 2008 11:29:18 Cousson, Benoit, vous avez écrit : This is a language limitation. This is because nested scope is implemented for python function only since 2.3 allow late binding of free variables. the scope in class statment is not a closure, so there is only two possible scope in it : local and global. When "class C2(C1):" statment is interpreted, it is in the scope of class B for which a name C1 exists, but it...
0
154
by: Cousson, Benoit | last post by:
This is a language limitation. That was my understanding as well, but I think it is a pity to have that limitation. Don't you think that the same improvement that was done for method nested scope could be done as well for nested class? I can easily fix my current issue by doing the binding after the class declaration. My concern is more about the lack of symmetry of that approach; meaning that if both classes are in the global scope, one...
0
9566
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9127
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7607
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2974
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.