And the prize for the most stupid error messages goes to....
-
Microsoft C#. I set a function parameter (string myVar = string.Empty) and got: Default parameter value for 'myVar' must be a compile-time constant I fail to see how anything could be more constant than string.Empty.... :laugh:
-
Microsoft C#. I set a function parameter (string myVar = string.Empty) and got: Default parameter value for 'myVar' must be a compile-time constant I fail to see how anything could be more constant than string.Empty.... :laugh:
String.Empty isn't a constant: it's a
static readonly
value. Reference Source[^] As such, it's value could depend on the initialization sequence in theory. Anyway, "" is a lot clearer - and it's a constant! :laugh:Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
Microsoft C#. I set a function parameter (string myVar = string.Empty) and got: Default parameter value for 'myVar' must be a compile-time constant I fail to see how anything could be more constant than string.Empty.... :laugh:
It's not a constant. From the CLI source:
//The Empty constant holds the empty string value.
//We need to call the String constructor so that the compiler doesn't
//mark this as a literal.
//Marking this as a literal would mean that it doesn't show up as a field
//which we can access from native.public static readonly String Empty = "";
98.4% of statistics are made up on the spot.
-
Microsoft C#. I set a function parameter (string myVar = string.Empty) and got: Default parameter value for 'myVar' must be a compile-time constant I fail to see how anything could be more constant than string.Empty.... :laugh:
-
It's not a constant. From the CLI source:
//The Empty constant holds the empty string value.
//We need to call the String constructor so that the compiler doesn't
//mark this as a literal.
//Marking this as a literal would mean that it doesn't show up as a field
//which we can access from native.public static readonly String Empty = "";
98.4% of statistics are made up on the spot.
If it is set to readonly so can never change, doesn't that make it constant?
-
Microsoft C#. I set a function parameter (string myVar = string.Empty) and got: Default parameter value for 'myVar' must be a compile-time constant I fail to see how anything could be more constant than string.Empty.... :laugh:
You should give your variables meaningful names.
-
If it is set to readonly so can never change, doesn't that make it constant?
No. A constant isn't actually a variable, it just looks like one. When you write this
const string x = "Hello";
if (x == "Hello")
{
Console.WriteLine (x);
}what gets compiled is this;
if ("Hello" == "Hello")
{
Console.WriteLine ("Hello");
}The compiler does a "find and replace" and in-lines the value of the constant. That is why a constant can't be an object.
const Person p = new Person();
p.Firstname = "John";
Console.WriteLine(p); // what can the compiler replace "p" with?? -
If it is set to readonly so can never change, doesn't that make it constant?
They work very differently and you can nuke a readonly with reflection. (and with
unsafe
, and in some odd corner cases with structs) A constant is so constant that even if you have some constant in a DLL and you change it/recompile/replace the DLL, the application that uses the DLL still uses the old value. You also can't even try to change them with reflection, there is nothing there to attempt to write to. A readonly is not directly writable, in all other ways it's a normal field. -
String.Empty isn't a constant: it's a
static readonly
value. Reference Source[^] As such, it's value could depend on the initialization sequence in theory. Anyway, "" is a lot clearer - and it's a constant! :laugh:Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
String.Empty isn't a constant: it's a
static readonly
value. Reference Source[^] As such, it's value could depend on the initialization sequence in theory. Anyway, "" is a lot clearer - and it's a constant! :laugh:Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
So "" != String.Empty ? :laugh:
Now is it bad enough that you let somebody else kick your butts without you trying to do it to each other? Now if we're all talking about the same man, and I think we are... it appears he's got a rather growing collection of our bikes.
-
You should give your variables meaningful names.
-
If it is set to readonly so can never change, doesn't that make it constant?
Maybe we should have a modifier "SetOnceReadMany", to describe something that is not a "constant" in the sense of a literal, but will always return the same value when read. At least in a given run it returns the same value - not necessarily under varying conditions, such as moving your code to another operating system. The value is not modifiable (hence 'constant'), but depends on the OS (or some similar external condition.)
-
So "" != String.Empty ? :laugh:
Now is it bad enough that you let somebody else kick your butts without you trying to do it to each other? Now if we're all talking about the same man, and I think we are... it appears he's got a rather growing collection of our bikes.
Nope, it's much better! :laugh:
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
Maybe we should have a modifier "SetOnceReadMany", to describe something that is not a "constant" in the sense of a literal, but will always return the same value when read. At least in a given run it returns the same value - not necessarily under varying conditions, such as moving your code to another operating system. The value is not modifiable (hence 'constant'), but depends on the OS (or some similar external condition.)
-
Nope, it's much better! :laugh:
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
F-ES Sitecore wrote:
You should give your variables meaningful names.
I'm sure it was just an example but how do you know it's not meaningful anyway?
Sin tack the any key okay
It was only a joke :)
-
It was only a joke :)
-
Does not help. It still needs to store the string somewhere, even if the string is "" and once it does that, (1) what's stored at that location can be externally modified after compilation and/or during execution.
Sin tack the any key okay
Sure, even if the value is burnt into a ROM, you can unsolder that ROM and put another one in place. So you can set Environment.NewLine to "xyzzy", if that satisfies you one way or the other. But that will neither change the newline convention in Windows, *nix or OSX. I've seen people, in classic K&R C, set up #define to give FALSE the value 1 and TRUE the value 0. And I've heard of people programming in Forth to set the value of 3 to 17 - in that language, the literal 3 is a symbol that just happens to initially have the value of e.g. the count of x-es in the string "xxx", but it can be redefined som something else. Aa a code obfuscation mechanism, it is of course great. I think that when you work in a given language, your task should be solved in that language, using the abstractions and concepts in that language. If it says that a value is constant, it IS constant for that problem solution. Just like you can go behind the compiler's back and change a contant value, you can also open an .exe or .dll file before it is run and change a "greater than" conditional jump to a "greater that or equal to" conditional jump. So the loop termination, while appearing to be constant at the source code level, is actually varying depending on which modification are made to the executable code file. If you start digging a huge hole in the ground underneath your house, your house will fall into it. You asked for it, you got it.
-
Wait, what? You know you can't get high on diesel. Or are we still excited with that extra 40% per gallon by switching fuels?
Sin tack the any key okay
Lopatir wrote:
You know you can't get high on diesel.
Well, technically.. [Continental Diesel](http://www.continentaldiesel.com/typo3/index.php?id=2&L=1) :laugh:
Now is it bad enough that you let somebody else kick your butts without you trying to do it to each other? Now if we're all talking about the same man, and I think we are... it appears he's got a rather growing collection of our bikes.