el ninho
el ninho

Reputation: 4233

Create dynamic names in loop

for (int i = 0; i < DT1.Rows.Count; i++)
{
    PivotGridField field + i = new PivotGridField();
}

Of course, this code will not work, but how to make it work this way, as I need to create unknown number of fields.

Thanks!

Upvotes: 3

Views: 2962

Answers (5)

Maciej
Maciej

Reputation: 7961

You can do it in different ways. First you can use an array like Marc suggested. Alternative is to use dictionary:

Dictionary<string,PivotGridField> fields = new Dictionary<string, PivotGridField>();
for (int i = 0; i < DT1.Rows.Count; i++)
{
    fields["field"+i] = new PivotGridField();
}

fields["field1"] = ...

Third method is to use ExpandoObject:

dynamic fields = new ExpandoObject();
for (int i = 0; i < DT1.Rows.Count; i++)
{
    ((IDictionary<String, Object>) fields).Add("field" + i, new PivotGridField());
}

You can then access your fields as if they were real member fields:

fields.field1 = ...
fields.field2 = ...

Upvotes: 3

GregRos
GregRos

Reputation: 9113

You can create fields/properties on the fly in C#, but only for a specific sort of object. Using an ExpandoObject, you can do this:

    ExpandoObject expando = new ExpandoObject();
    IDictionary<string, object> expandoAsDict = expando as IDictionary<string, object>;
    expando.Add("MyTotallyNewProperty", "MyTotallyNewPropertyValue");
    Console.WriteLine(expando.MyTotallyNewProperty); //this will print "MyTotallyNewPropertyValue"

You can customize the nature of your dynamic object by inheriting from DynamicObject. read up on this here.

That said, in your case, you should instead use the other suggestions. Dynamic objects should only be used as required, and in your case, a dynamic data structure would do the trick quite nicely. Remember that just because the word 'field' appears in the specification, doesn't mean you need to use the C# concept of a field to implement it.

NOTE: DynamicObject only exists in .NET 4.0 or higher. There are no convenient ways of doing this in previous versions.

Upvotes: 0

slapthelownote
slapthelownote

Reputation: 4279

Perhaps you could instead create a list of type PivotGridField and add to list on each iteration. If one of the properties of PivotGridField is something like "name", you can set it to be "field" + i.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062955

You cannot create fields (or, as in this case, variables) on the fly. What you can do is have an array / list / similar:

PivotGridField[] fields = new PivotGridField[DT1.Rows.Count];
for (int i = 0; i < DT1.Rows.Count; i++)
{
    fields[i] = new PivotGridField();
}

Now whenever you want "field n", use fields[n]

Upvotes: 3

abatishchev
abatishchev

Reputation: 100288

No, in C# you can't generate variables' name dynamically.

But you can set various field properties' value:

for (int i = 0; i < DT1.Rows.Count; i++)
{
    PivotGridField field = new PivotGridField();
    field.Name = "Name " + i;
    grid.Fields.Add(field);
}

Upvotes: 0

Related Questions