473,398 Members | 2,404 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,398 software developers and data experts.

Global variable question.

I'm switching over from VB and I have a question.

Suppose I had a VB project with 2 forms and one variable. Call them
Form1,Form2 and strUsername. Suppose I want to initialize strUsername
to "jones" and use this value in Form1 and Form2 at any time.

In VB I would create a module with the following code:

Module test
Public strUsername As String
End Module

In my Form1_Load event I would have something like this:

Private Sub Form1_Load
strUsername = "jones"
End Sub

So my question is what would be the best way to do this in C#? I
realize there are no globals so the only thing I can think of doing is
to add a property to Form1 and Form2 called strUsername and set it each
time I activate either Form1 or Form2.

Aug 18 '06 #1
3 2960

cr113 wrote:
I'm switching over from VB and I have a question.

Suppose I had a VB project with 2 forms and one variable. Call them
Form1,Form2 and strUsername. Suppose I want to initialize strUsername
to "jones" and use this value in Form1 and Form2 at any time.

In VB I would create a module with the following code:

Module test
Public strUsername As String
End Module

In my Form1_Load event I would have something like this:

Private Sub Form1_Load
strUsername = "jones"
End Sub

So my question is what would be the best way to do this in C#? I
realize there are no globals so the only thing I can think of doing is
to add a property to Form1 and Form2 called strUsername and set it each
time I activate either Form1 or Form2.
There are "globals" in C#, after a fashion. Now, to my understanding
it's different in ASP.NET (WebForms), but in a WinForms application you
just need to make a class that will contain some global information. In
your example, let's say it's user information. You could make a class
like this:

public class UserInformation
{
private static string _userName = "";

// So that nobody can create an instance... .NET 2.0 has "static
class" for this...
private UserInformation()
{ }

public static string UserName
{
get { return UserInformation._userName; }
set
{
if (value == null)
{ this._userName = ""; }
else
{ this._userName = value; }
}
}
}

Now you can say UserInformation.UserName from any class in your
program, and get / set the user name.

Aug 18 '06 #2

Bruce Wood wrote:
cr113 wrote:
I'm switching over from VB and I have a question.

Suppose I had a VB project with 2 forms and one variable. Call them
Form1,Form2 and strUsername. Suppose I want to initialize strUsername
to "jones" and use this value in Form1 and Form2 at any time.

In VB I would create a module with the following code:

Module test
Public strUsername As String
End Module

In my Form1_Load event I would have something like this:

Private Sub Form1_Load
strUsername = "jones"
End Sub

So my question is what would be the best way to do this in C#? I
realize there are no globals so the only thing I can think of doing is
to add a property to Form1 and Form2 called strUsername and set it each
time I activate either Form1 or Form2.

There are "globals" in C#, after a fashion. Now, to my understanding
it's different in ASP.NET (WebForms), but in a WinForms application you
just need to make a class that will contain some global information. In
your example, let's say it's user information. You could make a class
like this:

public class UserInformation
{
private static string _userName = "";

// So that nobody can create an instance... .NET 2.0 has "static
class" for this...
private UserInformation()
{ }

public static string UserName
{
get { return UserInformation._userName; }
set
{
if (value == null)
{ this._userName = ""; }
else
{ this._userName = value; }
}
}
}

Now you can say UserInformation.UserName from any class in your
program, and get / set the user name.
Thinking about this some more, I think that it's more likely that in
your program you have something called a "User", and that user has an
attribute, which is "Name". This allows you to add more attributes to
your "User" in the future, rather than creating a bunch of independent
global variables for the same thing. You could modify the code like
this:

public class User
{
private static _currentUser = null;
private string _name;

public User() : this("")
{ }

public User(string name)
{
if (name == null) throw new ArgumentNullException("name");
this._name = name;
}

public string Name
{
get { return this._name; }
set
{
if (value == null)
{ this._name = ""; }
else
{ this._name = value; }
}
}

public static User CurrentUser
{
get
{
if (User._currentUser == null) User._currentUser = new
User();
return User._currentUser;
}
set { User._currentUser = value; }
}
}

Now you can get the current user's name using

User.CurrentUser.Name

from any class in your program. You can also create other User
instances with other names, and add more attributes to your User (like
e-mail address, password, whatever) in the future.

Aug 18 '06 #3

Excellent!

Thanks!

Aug 18 '06 #4

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

Similar topics

10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
9
by: Tony Johansson | last post by:
Hello! I know it's bad design to use global variables. I just want to ask a question about them. Is global variables and global static variables the same. These are define outside any...
7
by: Lyn | last post by:
Hi and Season's Greetings to all. I have a question regarding the use of a qualifier word "Global". I cannot find any reference to this in Access help, nor in books or on the Internet. "Global"...
44
by: Mohanasundaram | last post by:
int i = 10; int main() { int i = 20; return 0; } Hi All, I want to access the global variable i inside the main. Is there
24
by: LP | last post by:
After a code review one coworker insisted that global are very dangerous. He didn't really give any solid reasons other than, "performance penalties", "hard to maintain", and "dangerous". I think...
44
by: fabio | last post by:
Why? i' ve heard about this, the usage of global vars instead of locals is discouraged, but why? thx :)
37
by: eoindeb | last post by:
Sorry to ask another global variable question, but from reading other posts I'm still not sure whether to use them or not. I have a program with a set function that calls 4 other functions in...
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
9
by: Ed Jensen | last post by:
I'm having a vexing problem with global variables in Python. Please consider the following Python code: #! /usr/bin/env python def tiny(): bar = for tmp in foo: bar.append(tmp) foo = bar
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...
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.