473,749 Members | 2,636 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Where to declare Public variable

I need to have a Public variable in my project. In VB it can be declared in
a standard module. Where can I do it in C# ?
I tried to do it in default class Program.cs and I tried it in an added by
me class.
No success

So, how and where I declare a variable visible by any module in the project?

Esha
Sep 13 '06 #1
15 2999
esha wrote:
I need to have a Public variable in my project. In VB it can be declared in
a standard module. Where can I do it in C# ?
I tried to do it in default class Program.cs and I tried it in an added by
me class.
No success

So, how and where I declare a variable visible by any module in the project?
Quick solution:

public class Globals
{
public SomeClass Varname;
}

can be used as Globals.Varname

Better solution:

look at singleton pattern or another way of restructuring,
because the first one is not good OOP

Arne
Sep 13 '06 #2
"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:UQ%Ng.3783 1$_q4.15928@duk eread09...

You forgot the static
public class Globals
{
public static SomeClass Varname;
}
Michael
Sep 13 '06 #3
Michael C wrote:
"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
>public class Globals
{
public static SomeClass Varname;
}

You forgot the static
It is optional in 2.x+ and not valid in 1.x.

You may argue, that it is good style, but nothing
else in that class is so ...

Arne
Sep 13 '06 #4
I'm still confused. I tried many ways and nothing works.
Could you please give me translation for these 2 blocks of code from VB to
C#:

Imports System.Data.Sql Client

Module GlobalDataStuff

Public objConn As SqlConnection

End Module

'The second block let's say in the Form module:

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

objConn = New SqlConnection

objConn.Connect ionString = "Data Source=DELLNEW; Initial Catalog=SRSNew; User
ID=administrato r;Password=deve loper;"

objConn.Open()

End Sub

Thank you

Esha

"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:UQ%Ng.3783 1$_q4.15928@duk eread09...
esha wrote:
>I need to have a Public variable in my project. In VB it can be declared
in a standard module. Where can I do it in C# ?
I tried to do it in default class Program.cs and I tried it in an added
by me class.
No success

So, how and where I declare a variable visible by any module in the
project?

Quick solution:

public class Globals
{
public SomeClass Varname;
}

can be used as Globals.Varname

Better solution:

look at singleton pattern or another way of restructuring,
because the first one is not good OOP

Arne

Sep 13 '06 #5
esha wrote:
I'm still confused. I tried many ways and nothing works.
Could you please give me translation for these 2 blocks of code from VB to
C#:

Imports System.Data.Sql Client

Module GlobalDataStuff

Public objConn As SqlConnection

End Module

'The second block let's say in the Form module:

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

objConn = New SqlConnection

objConn.Connect ionString = "Data Source=DELLNEW; Initial Catalog=SRSNew; User
ID=administrato r;Password=deve loper;"

objConn.Open()

End Sub
using System.Data.Sql Client;

public class GlobalDataStuff
{
public static SqlConnection objConn;
}

and

GlobalDataStuff .objConn = new SqlConnection() ;
GlobalDataStuff .objConn.Connec tionString = "...";

but this is not good code.

You should reorganize your code to be object oriented.

Arne
Sep 14 '06 #6
Thank you Arne.
I'm the very beginner. I know VB 2005 a little bit and I just wanted to
start somewhere with C#. I couldn't find the way to declare and use public
variables.
Comparing to VB the code you gave me doesn't look better than in VB. Here I
have to give the name of the class where the varible was declared. In VB the
variable declared in a standard module can be used anywhere without naming
the module.
Is there any way to avoid names of the classes?
Another question: How should look the code to be OO? I'm asking about my
sample.

Esha

"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:qg1Og.3783 8$_q4.13221@duk eread09...
esha wrote:
>I'm still confused. I tried many ways and nothing works.
Could you please give me translation for these 2 blocks of code from VB
to C#:

Imports System.Data.Sql Client

Module GlobalDataStuff

Public objConn As SqlConnection

End Module

'The second block let's say in the Form module:

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventAr gs) Handles MyBase.Load

objConn = New SqlConnection

objConn.Connec tionString = "Data Source=DELLNEW; Initial
Catalog=SRSNew ;User ID=administrato r;Password=deve loper;"

objConn.Open ()

End Sub

using System.Data.Sql Client;

public class GlobalDataStuff
{
public static SqlConnection objConn;
}

