Static constructor?
-
I swear I didn't see your reply ;)
V.
(MQOTD rules and previous solutions)Didn't think you did! :laugh:
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Hi! In my base class I want a "constant" FUNC_LENGTH that I assign at creation of my first object. I´d like to have it static. So I tried this:
public abstract class Package
{
private static readonly int funcLength;
protected static int FUNC_LENGTH { get {return funcLength;} }static Package(int _funcLength)
{
funcLength = _funcLength;
}public Package() {}
...
}Obviously this wont work, since a static constructor wont take any parameters. Does this mean it is impossible to make FUNC_LENGTH static? I read in a book that the static constructor is run once "before first use", does that mean it runs when (or immediately before) the first instance of the class is created?
-
Depending on where you're getting the value to set funcLength to, maybe you could read it from a config inside the static constructor?
A config file would work, of course, but the value wont ever change between compilations. It seems more streamlined to just set it via the constructor when creating the package factory class. Then the factory class will pass it to the package instances. I guess a static boolean would work, just thought there would be a more slick way to do it (I actually have more than one of those "constants"). Maybe I just skip the static part. Feels like a defeat though.
-
A config file would work, of course, but the value wont ever change between compilations. It seems more streamlined to just set it via the constructor when creating the package factory class. Then the factory class will pass it to the package instances. I guess a static boolean would work, just thought there would be a more slick way to do it (I actually have more than one of those "constants"). Maybe I just skip the static part. Feels like a defeat though.
If the value wont' change between compilations, why not just make it a
const
value? They are effectively static anyway. Or is it a complex calculation that has to be done at run time? You can't do it as a "prebuild step" or similar? I do something similar to that to time stamp assemblies with the build date / time: Timestamping assemblies with Build date and time.[^]Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
If the value wont' change between compilations, why not just make it a
const
value? They are effectively static anyway. Or is it a complex calculation that has to be done at run time? You can't do it as a "prebuild step" or similar? I do something similar to that to time stamp assemblies with the build date / time: Timestamping assemblies with Build date and time.[^]Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Uhm, didnt think of a const being static, but of course you´re right on that one. Great, const it is!
:thumbsup:
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
If the value wont' change between compilations, why not just make it a
const
value? They are effectively static anyway. Or is it a complex calculation that has to be done at run time? You can't do it as a "prebuild step" or similar? I do something similar to that to time stamp assemblies with the build date / time: Timestamping assemblies with Build date and time.[^]Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
I was too quick there. This is a lib, so when using this lib by creating a Package object, you will pass the value into its constructor. So I cant use a const here.
So it's not fixed at compile time - and you're back to a
static bool
I'm afraid!Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
So it's not fixed at compile time - and you're back to a
static bool
I'm afraid!Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Hi! In my base class I want a "constant" FUNC_LENGTH that I assign at creation of my first object. I´d like to have it static. So I tried this:
public abstract class Package
{
private static readonly int funcLength;
protected static int FUNC_LENGTH { get {return funcLength;} }static Package(int _funcLength)
{
funcLength = _funcLength;
}public Package() {}
...
}Obviously this wont work, since a static constructor wont take any parameters. Does this mean it is impossible to make FUNC_LENGTH static? I read in a book that the static constructor is run once "before first use", does that mean it runs when (or immediately before) the first instance of the class is created?
yeah, the static contructor is run by .net and without arguments ; and the static method runs when the class's instance or static members(method or property) is referred