C# question

Started by
16 comments, last by joanusdmentia 18 years, 9 months ago
I need a variable to act as if it had been declared static in c++. Whats the best way to do this in c#?
Advertisement
Maybe I'm missing something, but have you tried "static"?
Turring Machines are better than C++ any day ^_~
Have you ever used c#? Of course I tried static. If you look at the documentation it doesn't really do the same thing as in c++ atleast in this context.
Quote:Original post by chad_420
Have you ever used c#? Of course I tried static. If you look at the documentation it doesn't really do the same thing as in c++ atleast in this context.

Yes, I use C# every day. I've used the .Net Framework since it was in alpha many years ago. You haven't given me a context, so until you do I'm going to continue to think that static means "a variable that is accessable across all instances of a single class with only a single value" until you say something more. If that is what you mean, C# has exactly that.
Turring Machines are better than C++ any day ^_~
Depends on what static behavior you want. A static at a file level or local scope I don't think there is a direct equivalent in C#. Static at a class level works about the same. Since everything in C# needs a class probably declaring a variable or static variable, depending on the behavior you want, at the class level I think is going to be your best bet. Hard to say without knowing what exactly you are doing.
---CyberbrineDreamsSuspected implementation of the Windows idle loop: void idle_loop() { *((char*)rand()) = 0; }
Yeah, or they could have just told you:

const

Like

public const int AnInteger = 10;
here a c++ example.
class SomeThing{    void myFunc()    {        static int a = 0;        cout<<a;        a++;        if(a > 10) return;        myFunc();    }};


to recursivly print out 0 - 10, I'd like to do that in c# without a belonging to something. That is how a static c++ variable behaves. Is that clearer?

Quote:You haven't given me a context, so until you do I'm going to continue to think that static means "a variable that is accessable across all instances of a single class with only a single value"


"as if it had been declared static in c++", seems like enough info to me.
Quote:Original post by chad_420
here a c++ example.
class SomeThing{    void myFunc()    {        static int a = 0;        cout<<a;        a++;        if(a > 10) return;        myFunc();    }};



I'm sorry but you do not need to use recursive functions for that. It can simply be done using a loop:

void Foo(){  int a=0;  while (a < 10)  {    a++;    cout << a << endl;  }  return;}
Quote:Original post by chad_420That is how a static c++ variable behaves. Is that clearer?


Don't be so pissy. Static in c++ is an overloaded term, which can refer to one of the many following uses:

1) As declared in a function - will retain its value from function call to function call. Basically a global only accessible from that function.

2) As a member of a class. One instance of the variable will be created for that class, and you won't need an instance of the class to access it.

3) As declared at namespace scope - the C meaning - this variable won't be visible outside the current file.

Since you didn't clarify that you meant #1, and as c# supports #2 but not 1 and 3, it is quite reasonable to ask for clarification.
Alrighty guys.

This topic is closed to new replies.

Advertisement