Глобальные переменные си шарп

Глобальные переменные си шарп

Aldo Marchioni said: I just discovered that to write

into a namespace is not allowed.

Any class needs to be public (if so, why the hecck the word «public» is required?)

So, i can’t have variables global to a namespace but not visible outside that namespace.

Congrats, Microsoft eggheads. Strike!

I think the visibility that you are looking for is internal:

This doesn’t protect you across namespaces — an internal class or class member is visible to all of the types in the same assembly (or assemblies declared as friends), regardless of the namespace.

I put static on the class declaration to indicate that it would only contain static members, and no instances should be created. You could of course, leave that off if that is not your intention.

private can be used on classes when they are nested:

Private classes can actually access private members of their containing class, but you need to pass a reference to the inner class if you want to take advantage of this access, because they do not get an implicit reference to their containing class (as I believe inner classes do in Java).

Anyway, you might want to read up on the various C# access modifiers and learn how the work.

Aldo Marchioni said: Thank you Matt.
I was told C# was an ENHANCEMENT to C++ . but i see that at Redmond’s they never change their mind.

And thank you for your kind advice about good programming tecniques. I was studying structured programming tecniques in the seventies, whan you probably attended infant school, if you were born at all.

I could need variables global to a namespace, or to a module a module. And, believe me, there are occasions when a very global variable is needed: if you ever worked at very low level, accessing some shared hardware resource, you would know. But i understand you are very young.

That sounds like a rather rude answer to what was quite a reasonable response. If you are attempting to use global variables in C# then it marks you as a novice .NET programmer, and so it is perfectly fair to give advice that applies to novices. You are not going to be working at a very low level in C#; it’s the wrong tool for the job, and the same rules do not apply.

There are many of us here who have been working with .NET since the alpha versions of 1.0 which means we have a very good understanding of how to develop in this particular environment. Coming across as defensive or rude is not the best way to obtain our help with it.
http://gregbeech.com/blogs/tech

Aldo Marchioni said: I was told C# was an ENHANCEMENT to C++ . but i see that at Redmond’s they never change their mind.

And thank you for your kind advice about good programming tecniques. I was studying structured programming tecniques in the seventies, whan you probably attended infant school, if you were born at all.

You are a rude and unpleasant fellow, eh? Not only that, it seems that you’re programming paradigm seems to be stuck in the 80s.

You need to go back to your little C++ world, and get the hell away from here. 🙂

All replies

If you want a variable to be visible application wide you can use a static variable.

Then anywhere in your application you want to access SomeVariable you just do:
SomeGlobalVariables.SomeVariable.

While this will work, you should try to avoid global variables in an application. It is much more maintainable and clean to keep variables to as local a scope as possible.
-Matt

Thank you Matt.
I was told C# was an ENHANCEMENT to C++ . but i see that at Redmond’s they never change their mind.

And thank you for your kind advice about good programming tecniques. I was studying structured programming tecniques in the seventies, whan you probably attended infant school, if you were born at all.

I could need variables global to a namespace, or to a module a module. And, believe me, there are occasions when a very global variable is needed: if you ever worked at very low level, accessing some shared hardware resource, you would know. But i understand you are very young.

I just discovered that to write

into a namespace is not allowed.

Any class needs to be public (if so, why the hecck the word «public» is required?)

So, i can’t have variables global to a namespace but not visible outside that namespace.

Congrats, Microsoft eggheads. Strike!

If a value is in a namespace then it wouldn’t be visible to others. You have to use:

Also in your example, you can’t have a private class with public variables because how can you access the variables variables when the class is private?

Aldo Marchioni said: I just discovered that to write

into a namespace is not allowed.

Any class needs to be public (if so, why the hecck the word «public» is required?)

So, i can’t have variables global to a namespace but not visible outside that namespace.

Congrats, Microsoft eggheads. Strike!

I think the visibility that you are looking for is internal:

This doesn’t protect you across namespaces — an internal class or class member is visible to all of the types in the same assembly (or assemblies declared as friends), regardless of the namespace.

I put static on the class declaration to indicate that it would only contain static members, and no instances should be created. You could of course, leave that off if that is not your intention.

private can be used on classes when they are nested:

Private classes can actually access private members of their containing class, but you need to pass a reference to the inner class if you want to take advantage of this access, because they do not get an implicit reference to their containing class (as I believe inner classes do in Java).

Anyway, you might want to read up on the various C# access modifiers and learn how the work.

Aldo Marchioni said: Thank you Matt.
I was told C# was an ENHANCEMENT to C++ . but i see that at Redmond’s they never change their mind.

And thank you for your kind advice about good programming tecniques. I was studying structured programming tecniques in the seventies, whan you probably attended infant school, if you were born at all.

I could need variables global to a namespace, or to a module a module. And, believe me, there are occasions when a very global variable is needed: if you ever worked at very low level, accessing some shared hardware resource, you would know. But i understand you are very young.

That sounds like a rather rude answer to what was quite a reasonable response. If you are attempting to use global variables in C# then it marks you as a novice .NET programmer, and so it is perfectly fair to give advice that applies to novices. You are not going to be working at a very low level in C#; it’s the wrong tool for the job, and the same rules do not apply.

