Enum and Properties.Settings

H

henry.lee.jr

I thought I had a nice idea here:

private enum ProcessStatus : int
{
Initiated = Properties.Settings.Default.Status_Initiated,
Running = Properties.Settings.Default.Status_Running,
Complete = Properties.Settings.Default.Status_Complete,
Failed = Properties.Settings.Default.Status_Failed
}

This was great because I could define status codes in my settings file
(and later change them if needed on the fly without a code recompile
or going at a database table all the time) like so:

private void UpdateProcessStatus(ActionStatus status, string
information)

Problem is that the compiler does not like it ...

The property or indexer '[xxx].Properties.Settings.Default' cannot be
used in this context because it lacks the get accessor'

I'm not sure why a property would lack a get accessor, somewhat
defeats the purpose of a property. I'm also using properties.settings
in other points of my code to define stored procedure names without
issue. The only thing I can think of is that the enum is defined
before runtime, and these property defaults can only be accessed
during run time?

If so, does anyone have a nice clean way I can create enum values that
can be set with settings, or do I need to create variables for each
status type and then populate them at run time all the time?

Thanks
 
J

Jeff Johnson

I thought I had a nice idea here:

private enum ProcessStatus : int
{
Initiated = Properties.Settings.Default.Status_Initiated,
Running = Properties.Settings.Default.Status_Running,
Complete = Properties.Settings.Default.Status_Complete,
Failed = Properties.Settings.Default.Status_Failed
}

This was great because I could define status codes in my settings file
(and later change them if needed on the fly without a code recompile
or going at a database table all the time) like so:

private void UpdateProcessStatus(ActionStatus status, string
information)

Problem is that the compiler does not like it ...

What is "it"? You failed to show any actual code, just a method declaration.
 
A

Alberto Poblacion

[...] does anyone have a nice clean way I can create enum values that
can be set with settings, or do I need to create variables for each
status type and then populate them at run time all the time?


The values of the enum need to be known at compile time. However, the
Settings are supposed to be changeable after compilation. Therefore, it is
unlikely that you will find a working mechanism to do what you intend.
The alternative that you mention, creating variables and populating them
during program startup, should serve you perfectly well.
 
H

henry.lee.jr

What is "it"? You failed to show any actual code, just a method declaration.

The compiler doesn't like this:

private enum ProcessStatus : int
{
Initiated = Properties.Settings.Default.Status_Initiated,
Running = Properties.Settings.Default.Status_Running,
Complete = Properties.Settings.Default.Status_Complete,
Failed = Properties.Settings.Default.Status_Failed
}

For each assignment of my enum member, it gives me this error:

The property or indexer '[xxx].Properties.Settings.Default' cannot be
used in this context because it lacks the get accessor'

So for the code above, all 4 of those enum assignments give me that
same error.

When I change it to:

private enum ProcessStatus : int
{
Initiated = 1,
Running = 2,
Complete = 3,
Failed = -1
}

then everything works fine ... but of course at that point I am hard-
coding my app settings for these status codes, should they ever change
in the future I would need to recode and recompile.
 
J

Jeff Johnson

When I change it to:
private enum ProcessStatus : int
{
Initiated = 1,
Running = 2,
Complete = 3,
Failed = -1
}
then everything works fine ... but of course at that point I am hard-
coding my app settings for these status codes, should they ever change
in the future I would need to recode and recompile.

Like Alberto said, that's just the way enums work. Nothing you can do about
it. They are implemented under the covers as constants, and you're trying to
make them dynamic.
 
B

Ben Voigt [C++ MVP]

I thought I had a nice idea here:
As others have said, you can't use an enum, because they only use
compile-time constants.

But you're closer than you probably think:
private enum ProcessStatus : int
private static class ProcessStatus
{ public static readonly int
Initiated = Properties.Settings.Default.Status_Initiated,
Running = Properties.Settings.Default.Status_Running,
Complete = Properties.Settings.Default.Status_Complete,
Failed = Properties.Settings.Default.Status_Failed
// no change to those four lines, but add this semicolon
;
}

This was great because I could define status codes in my settings file
(and later change them if needed on the fly without a code recompile
or going at a database table all the time) like so:

private void UpdateProcessStatus(ActionStatus status, string
information)

Problem is that the compiler does not like it ...

The property or indexer '[xxx].Properties.Settings.Default' cannot be
used in this context because it lacks the get accessor'

I'm not sure why a property would lack a get accessor, somewhat
defeats the purpose of a property. I'm also using properties.settings
in other points of my code to define stored procedure names without
issue. The only thing I can think of is that the enum is defined
before runtime, and these property defaults can only be accessed
during run time?

If so, does anyone have a nice clean way I can create enum values that
can be set with settings, or do I need to create variables for each
status type and then populate them at run time all the time?

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top