473,795 Members | 3,002 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The equivalent of Global constants in VB

Hi,

Another stupid newbie question from me, I'm sorry to say...

but can anyone tell me how to simulate the concept of a global constant in a
C# Windows app? The app in question contains several forms, each of which
need to interrogate the value of a "global" constant. Do I have to create a
class with a public constant declaration and instantiate that class from
each form?

Any assistance gratefully received.

Best regards,

Mark Ra
Nov 15 '05 #1
8 2177
You wouldn't have to instantiate the class since a public const would be
static too.
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
For a laugh, try web browsing with Opera's User Mode and Nostalgia enabled
Nov 15 '05 #2
Mark Rae <ma**@markrae.c o.uk> wrote:
Another stupid newbie question from me, I'm sorry to say...

but can anyone tell me how to simulate the concept of a global constant in a
C# Windows app? The app in question contains several forms, each of which
need to interrogate the value of a "global" constant. Do I have to create a
class with a public constant declaration and instantiate that class from
each form?


Create a class with a static readonly or const value (depending on
type). For instance:

public class Constants
{
const int OneAndOne = 2;
}

You'd then access that with:

Constants.OneAn dOne

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
n!
> but can anyone tell me how to simulate the concept of a global constant in
a
C# Windows app? The app in question contains several forms, each of which
need to interrogate the value of a "global" constant. Do I have to create a class with a public constant declaration and instantiate that class from
each form?


You need to declare a class, but no need to instantiate it anywhere. The
constants become available by making them public static members. e.g.

namespace MyAppNamespace
{
public sealed class MyGlobalConstan ts
{
private MyGlobalConstan ts() // Prevent instantiation
{
}

public static const int ConstantOne = 53;
public static const int ConstantTwo = 56;
public static const string StringConstant = "A
Constant";
}
}

Which may be accessible from anywhere in your app via:

namespace MyAppNamespace
{
public class MyClass
{
private int myValue;
private string myString;

public MyClass()
{
// Access a constant...
myValue = MyGlobalConstan ts.ConstantOne;

myString = MyGlobalConstan ts.StringConsta nt;
}
}
}

static members (shared in VB) do not require a class instance in order to be
accessed.

Note, if the constants are all integer values an enumeration may be a better
choice.

n!
Nov 15 '05 #4
n!
And yes, I just remembered const values are implicitly static, so no need to
use the static keyword (gah). :)

n!
Nov 15 '05 #5
"n!" <nf********@nom ailplease.com> wrote in message
news:eS******** ******@TK2MSFTN GP10.phx.gbl...
You need to declare a class, but no need to instantiate it anywhere. The
constants become available by making them public static members. e.g.


Perfect! Thanks very much (and to the other people who replied)
Nov 15 '05 #6
n! <nf********@nom ailplease.com> wrote:
but can anyone tell me how to simulate the concept of a global constant in

a
C# Windows app? The app in question contains several forms, each of which
need to interrogate the value of a "global" constant. Do I have to create

a
class with a public constant declaration and instantiate that class from
each form?


You need to declare a class, but no need to instantiate it anywhere. The
constants become available by making them public static members. e.g.

namespace MyAppNamespace
{
public sealed class MyGlobalConstan ts
{
private MyGlobalConstan ts() // Prevent instantiation
{
}

public static const int ConstantOne = 53;
public static const int ConstantTwo = 56;
public static const string StringConstant = "A
Constant";
}
}


Slight error here: consts can't be marked as static because they *have*
to be static.

(They can be private, however - my own sample post mucked up in this
respect; the constant should have been marked as public.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Slight error here: consts can't be marked as static because they *have*
to be static.


Yeah - it genereated an error, so I just removed the word "static" and it's
working fine. Thanks.
Nov 15 '05 #8
n!
> Slight error here: consts can't be marked as static because they *have*
to be static.


Sorry yes, I did post a correction! I realised the mistake just after the
post was sent.

n!
Nov 15 '05 #9

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

Similar topics

1
4372
by: mark4asp | last post by:
What are the best methods for using global constants and variables? I've noticed that many people put all global constants in a file and include that file on every page. This is the best way of doing it - is it not? Once the application has loaded the page it is cached and is immediately available for other pages. With global variables - the best thing to do would be to use application variables - so long as there weren't too many...
10
17884
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can overwrite another copies global variable? I know that you could overwrite a value in a global variable from a function, but you could also do that if you pass the variable in and then out again... so how is that any different?
20
4504
by: 2obvious | last post by:
I've been trying to create read-only global variables by creating constants (Const) in my global.asa, but I can't seem to reference them. Sticking them in an include works fine, but it seems more structurally sound to use Application_OnStart. Am I attempting the impossible, and if so, why?
25
66084
by: Daniel Bass | last post by:
how do i declare a global variable in c#.net? it's like it want's everything in classes... there are times when globals are good, like having constants in a program which apply to several layers/objects/etc... or does it expect me to create a singleton global class structure? surely it's not that terrible.
4
51179
by: Amadelle | last post by:
Hi all and thanks again in advance, What is the best way of defining global constants in a C# application? (A windows application with no windows forms - basically a set of classes). Would it be a wise idea to create a clsCommonApp and let all other classes to be derived from that class? and define all constants in that base class? Any other suggestions are more than welcome. (BTW this is not for one constant, I have multiple...
8
2891
by: Marty | last post by:
Hi, I'm new to C#, I used to code in VB.NET. Where is the best place to declare all my constants and global objects in my C# project to have them accessible globally? I have an event logger class that I want its instance to be accessible from any other classe in the project. There is also a bunch of constants that I want to be public for the
1
3026
by: 2obvious | last post by:
I want to declare some constants on the application level in global.asax to use throughout my application, e.g.: Sub Application_OnStart() Const NUM As Integer = 5 End Sub Problem is, when I do it this way, the scope of the constants is local--attempting to use these constants in a typical .aspx file throws an error telling me that 'NUM' is undeclared. Doing this:
8
5675
by: Thomas Coleman | last post by:
Ok, I've obviously discovered that Global.aspx has been completely changed in ..NET 2.0. However, I haven't figured out how to declare a constant that's available to any page in my application without having to jump through a bunch of hoops. First, let me layout how it worked in 1.1. In the Global.asax, within the Global class construct, I would simply add something like: public const string FOO = "foo";
6
1633
by: lazy | last post by:
hi, I have some constants defined in a php script say config.php. I want to use the variables there defined in other scripts. couple of questions regd that: 1. Is there an alternative to including config.php and declaring the variables that will be used as global. This seems very inefficient. 2.Moreover these variables are constants, is there a way to make the variables unmodifiable in config.php so that scripts that use them ,
0
10439
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...
0
10215
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10165
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
9043
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...
1
7541
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
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
5437
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...
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.