I would like to set a const string from Settings.
In case I would like to change in the future the program language, it is quite easy; Just have to modify the appropriate settings!
When trying this:
private const string constString =
"-&" + Properties.Settings.Default.constStringText;
I get this error:
The property or indexer 'Properties.Settings.Default'
cannot be used in this context because it lacks the get accessor.
Any idea?
Since you intend to use this as the default value for an optional method argument, that is:
public void Foo(string something = constString)
{
//do something
}
This constString
must be a compile-time constant . From the MSDN page for "Named and Optional Arguments" :
A default value must be one of the following types of expressions:
a constant expression;
an expression of the form new ValType(), where ValType is a value type, such as an enum or a struct;
an expression of the form default(ValType), where ValType is a value type.
As such, there really is no way to read a value from a configuration file at runtime then use it for an optional argument .
One workaround would be to instead declare your constString
as a readonly
field:
private readonly string constString =
"-&" + Properties.Settings.Default.constStringText;
Then make your optional argument required and create a wrapping overload for your method that doesn't take that parameter. That overload in turn calls the old method with the default value resolved at runtime:
public void Foo(string something) //no longer optional
{
//do something
}
public void Foo()
{
Foo(constString); //calls the other overload with the default value
}
This error is in 99% of cases related to a wrong namespace. You've probably generated some class in which you are using Properties.Settings.Default
Solution: check namespace in that class file. It must be the same as the name of the tag in App.config (Web.config) where that particular setting is stored.
While Chris Sinclair's answer (a const
member must have value type and be evaluable at compile-time, so use readonly
) is correct, I should like to explain these restrictions and answer the implicit question what the compiler message means, albeit with the disappointing remark that seems to be simply an error in the compiler .
The property or indexer 'Properties.Settings.Default' cannot be used in this context because it lacks the get accessor.
This message suggests that the expression Properties.Settings.Default
could be made acceptable by adding a getter (and maybe something else) — as far as I know, that is simply wrong. After all, on the one hand, as the asker assured us 1 , there was a getter, and on the other, as Chris explains, the reason the expression is invalid is that is not evaluable at compile-time, and never can be, given that it depends on the run-time configuration.
Presumably this message is intended for other situations, and has been used here by mistake.
1 I have also seen this, in MSVS 2013, when a default parameter value referred to a property which did have a getter – but at least it also reported “ Default parameter value for ' <parname>
' must be a compile-time constant ”.
The restriction of const
members and default parameter values to value types (as at the C# 5.0 Language Specification, 2012) appears to be not entirely inevitable , but an understandable consequence of other language design decisions.
static
methods can also be considered fully determined at compile-time, as would those bound to immutable objects, were that concept supported. static
) readonly constString
instead of const constString
, to make clear that while the value will not change, it is not determined until run-time, at class or object initialisation (as in Chris's answer). If you want to use the expression as a default value for an optional parameter, it must be a true compile-time constant, leaving you two possibilities:
Declare an overload, as in Chris's answer, eg:
Foo() { Foo(Default); } Foo(string s) { Bar(s); }
Foo() { Foo(Default); } Foo(string s) { Bar(s); }
.
Use null
as a convention for the default, and interpret that in your method:
Foo(string s = null) { Bar(s != null ? s : Default); }
Foo(string s = null) { Bar(s != null ? s : Default); }
.
null
is not a supported parameter to Foo(string)
and should definitely be clarified in documentation comments. Maybe : apply the Optional
attribute, as in this question: behaviour of Optional attribute :
Foo([Optional] string s) { Bar(etc.?); }
Foo([Optional] string s) { Bar(etc.?); }
.
= null
, unless perhaps null
is a supported argument to Foo(string)
. ¹ Language Specification 5.0 §10.5.2.2 10.5.2.2 Versioning of constants and static readonly
fields
² I recall reading this, but have not found it in the Language Specification.
I recently encountered this scenario, and while searching for a solution I stumbled on this page. Based on the example above, I was able to resolve my issue like this:
private static readonly String ConStr
= string.Format("Data Source={0};Initial Catalog={1}; User ID={2}; Password={3};",
Properties.Settings.Default.DataSource,
Properties.Settings.Default.Catalog,
Properties.Settings.Default.UserID,
Properties.Settings.Default.Password);
Just wanted to share.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.