Global variables

K

kuhrty

I am using a window form and trying to get a dataset used in the form
globally but I am having an issue setting the property and setting it
locally to the class. When I try to call the dataset later, I end up
re-instaniating the class which I know its not need. My issue is the
code that I am using setting the dt = dsPyramid.Get_Pyramid. When I
change the code I am getting error that the class can't be found. Any
help would be greatly appreciated, I am also new and I am still
learning. There maybe a better way to code this in general.

Mark

//Windows form global variable
DataSet dsPyramid;

//Private method on the form
private void Load_Pyramid()
{
DataTable dt = new DataTable();
int i;

Pyramid dsPyramid = new Pyramid();
dt = dsPyramid.Get_Pyramid; // I am messing up the code to set
the variable
cbPyramid.Items.Clear();

for (i = 0; i < dt.Rows.Count; i++)
{
cbPyramid.Items.Add(dt.Rows["Pyramid"]);
}
}

//Class code
public class Pyramid
{
#region Private Members

private DataSet _dsPyramid;

#region Public Properties
public DataTable Get_Pyramid
{
get { return _dsPyramid.Tables[0]; }
}
#endregion

#region Constructors
public Pyramid()
{
//This is setting up the connection
Database db = DatabaseFactory.CreateDatabase("Test");
//This will execute the record set immediately
_dsPyramid =
db.ExecuteDataSet(CommandType.StoredProcedure,"usp_GetPyramid");
}
#endregion
}
 
J

Joanna Carter [TeamB]

"kuhrty" <[email protected]> a écrit dans le message de (e-mail address removed)...

|I am using a window form and trying to get a dataset used in the form
| globally but I am having an issue setting the property and setting it
| locally to the class. When I try to call the dataset later, I end up
| re-instaniating the class which I know its not need. My issue is the
| code that I am using setting the dt = dsPyramid.Get_Pyramid. When I
| change the code I am getting error that the class can't be found. Any
| help would be greatly appreciated, I am also new and I am still
| learning. There maybe a better way to code this in general.

If you mean you are trying to get a dataset declared in the form to outlive
the form, then you would have to declare it as a static member, not an
instance member.

| //Windows form global variable
| DataSet dsPyramid;

There is no such thing as a global variable in C#, you have simply declared
a field in your class which is scoped to an instance of your form class.

You would, at least, need to set up a static class to hold the "global"
instance, but you might need also to reorganise your code as it seems
somewhat convoluted.

