Static constructor?
-
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?
-
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?
-
What you probably want is a Singleton design: see here[^]
V.
(MQOTD rules and previous solutions)No, I dont think so. My class is an abstract base class (I just updated the code above). So I will actually have several instances of this class subclasses. Its part of a communication protocol, so I want to define this specific package field size (FUNC_LENGTH) before creation of the first package. After that there must not be any way to change that size, or the communication will break down. The singleton is, as I know it, a class that can only have one single instance. Correct me if Im wrong.
-
No, I dont think so. My class is an abstract base class (I just updated the code above). So I will actually have several instances of this class subclasses. Its part of a communication protocol, so I want to define this specific package field size (FUNC_LENGTH) before creation of the first package. After that there must not be any way to change that size, or the communication will break down. The singleton is, as I know it, a class that can only have one single instance. Correct me if Im wrong.
ok, I misunderstood, I would probably have a private static boolean that indicates if the func_length was set or not. In that case you may pass anything you want, but only the first will update the property. I'm sure there are better solutions, but I think it would work and it is pretty simple.
V.
(MQOTD rules and previous solutions) -
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?
It's not impossible, but it is nasty. Very nasty! As you say, you can't have a static constructor with a parameter - because static constructors are never called from your code directly, but by the system immediately prior to the first instance being created, so it can't call a parametrized one. So it has to be a parameter to an instance constructor: which means a check is needed:
public abstract class Package { protected static int FUNC\_LENGTH { get; private set; } private static bool onceAlready = false; public Package(int \_funcLength) { if (!onceAlready) { FUNC\_LENGTH = \_funcLength; onceAlready = true; } } ... }
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
ok, I misunderstood, I would probably have a private static boolean that indicates if the func_length was set or not. In that case you may pass anything you want, but only the first will update the property. I'm sure there are better solutions, but I think it would work and it is pretty simple.
V.
(MQOTD rules and previous solutions)Snap! :laugh:
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Snap! :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?
-
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...
-
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