VERIFICATION_CODE is a const so VERIFICATION_CODE can
not appear on the LEFT side of an assignment.
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
Marked as answer by Mark - MSFT on Nov 20, 2012 04:01 AM
The following line will create a constant string that will represent the FORMAT-ONLY for some string that will be use later:
VERIFICATION_CODE = "User with Email ID: {0} has requested for the verfication Code.";
Creating this string as constant is a good idea if we need to use this string in multiple places. but if we try to change this string later in the code this will give us a compile error as this string is declared as constant. so the following line will not work for sure:
Many times if const is too restrictive then readonly may be right. The effect is very similar. A readonly type can only be modified in the constructor or as part of its declaration. If you 'parm' is passed in to the constructor then you could use the
code you have shown but change 'const' to 'readonly' as long as the
VERIFICATION_CODE = String.Formt(...
is in the constructor. There are plusses and minuses for both const and readonly which a little searching should find.
(Note that const was never going to be the right thing in your case as values with the const modify are substituted at
compile time - the parm variable is not even a twinkle in its parent's eyes at that stage)
dotnet_CH
Member
40 Points
259 Posts
Can we const with String.Format
Nov 12, 2012 03:16 AM|LINK
Hi All,
I wan to use const with string.Format - Is this possible to use this
Code:
Variable declare
private const string VERIFICATION_CODE = "User with Email ID: {0} has requested for the verfication Code.";
Variable use in method
VERIFICATION_CODE = String.Format(VERIFICATION_CODE, parm);
Error:
The left-hand side of an assignment must be a variable, property or indexer
Please let me know if any way-out is to use this or it is not good idea to use this const with string.Format
gerrylowry
All-Star
20515 Points
5713 Posts
Re: Can we const with String.Format
Nov 12, 2012 03:23 AM|LINK
@ dotnet_CH
VERIFICATION_CODE is a const so VERIFICATION_CODE can not appear on the LEFT side of an assignment.
g.
gerrylowry
All-Star
20515 Points
5713 Posts
Re: Can we const with String.Format
Nov 12, 2012 03:26 AM|LINK
@ dotnet_CH
why not try something like:
String FormattedVerificationCode = String.Format(VERIFICATION_CODE, parm);
g.
Rahul Rajat ...
Member
486 Points
25 Posts
Re: Can we const with String.Format
Nov 12, 2012 03:33 AM|LINK
The following line will create a constant string that will represent the FORMAT-ONLY for some string that will be use later:
VERIFICATION_CODE = "User with Email ID: {0} has requested for the verfication Code.";Creating this string as constant is a good idea if we need to use this string in multiple places. but if we try to change this string later in the code this will give us a compile error as this string is declared as constant. so the following line will not work for sure:
The only reason for this to not work is that the variable on the left side is a const.
So if this constant string you defined earlier is being used in multiple places then the right way to use this would be:
(Rahul Rajat Singh)
Paul Linton
Star
13421 Points
2535 Posts
Re: Can we const with String.Format
Nov 12, 2012 05:11 AM|LINK
Many times if const is too restrictive then readonly may be right. The effect is very similar. A readonly type can only be modified in the constructor or as part of its declaration. If you 'parm' is passed in to the constructor then you could use the code you have shown but change 'const' to 'readonly' as long as the
VERIFICATION_CODE = String.Formt(...
is in the constructor. There are plusses and minuses for both const and readonly which a little searching should find.
(Note that const was never going to be the right thing in your case as values with the const modify are substituted at compile time - the parm variable is not even a twinkle in its parent's eyes at that stage)