There are many of us here who have been working with .NET since the alpha versions of 1.0 which means we have a very good understanding of how to develop in this particular environment. Coming across as defensive or rude is not the best way to obtain our help with it.
http://gregbeech.com/blogs/tech

Aldo Marchioni said: I was told C# was an ENHANCEMENT to C++ . but i see that at Redmond’s they never change their mind.

And thank you for your kind advice about good programming tecniques. I was studying structured programming tecniques in the seventies, whan you probably attended infant school, if you were born at all.

You are a rude and unpleasant fellow, eh? Not only that, it seems that you’re programming paradigm seems to be stuck in the 80s.

You need to go back to your little C++ world, and get the hell away from here. 🙂

Aldo Marchioni said: Thank you Matt.
I was told C# was an ENHANCEMENT to C++ . but i see that at Redmond’s they never change their mind.

And thank you for your kind advice about good programming tecniques. I was studying structured programming tecniques in the seventies, whan you probably attended infant school, if you were born at all.

I could need variables global to a namespace, or to a module a module. And, believe me, there are occasions when a very global variable is needed: if you ever worked at very low level, accessing some shared hardware resource, you would know. But i understand you are very young.

OMG!! I have been known to be defensive before but mate you are just arrogant. Its people like Matt who make this community a pleasure by offering free support to people who need help. Its people like you who leave me shaking my head in dismay. If you cant take a useful suggestion then dont ask for advice.

  • Proposed as answer by Rick at Claymate Saturday, December 8, 2012 3:25 PM
  • Unproposed as answer by Rick at Claymate Saturday, December 8, 2012 3:26 PM

Sorry.. first time user error ! Global variables. I too have read and been told) that C# has no concept of global variable and I can see both sides of this (heated) discussion. I am a 60 year old who has been programming in assembler for the last 45 years, using PIC’s and 6502. I am told I am quite good at it.
In MY application, I THINK I need global variables because I would need to setup a parameter block in one routine and then have other routines look at and modify the parameter block based on the data placed in the block by other routines. I say «think» because I am indeed a novice to C# and a language that doesn’t have a concept of GOTO (or a global variable) is something I need to get my head around. C# solves SOME of my problems but creates more BUT I am prepared to listen and learn. I cannot, for instance (!) understand the concept of a cell phone in my car which does not work in my house because my phone was created in my car and creating an ‘instance’ of «myCell» in my house gives me a new phone but with no data. myHouse = new myCell; To ME. The data in my cell should be ‘global’ that is I can see my numbers wherever I am, though this analogy breaks down as it is the phone that’s global as I clearly take it with me. but you may take my point. No doubt, all will become clear with patience of my ‘tutors’. If you cannot explain the questions of a child in words the child understands, you are not a teacher. best wishes from a novice of 60 years with an understanding of a C# like a child. Maybe this was not the right platform for this but I am certain you are all very bright cookies. Much respect. No offence intended.

The «singleton» pattern is oft criticised in OOP as simply a global variable. In general this pattern should be avoided, because the assumption that «there can only ever be 1 of something» is usually short-sighted. However, last week I encountered a situation where enforcing «exactly one of something» actually is the «correct» implementation. That scenario was the use of Random with a system-generated seed. Because the granularity of system-generated seeds is often less than the speed with which multiple Randoms might be created on start-up, it is actually correct practice to have exatly one instance of Random in an application, as a singleton; that is to say, one global instance wrapped in a singleton class. I do admit to occasionally using singletons and globals inappropriately (in a theoretical sense), but I do so with my eyes open, and with an open mind. «Premature optimization is the root of all evil.» — Knuth If I provoked thought, please click the green arrow If I provoked Aha! please click Propose as Answer We are here to learn, to share knowledge, and to earn points; all in about equal measure.

Here is what I found out by reading and playing around. namespace ricksapplication1 // dos screen display

class Program
public class myGlobalVariables
public static string myName = «Rick»;
public static int myAge = 60;
>
static int multiplicate(int c, int d) // padding for more stuff
return (c * d);
>
static int summof(int a, int b)
return (a + b);
>

static void Main(string[] args)

string temp = myGlobalVariables.myName;
Console.WriteLine(«your name is «,temp);
int tempi = myGlobalVariables.myAge;
Console.WriteLine(«And you are years old»,tempi);
Console.WriteLine(«»); // now mess with the data myGlobalVariables.myName = «Fred Bloggs»; // change the data
myGlobalVariables.myAge = 12;
temp = myGlobalVariables.myName; // make the line shorter
Console.WriteLine(«your name is «,temp);
tempi = myGlobalVariables.myAge;
Console.WriteLine(«And you are years old»,tempi);

.. rest of code. displayed results. your name is Rick And you are 60 years old your name is Fred Bloggs
And you are 12 years old This, my friends is what I call a Global variable and this method (!) does what I want in MY SPECIFIC application, so it’s a solution of perhaps many. Much respect to all.

Источник

Читайте также:  Hi, I'm bold!
Оцените статью