Public variable value becomes 0

  • Thread starter Thread starter RP
  • Start date Start date
R

RP

I have a class file (Global.cs) containing following variable:

Public Int32 TotalRecords=0

I have a Windows Form from where I am assigning a value to this value
as below:

private void SaveRecord()
{
Global objGlob = new Global();
objGlob.TotalRecords=10;

//Open other Form
LeaveRecordForm LeaveRec = new LeaveRecordForm();
LeaveRec.Show();
}

Now, in LeaveRecordForm Load I want to show the value of TotalRecords
in a Text Box.

private void LeaveRecordForm_Load(object sender, EventArgs e)
{
Global objGlobal = new Global();
TextBox1.Text = objGlobal.TotalRecords;
}

The value of TotalRecords being shown is 0 whereas, I assigned it
value 10. Why it is becoming 0?
 
That you give it the name global does not mean that it is global.

You have to declare it on a global place (in other words outside the
method), then when you don't create a new one as you do in the method where
it is used, you can do what you want.

Cor
 
RP said:
[...]
The value of TotalRecords being shown is 0 whereas, I assigned it
value 10. Why it is becoming 0?

It's not "becoming" 0. You aren't using the same instance of the class.
In the instance in which you retrieve it, the value was never set, and
so it still has the original default value of 0.

And likewise, the instance in which you set it lived only long enough
for you to set it; at some point shortly after you set the value, the
garbage collector came along and released the instance in which you set
the value, because no one was referring to it any longer.

It sounds as though you are looking for some sort of "global variables"
class. Keeping in mind, of course, that it is generally better to have
data associated with some specific class rather than a general-purpose
"global variables" class, let's assume the desired behavior is reasonable.

Then what you probably want is for the class to actually just be a
static class. Declare the class and all of its members to be static,
then rather than instantiating the class, you'll just refer to it by the
type name. For example:

static class Global
{
static public int TotalRecords = 0;
}

(the initialization is superfluous, since 0 is the default for int
anyway, but whatever...)

Then elsewhere:

private void SaveRecord()
{
Global.TotalRecords = 10;
// etc...
}

private void LeaveRecordForm_Load(object sender, EventArgs e)
{
textBox1.Text = Global.TotalRecords.ToString();
}

Now, all that said, I would revisit my previous comment about avoiding
globals. They aren't in and of themselves terrible, but they are often
misused and, frankly, the short snippet of code you've provided here
seems to possibly be such a case. Two forms should not be using a
global variable to communicate with each other, IMHO. It is likely that
it would be better for the LeaveRecordForm constructor to take the value
as a parameter, or for the LeaveRecordForm class to expose a property
that the other Form can set, or for the other Form to expose the value
as a property and pass a reference to that Form instance to the
LeaveRecordForm (again, either in the constructor or a public property).

But if you really want a global variable, the above is one way to do it.

Pete
 
Myself dont like globals for the most part, they do have there place...below
is a example of how to use statics withing a instance class

DaveP
using System;

using System.Collections.Generic;

using System.Text;

namespace StaticClass

{

class Program

{

static void Main(string[] args)

{

myglobals one = new myglobals();

one.global1 = 10;


myglobals two = new myglobals();

//instance two has value of 10

Console.WriteLine(two.global1);

//set global1 from instance 2

two.global1 = 20;

//instance one has value of 20

Console.WriteLine(one.global1);

//instance two can change instance one global

//so all instances of this class can see

//the static fields through properties of either instance

Console.ReadKey();

}

}

public class myglobals

{

//all instances of this class the static fields/properties

//are visible and retain there values

static int _global1;


public int global1

{

set

{

_global1 = value;

}

get

{

return _global1;

}

}

}

}
 
the nice part about that class
you can be way down in your App Some where
and can retrieve all your app public/global vars
with one instance of the above class
you can set a field to you app.config,etc. , all sorts of information you
need to be global...and in fact is not declared global
DaveP
 
Back
Top