Jump to content
  • Advertisement
Sign in to follow this  
chad_420

C# question

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need a variable to act as if it had been declared static in c++. Whats the best way to do this in c#?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!