Error 1 The Expression Being Assigned To Must Be Constant
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies the expression being assigned to optional parameter must be a constant or default value of this site About Us Learn more about Stack Overflow the company
An Attribute Argument Must Be A Constant Expression
Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users const array c# Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a static readonly minute: Sign up The expression being assigned to '…' must be constant [duplicate] up vote 3 down vote favorite This question already has an answer here: CS0133 “The expression being assigned to 'identifier' must be constant” - what's the reason behind that? 4 answers What's wrong with this code? I get this error: "The expression being assigned to '....' must be constant"
C# Const Static
public const string ConnectionString = System.IO.File.ReadAllText(@"D:\connection.ini"); c# file const share|improve this question asked Oct 7 '13 at 10:33 Attila 4015 marked as duplicate by musefan, Kate Gregory, iCodez, Brant Bobby, w0lf Oct 7 '13 at 15:35 This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. Did you read the documentation on const? –Jon Oct 7 '13 at 10:35 A 'const' value has to be known at compile time. In your example you are asking the compiler to go away and execute a random bit of code - which it will not do. –Rob Oct 7 '13 at 10:37 Your File.ReadAllText part is evaluated in runtime, const is used to specify a compile-time expressions. You can use readonly instead. –Soner Gönül Oct 7 '13 at 10:37 Use readonly instead of const and make static public static readonly string ConnectionString = System.IO.File.ReadAllText(@"D:\connection.ini");, note that now it's simply a field that can be changed only in constructors. –Alessandro D'Andria Oct 7 '1
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies readonly c# of this site About Us Learn more about Stack Overflow the company Business readonly property c# Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign http://stackoverflow.com/questions/19222533/the-expression-being-assigned-to-must-be-constant up The Expression assigned to X must be constant? [duplicate] up vote 1 down vote favorite This question already has an answer here: CS0133 “The expression being assigned to 'identifier' must be constant” - what's the reason behind that? 4 answers I got that error with this code: const string body = HighPoint; // HighPoint is a string arg passed in to http://stackoverflow.com/questions/11425281/the-expression-assigned-to-x-must-be-constant the method ...and was able to work around it by removing the constant: string body = HighPoint; ...or, of course, assigning a constant value: const string body = "My Dinner with Andre"; ...but is "my way not a very sporting way"? (gratuitous Princess Bride reference) c# string const share|improve this question asked Jul 11 '12 at 3:54 B. Clay Shannon 25157196403 marked as duplicate by Kevin Panko, Conrad Frix, showdev, 48klocs, Frank van Puffelen Jan 29 '14 at 22:38 This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. Basically you are looking for a readonly local, and that is not a feature that C# supports. –Anthony Pegram Jul 11 '12 at 3:58 Related: stackoverflow.com/questions/443687/… –Anthony Pegram Jul 11 '12 at 4:01 I guess what's strange to me is that somebody would make something like this const in the first place. I derived this code from a sample on sending email. When would you ever have the same exact body in an email (unless you
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About http://stackoverflow.com/questions/15474457/the-expression-assigned-to-x-must-be-constant Us Learn more about Stack Overflow the company Business Learn more about hiring http://www.nullskull.com/q/10100258/the-expression-being-assigned-must-be-constant.aspx developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up The Expression assigned to X must must be be constant up vote 3 down vote favorite I'm having this problem and searching for solutions, but didn't find a situation similar to mine.Here's what I have: in a class file X.cs: namespace BlaBla{ public class X{ public const string foo = "foo"; // other code } public class Y{ public const int bar = 0; } } and in some other class, I'm writing: private the expression being const string someString = X.foo + Y.bar + ""; it keeps giving me a compile error: The Expression assigned to someString must be constant although they are both constant! when I remove Y.bar the error is removed and I dont know why. Any help ? c# compiler-errors const share|improve this question edited Mar 18 '13 at 13:53 Phil 26.7k35674 asked Mar 18 '13 at 10:23 Nada Naoushi 4902822 add a comment| 3 Answers 3 active oldest votes up vote 4 down vote accepted You're using the const keyword wrong. You probably want to define someString as: private readonly someString = X.foo + Y.bar; When a variable is marked as const they are compiled in to the assembly and then optimised. While Y.bar is defined as const, you're implicitly invoking its ToString() method which makes it not constant. share|improve this answer edited Mar 18 '13 at 10:31 answered Mar 18 '13 at 10:25 Daniel Imms 24.5k35799 That doesn't really explain why it's not working. –Jon Skeet Mar 18 '13 at 10:28 add a comment| up vote 3 down vote You're including a conversion from int to string. Even though the int is a constant, the
Constantpublic class XMLSchemas{public const string DropDownOptionsSchema = HttpContext.Current.Server.MapPath(@"App_Data\Schema\DropDownOptionsSchema.xsd");}}but it give an error "The expression being assigned to Constant.XMLSchemas.DropDownOptionsSchema must be constant"Any idea guys.. Thanks Harry L replied to tayuwi n00dle$ on 02-Jun-09 12:42 AM Hi you can not assign any variable value to constant variable. Constant variables are meant for only holding constant values. public const string DropDownOptionsSchema = HttpContext.Current.Server.MapPath(@"App_Data\Schema\DropDownOptionsSchema.xsd"); remove the const from above declaration and see. If you want to assign any variable value to constant variable, the only way is declare readonly variable, but for readonly variable also one condition is that, you cas assign vaule to only in the constructor. Re - Ravenet Rasaiyah replied to tayuwi n00dle$ on 02-Jun-09 12:44 AM HiFirst you have wrong syntax in your codepublic class Constant{public class XMLSchemas{public const string DropDownOptionsSchema = HttpContext.Current.Server.MapPath(@"App_Data\Schema\DropDownOptionsSchema.xsd");}}the second is Constants are values which are known at compile time and do not change. according this line may be change the value in runtime you can't do like that way.public class Constant { public class XMLSchemas { public XMLSchemas() { } public string DropDownOptionsSchema = HttpContext.Current.Server.MapPath(@"App_Data\Schema\DropDownOptionsSchema.xsd"); } }or else you can use the static variable public class Constant { public class XMLSchemas { public XMLSchemas() { } public static string DropDownOptionsSchema = HttpContext.Current.Server.MapPath(@"App_Data\Schema\DropDownOptionsSchema.xsd"); } }thank you one more way - Ravenet Rasaiyah replied to tayuwi n00dle$ on 02-Jun-09 12:46 AM Hiif you are think s