Re: anonymous initializer problem

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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.







.



Relevant Pages

  • Re: Is the order of static block execution guaranteed ?
    ... >>even though they're declared in the same line of source code. ... The declaration and initialization are after the assignment in the ... there is a static initializer and after it ... a class variable declaration combined with a class variable initializer. ...
    (comp.lang.java.programmer)
  • PROBLEM: Compile Break 2.6.0-test2: More Broken Stuff
    ... declaration of `DECLARE_TASK_QUEUE' ... initializer but incomplete type ... drivers/net/wan/sdlamain.c:224: unknown field `routine' specified in ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)
  • Re: Compiling Guppy-PE extension modules
    ... >> The MSDN help gives a simple example of code raising compiler error ... src\sets\sets.c: error C2099: initializer is not a constant ... I don't know if it applies here, but in this context the extern keyword ...
    (comp.lang.python)
  • Re: Arrays of incomplete type
    ... Even if `struct opaque' ... this declaration would be incorrect as ... Your comment said it would be incorrect ... with an initializer equal to 0." ...
    (comp.std.c)
  • Re: Invalid conversion from char tp char*
    ... Consider using std::strings instead of char arrays. ... search as much for the declaration of an object. ... The problem is that array sizes must be compile-time constants. ... You should also prefer standard containers to arrays. ...
    (comp.lang.cpp)