Need help with global sttrings
-
Can anybody shed some light. I'm a bit confused. Still a noob at C#. I read that Global variables are not supported in C# yet it seems to work for. They recommend you use struct instead but I can't seem to get it to work for me. I'm having a hard time understanding to program a string that can be accessed by any function without having to pass the String contents to the function. Basiccaly I need to access some strings globally. Any suggestions? :^)
-
Can anybody shed some light. I'm a bit confused. Still a noob at C#. I read that Global variables are not supported in C# yet it seems to work for. They recommend you use struct instead but I can't seem to get it to work for me. I'm having a hard time understanding to program a string that can be accessed by any function without having to pass the String contents to the function. Basiccaly I need to access some strings globally. Any suggestions? :^)
Make the strings static members of a class (eg:
StringHolder
) and refer to them from any class in the following manner:public class StringHolder
{
public static readonly string SomeString = "hello";
}public class ReferringClass
{
void someMethod()
{
// foo gets reversed version of SomeString
string foo = StringHolder.SomeString.Reverse();
}/ravi
This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com
-
Can anybody shed some light. I'm a bit confused. Still a noob at C#. I read that Global variables are not supported in C# yet it seems to work for. They recommend you use struct instead but I can't seem to get it to work for me. I'm having a hard time understanding to program a string that can be accessed by any function without having to pass the String contents to the function. Basiccaly I need to access some strings globally. Any suggestions? :^)
There are global variables in C#. Just create them outside the functions. For instance:
public class Circle
{
double radius = 0.0;
double pi = 3.14;public Circle(double Radius)
{
radius = Radius;
}public double Area()
{
//use global variable.
return pi*Math.Pow(radius, 2);
}
}Best regards! Larantz
for those about to code, we salute you
http://www.tellus-software.com -
There are global variables in C#. Just create them outside the functions. For instance:
public class Circle
{
double radius = 0.0;
double pi = 3.14;public Circle(double Radius)
{
radius = Radius;
}public double Area()
{
//use global variable.
return pi*Math.Pow(radius, 2);
}
}Best regards! Larantz
for those about to code, we salute you
http://www.tellus-software.com -
There are global variables in C#. Just create them outside the functions. For instance:
public class Circle
{
double radius = 0.0;
double pi = 3.14;public Circle(double Radius)
{
radius = Radius;
}public double Area()
{
//use global variable.
return pi*Math.Pow(radius, 2);
}
}Best regards! Larantz
for those about to code, we salute you
http://www.tellus-software.comYes I know that works. I been programming that way all along but I just upgraded to Vista and downloaded the latest C# version. One of the note I read claimed it does not support it so I'm weary programming that way in xase it sops working in the future. See this link http://tinyurl.com/29o64p http://tinyurl.com/29o64p[^]