473,568 Members | 2,923 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

project level scope declaration in C#

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

Nov 17 '05 #1
5 1994
Information <ku******@gmail .com> wrote:
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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
>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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #3
Thanks a lot guys.

Nov 17 '05 #4
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.DoThi s(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.DoThi s();
}

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?
"Jon Skeet [C# MVP]" wrote:
Information <ku******@gmail .com> wrote:
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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #5
<=?Utf-8?B?QnJhZCBCZWx sb21v?= <Brad
Be*****@discuss ions.microsoft. com>> wrote:
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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6

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

Similar topics

18
3149
by: JKop | last post by:
Here's what I know so far: You have a C++ project. You have source files in it. When you go to compile it, first thing the preprocessor sticks the header files into each source file. So now you have your ".cpp" files all ready, without any "#include" or "#define" in them. Let's assume that there's 2 source files in this project,...
8
3362
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an actual program. To me "scope" of the name of a function or object are the general rules for the areas of a program that can through a declaration, have...
7
2175
by: Kobu | last post by:
The code below isn't compiling for me (error message: conflicting types for 'total' - pointing to the extern declaration). Why wouldn't this work, since the types are different, the extern declaration obviously refers to the 'long total' in total.c? Is my compiler wrong? total.c
10
3814
by: ElanKathir .S.N | last post by:
Hi all ! VB.NET adds the ability to create variables that are visible only within a block. A block is any section of code that ends with one of the words End , Loop , or Next . This means that For...Next and If...End If blocks can have their own variables. So,
8
2011
by: Howard Kaikow | last post by:
I got bored today, so I decided to rewrite the code in KB article 316383 to decrease the number of object references. This resulted in a number of nested With ... End With. The original code had a Dim r As Integer, c As Integer shortly before a For Next.
1
1172
by: Alec MacLean | last post by:
Hi all, Can anyone tell me if there is any functional difference between declaring an Imports at the individual file level compared with doing it once on the project properties? ( - Other than the scope of the declaration) Thx. Al
5
2025
by: Steven T. Hatton | last post by:
If find the following excerpt from the Standard a bit confusing: <quote> 3.3.6 - Class scope -1- The following rules describe the scope of names declared in classes. 1) The potential scope of a name declared in a class consists not only of the declarative region following the name's declarator, but also of all function bodies, default...
1
2572
by: Steven T. Hatton | last post by:
All of the following terms are used in some way to describe where and how a name is relevant to a particular location in a program: visible, declarative region, scope, potential scope, valid, introduced, used, potentially evaluated and accessible. They all seem to have subtle difference in meanings. Any positive contribution to the...
1
3750
by: Giacomo Catenazzi | last post by:
Hello, To learn the details of C, I've build the following example, could you check if it is correct and if it miss some important cases? Are there some useful (real cases) examples of: - "function prototype scope" for structures and unions? - "extern" for internal linkage ?
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7605
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...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
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...
0
6277
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...
1
5501
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
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...
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.