473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Public variable value becomes 0

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?

Sep 15 '07 #1
4 1959
RP wrote:
[...]
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
Sep 15 '07 #2
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" <rp*********@gmail.comschreef in bericht
news:11**********************@57g2000hsv.googlegro ups.com...
>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?
Sep 15 '07 #3
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;

}

}

}

}

"RP" <rp*********@gmail.comwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
>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?

Sep 15 '07 #4
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

"RP" <rp*********@gmail.comwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
>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?

Sep 15 '07 #5

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

Similar topics

10
by: R.G. Vervoort | last post by:
I am using a javafunction (onclick in select) in which i am calling a function in php (thats why i send this to both php and javascript newsgroups). in the onclick i call the function...
10
by: Zap | last post by:
Widespread opinion is that public data members are evil, because if you have to change the way the data is stored in your class you have to break the code accessing it, etc. After reading this...
4
by: louise raisbeck | last post by:
Resending this as own topic as didnt get answer from original. Would be grateful for a response from anyone that knows. Thanks. Hi there, I found your post really helpful..but i wondered if, once...
27
by: thomasp | last post by:
Variables that I would like to make available to all forms and modules in my program, where should I declare them? At the momment I just created a module and have them all declared public there. ...
25
by: Sourav | last post by:
Suppose I have a code like this, #include <stdio.h> int *p; void foo(int); int main(void){ foo(3); printf("%p %d\n",p,*p);
2
by: fachero1 | last post by:
How Can I set a public global variable whos value can be accessed by any thread? not just by one thread but by all?... Currently from what I see and understand if a thread is created the global...
9
by: Rudy | last post by:
Hello All! I'm a little confused on Public Class or Modules. Say I have a this on form "A" Public Sub Subtract() Dim Invoice As Decimal Dim Wage As Decimal Static PO As Decimal Invoice =...
29
by: garyusenet | last post by:
I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is:...
11
by: Web Search Store | last post by:
Hello, I set up a web page with 2 user controls. In classic asp, the first one did all the declarations, and the second one used the values, and could reset it. In ASP.Net so far I can't...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.