all groups > dotnet general > december 2004 >
How can I have a variable that has been initialized and set to a value within one source file (*.cs), and have access to that same variable in other files of the same project? NOTE: That singleton stuff won't work for me, I need it to work like it does in most every other development lanagauge I've ever used...or at the very least, something close to how you do it in C++ (by using "extern"). Also, if it can't be done, then state that as well. Thanks
Well maybe you can answer this question for me. If I create a class, and have that class hold all of my global information...(the information in that global class would be references to other classes (that I would how instantiated and then assigned to a variable within the global class), and the global class would also hold normal variables (properties) that I would set to some value. How can I access this "global" class from other *.cs file within my project? Thanks [quoted text, click to view] "Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message news:MPG.1c260a33e502af5898baac@msnews.microsoft.com... > If singletons or static variables aren't good enough, then what you > want can't be done. There's no such concept as a global variable which > doesn't live in any particular class.
Thank you Jon and Strong...I was able to accomplish what I needed. Thanks for taking the time to answer my question, instead of telling me to goto a different newsgroup. This language is very new to MOST of us and with people like (Jon & Strong) this will help us with the transition. [quoted text, click to view] "Str0nG" <str0ng@caths.co.uk> wrote in message news:3231c3F3hnuieU1@individual.net... > Relaxin wrote: > >> Well maybe you can answer this question for me. >> >> If I create a class, and have that class hold all of my global >> information...(the information in that global class would be references >> to other classes (that I would how instantiated and then assigned to a >> variable within the global class), and the global class would also hold >> normal variables (properties) that I would set to some value. >> >> How can I access this "global" class from other *.cs file within my >> project? >> > > I am not sure what you want to achive, but you can use public classes, > public properties and/or static properties. For example to access an > Integer i using the public property MyInt you can use something like > > public class Myclass > { > int i; > > public int MyInt > { > get { return this.i; } > set { this.i = value; } > } > } > > and instantiate Myclass > > Myclass m = new Myclass(); > m.MyInt = 10; > > and assign the Integer a value (which could be accomplished using a proper > constructor, of course). > > Another way is to use static properties: > > public class Myclass > { > static int myInt = 10; > > public static int MyInt > { > get { return myInt; } > set { myInt = value; } > } > } > > and use it without creating an instance: > > MyClass.MyInt = 5; > Console.WriteLine("MyInt: {0}", MyClass.MyInt); > > I think this is the easiest way to make data structures "globally" > available. > > Hope this helps. > > \Str0nG
[quoted text, click to view] Relaxin <me@yourhouse.com> wrote: > How can I have a variable that has been initialized and set to a value > within one source file (*.cs), and have access to that same variable in > other files of the same project? > > NOTE: That singleton stuff won't work for me, I need it to work like it does > in most every other development lanagauge I've ever used...or at the very > least, something close to how you do it in C++ (by using "extern"). > > Also, if it can't be done, then state that as well.
If singletons or static variables aren't good enough, then what you want can't be done. There's no such concept as a global variable which doesn't live in any particular class. -- Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet
[quoted text, click to view] Relaxin <me@yourhouse.com> wrote: > Well maybe you can answer this question for me. > > If I create a class, and have that class hold all of my global > information...(the information in that global class would be references to > other classes (that I would how instantiated and then assigned to a variable > within the global class), and the global class would also hold normal > variables (properties) that I would set to some value. > > How can I access this "global" class from other *.cs file within my project?
I *think* you're asking about how you access static variables/properties in a different way from instance variables/properties. If so, you just use the classname. For instance, the static ASCII property of Encoding is accessed with: Encoding e = Encoding.ASCII; (in C#). -- Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet
[quoted text, click to view] Relaxin wrote: > Well maybe you can answer this question for me. > > If I create a class, and have that class hold all of my global > information...(the information in that global class would be references to > other classes (that I would how instantiated and then assigned to a variable > within the global class), and the global class would also hold normal > variables (properties) that I would set to some value. > > How can I access this "global" class from other *.cs file within my project? >
I am not sure what you want to achive, but you can use public classes, public properties and/or static properties. For example to access an Integer i using the public property MyInt you can use something like public class Myclass { int i; public int MyInt { get { return this.i; } set { this.i = value; } } } and instantiate Myclass Myclass m = new Myclass(); m.MyInt = 10; and assign the Integer a value (which could be accomplished using a proper constructor, of course). Another way is to use static properties: public class Myclass { static int myInt = 10; public static int MyInt { get { return myInt; } set { myInt = value; } } } and use it without creating an instance: MyClass.MyInt = 5; Console.WriteLine("MyInt: {0}", MyClass.MyInt); I think this is the easiest way to make data structures "globally" available. Hope this helps.
Relaxin, Did you know that there is a newsgroup microsoft.public.dotnet.languages.csharp Global variables are in every dotnet programlanguage a little bit else. Cor
"Relaxin" <me@yourhouse.com> schreef in bericht news:OBPtVkF4EHA.1392@tk2msftngp13.phx.gbl... [quoted text, click to view] > Thank you Jon and Strong...I was able to accomplish what I needed. > > Thanks for taking the time to answer my question, instead of telling me to > goto a different newsgroup. > > This language is very new to MOST of us and with people like (Jon & > Strong) this will help us with the transition. >
Can you tell who told you *to go* to a different newsgroup? Because it seems that there are messages missing which I don't have in this thread. I did not see that one. Cor
[quoted text, click to view] Cor Ligthert wrote: > "Relaxin" <me@yourhouse.com> schreef in bericht > news:OBPtVkF4EHA.1392@tk2msftngp13.phx.gbl... > >>Thank you Jon and Strong...I was able to accomplish what I needed. >> >>Thanks for taking the time to answer my question, instead of telling me to >>goto a different newsgroup. >> >>This language is very new to MOST of us and with people like (Jon & >>Strong) this will help us with the transition. >> > > Can you tell who told you *to go* to a different newsgroup? > > Because it seems that there are messages missing which I don't have in this > thread. > > I did not see that one.
You didn't? What about this one? Message ID ura31OF4EHA.1392@tk2msftngp13.phx.gbl [quoted text, click to view] > Relaxin, > > Did you know that there is a newsgroup > > microsoft.public.dotnet.languages.csharp > > Global variables are in every dotnet programlanguage a little bit else. > > Cor
May be there is another Cor Ligthert sending 50% of posters to other sources of information ;-) Cheers Catherine
"Catherine Lowery" [quoted text, click to view] > > > Did you know that there is a newsgroup > > > > microsoft.public.dotnet.languages.csharp > > > > Global variables are in every dotnet programlanguage a little bit else. > > > > Cor
Does that mean for you that I wrote "to goto a different newsgroup". I thought that after his messages to Jon, where he obviously did not direct understand Jon and I am not the one who can help, because it is C# what is a speciality from Jon. In VBNet I could have told it him in 2 sentences. However when Jon does not succeed in C#, than in my opinion should I not try it, but there could have been somebody else who could explain in more in Relaxins words. And the best place is than in my opinion the place I showed. And so it was, you can probably see that the message from Strong came almost at the same time as mine. However it is not me to tell that he "had to go" and in my opinion did I not write that in my words at all, however maybe can you point me on where I did that? Sometimes people read what they want to read while it is not written. Cor
[quoted text, click to view] Cor Ligthert wrote: > "Catherine Lowery" > > > >>>Did you know that there is a newsgroup >>> >>>microsoft.public.dotnet.languages.csharp >>> >>>Global variables are in every dotnet programlanguage a little bit else. >>> >>>Cor > > > Does that mean for you that I wrote "to goto a different newsgroup". > > I thought that after his messages to Jon, where he obviously did not direct > understand Jon and I am not the one who can help, because it is C# what is a > speciality from Jon. In VBNet I could have told it him in 2 sentences. > However when Jon does not succeed in C#, than in my opinion should I not try > it, but there could have been somebody else who could explain in more in > Relaxins words. And the best place is than in my opinion the place I showed. > > And so it was, you can probably see that the message from Strong came almost > at the same time as mine. > > However it is not me to tell that he "had to go" and in my opinion did I not > write that in my words at all, however maybe can you point me on where I did > that? > > Sometimes people read what they want to read while it is not written. > > Cor
OK, we (you, Jon, Strong, and me) know that the concept of static/public variables is not a matter of language. It's a .Net thing. From this perspective this is the right place to ask. C# knows the public and static keywords, VB on the other hand works almost the same way using the Public Shared construct. Relaxin, that's how I read his/her question, wanted to know how "globals" can be employed in a language he/she knows: C#. I don't want to drive this thread into an off-topic discussion, but please don't get me wrong: I browsed through a couple of your postings realizing that you redirected people to other groups or websites quite often. So, yes, your last posting suggested that you actually meant _post to another group_. Feel free to point to additional sources of information that help people to get a better understanding, which is really OK. I just couldn't resist to place a reply on your little complaint that you might have been misunderstood. Please take it with the same sense of humor as I did. Happy posting!
Catherine, When you see how I did it you can see that I took the same approach as you. However there is AFAIK a difference between static in C# and VBNet. The keyword "shared" is in VBNet the same as in C(#) the keyword "static". However has the keyword "static" in VBNet another meaning than in C# while it declares a static variable inside a method. (And that is probably the reason why shared is named as it is, because that static existed already in VB classic and stayed in VBNet). Cor "Catherine Lowery" <clowery@imap.cc> [quoted text, click to view] > >> "Catherine Lowery" >> > >> >>>>Did you know that there is a newsgroup >>>> >>>>microsoft.public.dotnet.languages.csharp >>>> >>>>Global variables are in every dotnet programlanguage a little bit else. >>>> >>>>Cor >> >> >> Does that mean for you that I wrote "to goto a different newsgroup". >> >> I thought that after his messages to Jon, where he obviously did not >> direct understand Jon and I am not the one who can help, because it is C# >> what is a speciality from Jon. In VBNet I could have told it him in 2 >> sentences. However when Jon does not succeed in C#, than in my opinion >> should I not try it, but there could have been somebody else who could >> explain in more in Relaxins words. And the best place is than in my >> opinion the place I showed. >> >> And so it was, you can probably see that the message from Strong came >> almost at the same time as mine. >> >> However it is not me to tell that he "had to go" and in my opinion did I >> not write that in my words at all, however maybe can you point me on >> where I did that? >> >> Sometimes people read what they want to read while it is not written. >> >> Cor > > OK, we (you, Jon, Strong, and me) know that the concept of static/public > variables is not a matter of language. It's a .Net thing. From this > perspective this is the right place to ask. C# knows the public and static > keywords, VB on the other hand works almost the same way using the Public > Shared construct. Relaxin, that's how I read his/her question, wanted to > know how "globals" can be employed in a language he/she knows: C#. I > don't want to drive this thread into an > off-topic discussion, but please don't get me wrong: I browsed through a > couple of your postings realizing that you redirected people to other > groups or websites quite often. So, yes, your last posting suggested that > you actually meant _post to another group_. Feel free to point to > additional sources of information that help people to get a better > understanding, which is really OK. I just couldn't resist to place a > reply on your little complaint that you might have been misunderstood. > Please take it with the same sense of humor as I did. > > Happy posting! > > Catherine
Hello Relaxin, I'm glad you found your solution. I hope that you will continue to learn the C# language. In particular, I hope that you get a chance to explore some of the more object-oriented aspects of the language that may help you to work around the need for global variables in the future. Sure, globals are still sometimes useful. However, they also increase the unintentional coupling that occurs when an object or value can be modified from anywhere, and can be accessed from anywhere. As your designs improve, I'm hoping that you will see the utility of reducing this kind of coupling, which can make your code more maintainable over time. This is not a criticism. Just a little advice. Good luck in your efforts and welcome to .Net, --- Nick [quoted text, click to view] "Relaxin" <me@yourhouse.com> wrote in message news:uXw5ErS4EHA.3336@TK2MSFTNGP11.phx.gbl... > Your solution would require me to instantiate the class within every *.cs > file that I need to use it. > > Jon and Strong have provided me with a usable solution. > > Thanks > > "Ravichandran J.V." <jvravichandran@yahoo.com> wrote in message > news:uDqeAeN4EHA.3368@TK2MSFTNGP10.phx.gbl... > > Add New Class by right clicking on the Project Name in the Solution > > Explorer. This class will be added as "class1.cs". In this class, > > declare your variable; instantiate the class in whichever file you need. > > > > with regards, > > > > > > J.V.Ravichandran > > - http://www.geocities.com/ > > jvravichandran > > - http://www.411asp.net/func/search? > > qry=Ravichandran+J.V.&cob=aspnetpro > > - http://www.southasianoutlook.com > > - http://www.MSDNAA.Net > > - http://www.csharphelp.com > > - http://www.poetry.com/Publications/ > > display.asp?ID=P3966388&BN=999&PN=2 > > - Or, just search on "J.V.Ravichandran" > > at http://www.Google.com > > > > *** Sent via Developersdex http://www.developersdex.com *** > > Don't just participate in USENET...get rewarded for it! > >
Nick, My fingers every time wanted to make this kind of answer in this thread, however for two reasons I could not do that in this thread. (I had to think on classic Cobol style programming) Nice you did it. :-) Cor "Nick Malik" <nickmalik@hotmail.nospam.com> [quoted text, click to view] > Hello Relaxin, > > I'm glad you found your solution. > > I hope that you will continue to learn the C# language. > > In particular, I hope that you get a chance to explore some of the more > object-oriented aspects of the language that may help you to work around > the > need for global variables in the future. Sure, globals are still > sometimes > useful. However, they also increase the unintentional coupling that > occurs > when an object or value can be modified from anywhere, and can be accessed > from anywhere. As your designs improve, I'm hoping that you will see the > utility of reducing this kind of coupling, which can make your code more > maintainable over time. > > This is not a criticism. Just a little advice. > > Good luck in your efforts and welcome to .Net, > --- Nick > > "Relaxin" <me@yourhouse.com> wrote in message > news:uXw5ErS4EHA.3336@TK2MSFTNGP11.phx.gbl... >> Your solution would require me to instantiate the class within every *.cs >> file that I need to use it. >> >> Jon and Strong have provided me with a usable solution. >> >> Thanks >> >> "Ravichandran J.V." <jvravichandran@yahoo.com> wrote in message >> news:uDqeAeN4EHA.3368@TK2MSFTNGP10.phx.gbl... >> > Add New Class by right clicking on the Project Name in the Solution >> > Explorer. This class will be added as "class1.cs". In this class, >> > declare your variable; instantiate the class in whichever file you >> > need. >> > >> > with regards, >> > >> > >> > J.V.Ravichandran >> > - http://www.geocities.com/ >> > jvravichandran >> > - http://www.411asp.net/func/search? >> > qry=Ravichandran+J.V.&cob=aspnetpro >> > - http://www.southasianoutlook.com >> > - http://www.MSDNAA.Net >> > - http://www.csharphelp.com >> > - http://www.poetry.com/Publications/ >> > display.asp?ID=P3966388&BN=999&PN=2 >> > - Or, just search on "J.V.Ravichandran" >> > at http://www.Google.com >> > >> > *** Sent via Developersdex http://www.developersdex.com *** >> > Don't just participate in USENET...get rewarded for it! >> >> > >
Don't see what you're looking for? Try a search.
|
|
|