By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,110 Members | 1,639 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,110 IT Pros & Developers. It's quick & easy.

Where to declare Public variable

P: n/a
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
Share this Question
Share on Google+
15 Replies


P: n/a
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

P: n/a
"Arne Vajh°j" <ar**@vajhoej.dkwrote in message
news:UQ%Ng.37831$_q4.15928@dukeread09...

You forgot the static
public class Globals
{
public static SomeClass Varname;
}
Michael
Sep 13 '06 #3

P: n/a
Michael C wrote:
"Arne Vajh°j" <ar**@vajhoej.dkwrote 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

P: n/a
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.SqlClient

Module GlobalDataStuff

Public objConn As SqlConnection

End Module

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

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

objConn = New SqlConnection

objConn.ConnectionString = "Data Source=DELLNEW;Initial Catalog=SRSNew;User
ID=administrator;Password=developer;"

objConn.Open()

End Sub

Thank you

Esha

"Arne Vajh°j" <ar**@vajhoej.dkwrote in message
news:UQ%Ng.37831$_q4.15928@dukeread09...
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

P: n/a
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.SqlClient

Module GlobalDataStuff

Public objConn As SqlConnection

End Module

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

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

objConn = New SqlConnection

objConn.ConnectionString = "Data Source=DELLNEW;Initial Catalog=SRSNew;User
ID=administrator;Password=developer;"

objConn.Open()

End Sub
using System.Data.SqlClient;

public class GlobalDataStuff
{
public static SqlConnection objConn;
}

and

GlobalDataStuff.objConn = new SqlConnection();
GlobalDataStuff.objConn.ConnectionString = "...";

but this is not good code.

You should reorganize your code to be object oriented.

Arne
Sep 14 '06 #6

P: n/a
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.dkwrote in message
news:qg1Og.37838$_q4.13221@dukeread09...
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.SqlClient

Module GlobalDataStuff

Public objConn As SqlConnection

End Module

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

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

objConn = New SqlConnection

objConn.ConnectionString = "Data Source=DELLNEW;Initial
Catalog=SRSNew;User ID=administrator;Password=developer;"

objConn.Open()

End Sub

using System.Data.SqlClient;

public class GlobalDataStuff
{
public static SqlConnection objConn;
}

and

GlobalDataStuff.objConn = new SqlConnection();
GlobalDataStuff.objConn.ConnectionString = "...";

but this is not good code.

You should reorganize your code to be object oriented.

Arne

Sep 14 '06 #7

P: n/a

Arne Vajh°j wrote:
Michael C wrote:
"Arne Vajh°j" <ar**@vajhoej.dkwrote 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

P: n/a
Bruce Wood wrote:
Arne Vajh°j wrote:
>Michael C wrote:
>>"Arne Vajh°j" <ar**@vajhoej.dkwrote 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

P: n/a
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

P: n/a
Unfortunately, as I said I'm the very beginner. I googled SINGLETON and
found something I cannot understand.
VB samples I analyzed and tried to use their approaches have similar design.
Public variables in modules and no word about SINGLETON.

Esha

"Arne Vajh°j" <ar**@vajhoej.dkwrote in message
news:FP1Og.37842$_q4.33535@dukeread09...
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 #11

P: n/a
Esha wrote:
Unfortunately, as I said I'm the very beginner. I googled SINGLETON and
found something I cannot understand.
VB samples I analyzed and tried to use their approaches have similar design.
Public variables in modules and no word about SINGLETON.
I think you should drop the singleton for now.

Get your code working.

And then try and see if you can move from public static
fields to private non static fields and local variables.

Arne
Sep 14 '06 #12

P: n/a
esha,

A module in VB.Net is almost the same what is called in that a Shared class.
A class with shared members and properties which cannot be instanced but
exist forever in the program.
While you have forever to start with the class name.

The same is in C# but than it is called Static (Here is not used the word
Static class) however it is the same.

Although I whould never to do it this way, to mimic a VBNet module.

Public Class MyModule
{
public string Whatever = "Esha";
}
End Class

Now
string MyString = MyModule.Whatever;
will give Esha

I hope this helps,

Cor

"esha" <es**@newsgroups.comschreef in bericht
news:uO*************@TK2MSFTNGP05.phx.gbl...
>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 14 '06 #13

P: n/a
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
esha,

A module in VB.Net is almost the same what is called in that a Shared
class.
A class with shared members and properties which cannot be instanced but
exist forever in the program.
Technically doesn't the class never exist?

Michael
Sep 14 '06 #14

P: n/a
Hi Esha,

in .Net you could better create a new connection object in every method
where you use it. The framework will do connectionpooling amyway.
The static variable should only contain the connectionstring (and preferably
be
filled from configuration).

"esha" <es**@newsgroups.comschrieb im Newsbeitrag
news:ud*************@TK2MSFTNGP06.phx.gbl...
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.dkwrote in message
news:qg1Og.37838$_q4.13221@dukeread09...
>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.SqlClient

Module GlobalDataStuff

Public objConn As SqlConnection

End Module

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

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

objConn = New SqlConnection

objConn.ConnectionString = "Data Source=DELLNEW;Initial
Catalog=SRSNew;User ID=administrator;Password=developer;"

objConn.Open()

End Sub

using System.Data.SqlClient;

public class GlobalDataStuff
{
public static SqlConnection objConn;
}

and

GlobalDataStuff.objConn = new SqlConnection();
GlobalDataStuff.objConn.ConnectionString = "...";

but this is not good code.

You should reorganize your code to be object oriented.

Arne


Sep 14 '06 #15

P: n/a
doh,

I hope nobody saw this

It has to be,

public class MyModule
{
public static string Whatever = "Esha";
}

Now
string MyString = MyModule.Whatever;
will give Esha

Sorry Cor
Sep 14 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.