Where to declare Public variable

  • Thread starter Thread starter esha
  • Start date Start date
E

esha

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
 
esha said:
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
 
Michael said:
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
 
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
 
esha said:
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
 
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 said:
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.
 
Bruce said:
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
 
esha said:
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
 
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
 
Esha said:
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
 
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
 
Cor Ligthert said:
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
 
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).
 
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
 
Back
Top