and

GlobalDataStuff .objConn = new SqlConnection() ;
GlobalDataStuff .objConn.Connec tionString = "...";

but this is not good code.

You should reorganize your code to be object oriented.

Arne

Sep 14 '06 #7

Arne Vajhøj wrote:
Michael C wrote:
"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
public class Globals
{
public static SomeClass Varname;
}
>
You forgot the static

It is optional in 2.x+ and not valid in 1.x.

You may argue, that it is good style, but nothing
else in that class is so ...
Not true. Michael was referring to the "static" keyword on the field
declaration, which is required if you want a "global variable".

It is the "static" keyword on the _class_ declaration that is new (and
optional) in 2.0.

Sep 14 '06 #8
Bruce Wood wrote:
Arne Vajhøj wrote:
>Michael C wrote:
>>"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
public class Globals
{
public static SomeClass Varname;
}

You forgot the static

It is optional in 2.x+ and not valid in 1.x.

You may argue, that it is good style, but nothing
else in that class is so ...

Not true. Michael was referring to the "static" keyword on the field
declaration, which is required if you want a "global variable".
Oops.

Egg on my face.

My apologies.

Arne
Sep 14 '06 #9
esha wrote:
I'm the very beginner. I know VB 2005 a little bit and I just wanted to
start somewhere with C#. I couldn't find the way to declare and use public
variables.
Comparing to VB the code you gave me doesn't look better than in VB. Here I
have to give the name of the class where the varible was declared. In VB the
variable declared in a standard module can be used anywhere without naming
the module.
Is there any way to avoid names of the classes?
Another question: How should look the code to be OO? I'm asking about my
sample.
It is an advantage that you have to specify the class name. It
makes it easier to find the declaration.

No. Everything in C# is classes (or struct or interface or enum).

You could create a singleton for that connection.

But there are something wring with the whole idea of having
just one database connection.

The code should be redesigned so that it works in a multi
threaded context.

The connection should be a non static member of a class
or a local variable.

Arne
Sep 14 '06 #10

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

Similar topics

6
19060
by: rick | last post by:
Noob problem. I prefer to keep all my scripts in an external '.js' file. I am currently loading the external '.js' file from the header. Problem is I would like to declare a global variable in the external file, but I keep getting an error about the object does not exist. Can someone tell me where or how to declare a global variable in an external file that is available after the page is loaded.
9
2675
by: thomson | last post by:
Hi all, Would you please explain me where will be the heap stored if it is declared inside the Class, As class is a reference type, so it gets stored on the heap, but struct is a value type-stored on the stack Regards thomson
5
33079
by: MMSJED | last post by:
I am beginner in using C#, actually I am trying to move from VB6 to C# I need very small help in programming problem my be you will laugh when you get it That simply I have to form let’s say Form1 (main form) and Form2, there is parameter in form2 I have to called from form1. So please would you tell me how? Any way thanks
15
5322
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have private string myArray;
11
2268
by: Geoff Cox | last post by:
Hello, I am trying to get a grip on where to place the initialization of two arrays in the code below which was created using Visual C++ 2005 Express Beta 2... private: static array<String^>^ LHSquestions = gcnew array<String^> {"question 1","question 2"}; private: static array<String^>^ RHSquestions = gcnew array<String^> {"question 1",
2
11098
by: Mamatha | last post by:
Hi If any one knows,please tell me how can we declare a public variable in methods of VB.NET like public indx as integer. How can i use that variable in another methods like global variable.
6
2133
by: **Developer** | last post by:
Notice below I sometimes used the "A" version. I found by cut-and-try that only the "A" version would work correctly. Anyone have a suggestion of why the "W" version would not work correctly? One reason is that the ByRef or ByVal does not show by InteliSence so the "p" would help there. ------ Secondly, I'd like to be consistent with the parameter names. I'd like to
4
2834
by: ankurdave | last post by:
Is it possible to declare a class member variable in the constructor? For example, class SomeClass { public: SomeClass() { int SomeArray; } }
1
1462
by: lokesh kumar | last post by:
where and how to declare global variable in vb2005 ? EggHeadCafe - .NET Developer Portal of Choice http://www.eggheadcafe.com
0
8833
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
9568
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...
1
9335
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
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
8257
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
6079
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
4709
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
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2794
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.