473,698 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.TotalRe cords=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.Total Records;
}

The value of TotalRecords being shown is 0 whereas, I assigned it
value 10. Why it is becoming 0?

Sep 15 '07 #1
4 1976
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.TotalRec ords = 10;
// etc...
}

private void LeaveRecordForm _Load(object sender, EventArgs e)
{
textBox1.Text = Global.TotalRec ords.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*********@gm ail.comschreef in bericht
news:11******** **************@ 57g2000hsv.goog legroups.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.TotalRe cords=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.Total Records;
}

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.Collecti ons.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.WriteLi ne(two.global1) ;

//set global1 from instance 2

two.global1 = 20;

//instance one has value of 20

Console.WriteLi ne(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*********@gm ail.comwrote in message
news:11******** **************@ 57g2000hsv.goog legroups.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.TotalRe cords=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.Total Records;
}

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*********@gm ail.comwrote in message
news:11******** **************@ 57g2000hsv.goog legroups.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.TotalRe cords=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.Total Records;
}

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
7523
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 "Place_Selected" with the value from the select (naam_keuze.value) in the function the value becomes the $zoek_id and searches in the database for the record with the id of $zoek_id
10
5782
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 (also copied below for easier reference): http://groups.google.it/groups?hl=en&lr=&safe=off&selm=6beiuk%24cje%40netlab.cs.rpi.edu&rnum=95 I don't agree anymore.
4
2513
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 I have exposed a public property containing the value of a textbox in a user control..how do I grab this from the calling page? I cant think of the syntax, since my page doesnt know the contents of the class (and therefore, the public...
27
2709
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. What is the normal way to do this? Thanks, Thomas --
25
9524
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
1566
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 variable becomes locked or something like that... how can I do this? in my case this would be totally safe to do ^.^ thanks Jonathan
9
2357
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 = CDec(txbInv.Text) Wage = CDec(txbTotWage.Text)
29
5100
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: 65,535. But i'm trying to write a program to test this, assuming I didn't know this number in advance. I came up with the following but have two questions. Maybe someone can help? using System; using System.Collections.Generic; using System.Text;
11
1675
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 see how to relate them so this will work. This user control defines the properties:
0
8611
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
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8876
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7741
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...
0
5867
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
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.