project level scope declaration in C#

  • Thread starter Thread starter Information
  • Start date Start date
I

Information

Dear Friends,

In VB.NET you can declare something like following..

Module modVar2
Public sb as String
End Module

Now, sb is a project level scope variable and can be accessed any where
in the project.

Similarly I want to do it in C#.NET.

Any help would be appreciated.

Thanks
kdsv
 
Information said:
In VB.NET you can declare something like following..

Module modVar2
Public sb as String
End Module

Now, sb is a project level scope variable and can be accessed any where
in the project.

Similarly I want to do it in C#.NET.

Any help would be appreciated.

public class ModVar2
{
public static string sb;
}

It's generally not a good idea to have actual global variables like
this though - constants, and public static properties are fine, but
variables themselves don't give you much control.
 
Similarly I want to do it in C#.NET.

Just add a public static field or property to class. The only
difference is that you have to include the class name to reference the
member, it will not auto-import like VB Module members do.


Mattias
 
This is my biggest dislike of C# and Java vs C++, VB and most anything else.

I don't want to see global variable added to C#. However I really really
really would love something in the language that lets me treat static members
and functions of a class as if they were in the current scope.

This is purely for reason of saving the amount of typing I do (both speed of
development, and readability). For example, I want to just type:
x = DoThis(n);
Instead of:
x = MyLibrary.DoThis(n);

If I have a lot of code and frequently use utility functions accross my app,
this really is a lot of typing I have to do in C# that I would not have in
C++. Yes, I know I can just add a function to the class I am working on:

static int DoThis(int x)
{
return MyLibrary.DoThis();
}

but it is a pain to do this all the time for every class and library
function I use often.

How about a feature like putting
using globalscope = MyLibary;
at the top of my class?
 
<=?Utf-8?B?QnJhZCBCZWxsb21v?= <Brad
This is my biggest dislike of C# and Java vs C++, VB and most anything else.

Well, Java 1.5 has import static which does exactly what you're after.
In my experience it's handy in certain situations (particularly for
enums), but shouldn't be used particularly often.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top