public static class Pyramid
{
private static DataSet _dsPyramid;

static Pyramid()
{
Database db = DatabaseFactory.CreateDatabase("Test");
_dsPyramid = db.ExecuteDataSet(CommandType.StoredProcedure,
"usp_GetPyramid");
}

public static DataTable Get_Pyramid
{
get { return _dsPyramid.Tables[0]; }
}

Joanna
 
C

Cor Ligthert [MVP]

Kurthy,

You are not using that dataset.
You are using the local variable dt which is in not any way connected with
your global placed dataset.

On first sight is my idea that you miss.
dsPyramid.Tables.Add(dt);

Although in this case I would probably return the dataset instead of your
table.

I hope this helps,

Cor
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

You must have messed up the code somehow when pasting it.

You are declaring a local variable dsPyramid in the Load_Pyramid method
that has the same name as the variable you declared in the class. That
means that you can't access the variable that you declared in the class
from inside the method.

You are storing values in the cbPyramid variable, that you never created.
 
K

Kevin Spencer

Hi kuhrty,

You have several issues in your code. First, you have a private field (the
correct term for a "global or class-scoped variable" in your Form) named
"dsPyramid" of type DataSet. But in your method "Load_Pyramid" you declare a
locally-scoped (to your method) variable named "dsPyramid" of the type
DataSet, and you create an instance of it, which populates it from your
database, and then assign a table from it to a DataTable. The problem here
is that you have declared a locally-scoped method variable with the same
name as the class-scoped field. When this happens, the variable in the
method, which is closer in scope to the method code, "shadows" the
class-scoped field. Be aware that because you have explicitly declared it,
it is not a reference to the class-scoped field, but is a variable that is
scoped to the method. So, the class-scoped field is never assigned to, and
when the method exists, the method-scoped variable passes out of scope and
is gone. With each method call, it is created all over again.

It is very important in .Net to understand scope. I recommend reading the
following MSDN2 article:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/methodscope.asp

Also, I couldn't help but notice the following issue, which will not cause a
problem (other than wasting processor cycles and memory), but indicates to
me a hole in your understanding of the declaration and usage of variables
and fields:

DataTable dt = new DataTable();
int i;

Pyramid dsPyramid = new Pyramid();
dt = dsPyramid.Get_Pyramid;

Note the declaration of a DataTable at the top, which is combined with an
assignment to that variable of a new DataTable. This could also be written
as:

DataTable dt;
dt = new DataTable();

Next, a few lines down, you assign the return value of a property (which
returns a DataTable) to the variable:

dt = dsPyramid.Get_Pyramid;

This has the effect of replacing the new DataTable just assigned to the
variable with the DataTable returned by the property. In other words, you
create a new DataTable, and then throw it away.

It is important to understand what a variable is, and what assignment is. It
is best to think of a variable as a "box" that is specially built to contain
a certain type of data. When you declare a variable, you are essentially
telling your application "I need a box that I can store a DataTable in."
Note that the "box" and the "contents" of the box are completely different
things. When you assign a DataTable to the variable, you are essentially
"putting a DataTable into the box." Now, since the "box" can only contain
*one* item, every time you assign another "item" to the "box" it replaces
whatever is already *in* the box. So, when you put a new DataTable into the
box (DataTable dt = new DataTable()), you are creating the box, creating a
new DataTable, and putting the new DataTable into the box. When you
re-assign it from a property, you remove the original DataTable, and replace
it with the one you assign.

The confusion probably arises from the fact that the language syntax allows
you to refer to the box as if it were what it contains. In fact, this is not
true, but only a convenience for the programmer. It is much like algebra.
Consider the following:

x = y + 1

The variables 'x' and 'y' are "boxes" or containers for numbers. Now, if you
assign a value to either, it determines the value of the other:

x = 2
x = y + 1
Therefore, y = 1

In this algebraic series of statements, we are treating the variables as if
they were the numbers they represent. It is the same in programming. A
variable is not a value, but *represents* a value. It gives the value a
"name" or a "handle" we can refer to it by in statments. When you assign it,
you are also *re*-assigning it to the new value. So, unless you have a
reason for creating a new DataTable, the following would be better:

DataTable dt;
int i;

Pyramid dsPyramid = new Pyramid();
dt = dsPyramid.Get_Pyramid;

Or even:

Pyramid dsPyramid = new Pyramid();
DataTable dt = dsPyramid.Get_Pyramid;

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

I recycle.
I send everything back to the planet it came from.

kuhrty said:
I am using a window form and trying to get a dataset used in the form
globally but I am having an issue setting the property and setting it
locally to the class. When I try to call the dataset later, I end up
re-instaniating the class which I know its not need. My issue is the
code that I am using setting the dt = dsPyramid.Get_Pyramid. When I
change the code I am getting error that the class can't be found. Any
help would be greatly appreciated, I am also new and I am still
learning. There maybe a better way to code this in general.

Mark

//Windows form global variable
DataSet dsPyramid;

//Private method on the form
private void Load_Pyramid()
{
DataTable dt = new DataTable();
int i;

Pyramid dsPyramid = new Pyramid();
dt = dsPyramid.Get_Pyramid; // I am messing up the code to set
the variable
cbPyramid.Items.Clear();

for (i = 0; i < dt.Rows.Count; i++)
{
cbPyramid.Items.Add(dt.Rows["Pyramid"]);
}
}

//Class code
public class Pyramid
{
#region Private Members

private DataSet _dsPyramid;

#region Public Properties
public DataTable Get_Pyramid
{
get { return _dsPyramid.Tables[0]; }
}
#endregion

#region Constructors
public Pyramid()
{
//This is setting up the connection
Database db = DatabaseFactory.CreateDatabase("Test");
//This will execute the record set immediately
_dsPyramid =
db.ExecuteDataSet(CommandType.StoredProcedure,"usp_GetPyramid");
}
#endregion
}
 

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