Re: anonymous initializer problem
- From: Sergey Poberezovskiy <SergeyPoberezovskiy@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 5 Jun 2007 19:07:00 -0700
Ben,
I do realize that - I use arrays here just so that it is clearly defined in
the declaration section (via initializer). It is just the way I structure my
classes - constants and readonly fields first, then other declarations,
constructors, etc.
This way it is always clear where to go to see/change those definitions -
and that was the reason why I did not want that to be in either constructor
or a static method (which would also work).
At the end I just defined a private class that inherits the typed dictionary
and has only one public constructor that takes an array of field objects.
This way I could define my dictionary in the declaration section where it is
clearly seen:
private class fields : Dictionary<string, field>
{
public fields(field[] array)
{
foreach (field item in array)
{
this.Add(item.Key, item);
}
}
}
private static readonly fields _fields = new fields(
new field[] {
new field("Application", "AppName"),
...
}
);
It is not "anonymous", but is clearly defined in the declaration section -
as I wanted.
Thank you both for your time.
"Ben Voigt [C++ MVP]" wrote:
.
"Sergey Poberezovskiy" <SergeyPoberezovskiy@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote
in message news:B00DA930-14B4-47A4-9C92-3B392C6626B0@xxxxxxxxxxxxxxxx
Ben,
disregards my previos message - I should have read more carefully - my
bad.
Though this is a way to initialize - I wanted to accomplish the
initialization inside the declaration section (before it gets to the
static
constructor). The reason for that is that the next lines inside the
costructor declare custom static arrays that use the dictionary values,
such
as
private static readonly field[] _REQUIRED_COLUMNS = {_fields["Name1"],
...};
...
private static readonly field[] _CUSTOM_COLUMNS = {_fields["NameN"], ...};
Originally I had just the arrays declarations, and then I noticed that at
times a field name was changed in one array, but not in ALL. As a solution
I
created a dictionary to hold the values, so that the change needs to
happen
in one place only.
Hope it makes sense
If you want to control the order of initialization, you need to put it all
in the static constructor.
Also, I hope that you realize that your readonly arrays are fixed in size,
but not in content. If you need fixed content you should go for something
like ReadonlyCollection.
"Ben Voigt [C++ MVP]" wrote:
"Sergey Poberezovskiy" <SergeyPoberezovskiy@xxxxxxxxxxxxxxxxxxxxxxxxx>
wrote
in message news:4B1E2054-5823-4CEA-BCB1-F403E299A2F7@xxxxxxxxxxxxxxxx
Hi,
I need to initialize my class level dictionary (in .Net 2.0). I wanted
to
make it inline and employ anonymous methods as I do not use this code
for
anything else. Something similar to the following:
private static Dictionary<string, field> _fields = delegate()
{
Dictionary<string, field> result = new Dictionary<string, field>();
result.Add("Application", new field("Application", "AppName"));
...
return result;
};
And then I need to use the dictionary values in the rest of the
declarations.
Apparently the code above does not compile with the error: "Cannot
convert
anonymous method block to type
'System.Collections.Generic.Dictionary<string,GroupDates.field>'
because
it
is not a delegate type".
I understand that it is not - but what is the correct way to initialize
a
variable with anonymous method (and is there)?
I could always create a method that would return the correct
initialized
object, but would prefer not to.
The C# compiler just puts the initializer code in the static constructor,
so
that's what you should do too.
private static readonly Dictionary<string, field> _fields;
static ClassName()
{
_fields = new Dictionary<string, field>();
_fields.Add("Application", new field("Application", "AppName"));
}
Any suggestions are appreciated.
- References:
- Re: anonymous initializer problem
- From: Ben Voigt [C++ MVP]
- Re: anonymous initializer problem
- From: Sergey Poberezovskiy
- Re: anonymous initializer problem
- From: Ben Voigt [C++ MVP]
- Re: anonymous initializer problem
- Prev by Date: Re: Trigger/Event when a usb thumb drive is inserted? (vista)
- Next by Date: Re: C# IDE show all properties for controls and forms
- Previous by thread: Re: anonymous initializer problem
- Next by thread: RE: How to read the message of a system event log
- Index(es):
Relevant Pages
|