preserve retain variable between function calls #8038
Replies: 5 comments 14 replies
-
What's wrong with just having a class level static variable? |
Beta Was this translation helpful? Give feedback.
-
Yes - sure - that would work. but C# disallows this but we can make a class variable instead. But isn't this a forum for saying we want more??! In a large class I don't want the confusion of In C# I always end up sticking it below the function like this: class MyClass void SecondFunction() .... and to edit my own writings... having a static is not the same as (what if you want multiple instances of the class, each with one of these....) So I want this feature in C++ as well ! |
Beta Was this translation helpful? Give feedback.
-
Try a mini struct instead: code at SharpLab using static System.Console;
var mc1 = new MyClass ();
var mc2 = new MyClass ();
mc1.SecondFunction ();
mc1.SecondFunction ();
mc1.SecondFunction ();
mc2.SecondFunction ();
mc2.SecondFunction ();
var vmc1 = mc1.GetVar;
var vmc2 = mc2.GetVar;
WriteLine ($"mc1 should be 3: {vmc1}");
WriteLine ($"mc2 should be 2: {vmc2}");
public class MyClass {
public void FirstFunction () { }
struct SecondFunctor {
int varThatKeepsItsValueBetweenCalls;
internal void SecondFunction () {
varThatKeepsItsValueBetweenCalls++;
}
public int GetVar => varThatKeepsItsValueBetweenCalls;
}
SecondFunctor sf;
public void SecondFunction () => sf.SecondFunction ();
public int GetVar => sf.GetVar;
} This wraps the 'SecondFunction' and its 'varThatKeepsItsValueBetweenCalls' in an embedded struct 'SecondFunctor'. If you check out the generated code (click SharpLab link above) in release mode, you will notice that the Jitter inlined the mini struct 'SecondFunctor', its variable 'varThatKeepsItsValueBetweenCalls' and the struct member function 'SecondFunction'. But in the source, you have full control about who has access to what. And in C/C++ it should work too. |
Beta Was this translation helpful? Give feedback.
-
No, you cannot. public void ThirdFunction () {
var x = sf.varThatKeepsItsValueBetweenCalls;
// error CS0122: 'MyClass.SecondFunctor.varThatKeepsItsValueBetweenCalls' is inaccessible due to its protection level
}
That is an indirect access through struct member function 'SecondFunctor.SecondFunction ()', so it is a controlled access.
No, its not only visual - the compiler blocks any access from outside of nested struct 'secondFunctor'. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Is there (I'd like) an easy way to preserve a variable value between function calls ?
not a class variable declared outside the function.
And not wrapped in something like a mini class.
But something simple and non-verbose.
Like
class MyClass
{
void AddFunction()
{
preserved int thisIntKeepsItsValue = 1;
//
thisIntKeepsItsValue++;
}
}
Because I want the scope of it limited to the function.
My invented 'preserved' keyword here would give it the life-time of the class too,
ie it wouldn't be 'static'
Beta Was this translation helpful? Give feedback.
All reactions