1

I would like to create new instances of my Class called "Item" inside a for statement, but I dont know how to give the name dynamically.

for (i=0; i < Counter; i++)
{
    MyClass "XXXX" = Class.method();
}

How can I create 2 strings and give a name? -for instance-

for(i=0;i<2;i++){
  string name + i = "Hello" 
}

EDITED

I ve got some proposals to reach my solution which I can create a Dictionary.

       var bomItems = new Dictionary<Item, Item>();

       for (int i = 0; i < Count; i++)
       {
           Item bomItem = inn.newItem("Part BOM","add");
           bomItems.Add(bomItem + i, bomItem);
       }

But I got a reasonable error in "bomItem + i". that I cannot apply operand '+' . obviously.

Does anyone have any answer for this?

Thank you.

fedmich
  • 5,343
  • 3
  • 37
  • 52
Daniel Camacho
  • 423
  • 5
  • 12
  • 27
  • Are you taking about dictionaries? – BlackBear May 07 '12 at 20:44
  • 2
    Please explain the use case here. I would bet this isn't what you actually want. – Austin Salonen May 07 '12 at 20:44
  • the `Counter` variable cannot be a string. Its an `int` and has to be created outside of the for loop. But if you are wanting to dynamically assign things to a string field your `"xxxx"` should not have quotes around it. – Brad May 07 '12 at 20:45
  • I wanna create my own data´s variables type inside a for-loop. But I do not know how to give variable´s name to this data. I gave string as an example. Array and List do not give variable´s name. – Daniel Camacho May 08 '12 at 08:34
  • you will not be able to create dynamic variable names in the way you describe. Best solutions are the ones given below for Lists and Dictionaries. this is closest you will get.. Also If you declared a variable inside the for loop you would not have access to it outside the for loop so why would it need a dynamic name? Just call it something generic that relates to what it could be – Steve May 08 '12 at 09:38
  • @Steve Thanks,the point is that I need to create variables inside a loop, but I dont know in advance how many there will be created. I will need access them outside the loop as well. Can you give any clue how I can achive that? – Daniel Camacho May 08 '12 at 10:23
  • Personally I would go with @ademing2 example with the dictionary. showing how you declare the dictionary outside the for loop, Set values with names (Keys) inside the for loop. you will then have access outside of the loop. and be able to retrive the value using the key you set at the time (any string which you havent already used) – Steve May 08 '12 at 12:32

4 Answers4

5

Use an array!

string[] myArray = new string[Counter];
for (int i = 0; i < Counter; i++){
  myArray[i] = "Hello";
}
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • Are arrays significantly faster than List even for reference type objects? – Johan Lundberg May 07 '12 at 20:56
  • 3
    @JohanLundberg just marginally and ignorable for trivial cases; see http://stackoverflow.com/questions/454916/performance-of-arrays-vs-lists – Bala R May 07 '12 at 20:59
  • @BalaR I would like to give variable´s name to "Hello" value. Not referece it by an Array or List. I wana give a name for example: string name = "Hello" which later I will be able to call this value through the "name" – Daniel Camacho May 08 '12 at 08:36
4

You coud use a collection like List<String>:

var list = new List<String>();
for (i=0; i<Counter; i++){
     list.Add("Hello " + i);
}

Edit Accroding to your comment you want to access the variable by it's assigned name. Then you should use a Dictionary instead (if the names are unique).

For example:

var names = new Dictionary<String, String>();
for (i=0; i < Counter; i++){
     names.Add("Name" + i, "Hello");
}

Now you can get the string-value of a given string-key in this way:

String name10 = names["Name10"]; // "Hello" since all values are "Hello" in your sample
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I would like to do this: " string name + i = "Hello" " – Daniel Camacho May 08 '12 at 09:07
  • @kmxillo: If i understood you correctly, you could use a `Dictionary`, see my edited answer. – Tim Schmelter May 08 '12 at 09:28
  • Yes, you understood. But It cannot convert type from dictionary to myClass. myClass name= new Dictionary(); Thanks – Daniel Camacho May 08 '12 at 09:51
  • @kmxillo: Do you want to store your custom class `myClass` as key or as value in the dictionary? Anyway, if you want to store that type as value you need to change `Dictionary` to `Dictionary` and you need to show me how you create an instance of `myClass`. – Tim Schmelter May 08 '12 at 10:16
  • I asked the question as I had an string, but actually I have not. My class is "Item" and I would like to create new instances of it. I edit my question. – Daniel Camacho May 09 '12 at 09:38
  • @kmxillo: Do you want the item as key or the number as key(`i` in your for-loop)? You also have declared the dictionary as `bomItems` but you are adding items to `relatedItems`. You're creating an instance of `Item` with name `bomItem` but you're adding `relatedItem`. Are these all typos or have you just left out relevant parts of your code? Btw, what type is `inn` which has a method `newItem`. Is it a factory-method which constructs `Items`? Would you show the relevant part of your `Item` class? – Tim Schmelter May 09 '12 at 09:53
  • I commit a mistake in my question which I wrote "relatedItem". I just like to create "Item bomItem = inn.newItem("Part BOM","add"); " as many as counter value. I referred to my initial question that I do not knwo how to give dinamically names to bomItem as bomItem1, bomItem2. Item is a factory-method which I cannot modify. Thanks again – Daniel Camacho May 09 '12 at 10:15
  • @kmxillo: If i understand your requirement correctly, you want to access the items by a name. Does it need to be a name or can it be an index. Then my original `List` approach would be the best(a List can be accessed by index very fast). Or are these names `bomItem1,bomItem2,...` just a simplification for something that can be more complex than an appended number? Then the dictionary approach is the best(just use a **String as key** and an instance of `Item` as value). – Tim Schmelter May 09 '12 at 10:31
  • Can u give a code example how to handle your solution? The key points are that I need to use those `bomItem1 bomItem2`outside the for-loop and handle them as `Item` not as `string`. – Daniel Camacho May 09 '12 at 10:41
  • 1
    @kmxillo: I've showed already both ways(using a list and a dictionary). So either access the list by the index (`Item item1 = items[0] //where items is a List`) or use a dictionary(`Item item1 = items["bomItem1"] // where items is a Dictionary`). So the key problem in your code is that you use a `Item` as key what is only possible if you override `GetHashCode` and `Equals` in your class. You just need to use a String as key that you can construct for example: `bomItems.Add("bomItem" + i, bomItem);` – Tim Schmelter May 09 '12 at 11:25
1

You can use the Dictionary (TKey,TValue) class, where the key is the string you would like to count with.

Dictionary<string, string> myDictionary = new Dictionary<string, string>();
for (i=0; i < Counter; i++)
{
   myDictionary.Add("XXXX", "Hello");  // Matches your above example
}

So XXXX would be your counter string, and Hello would be the string you would like associated with that. Then you can retrieve each string using

string myString = myDictionary[XXXX];
Aaron Deming
  • 1,015
  • 10
  • 12
0

Another way is use of StringBuilder, like

var builder = new StringBuilder();
for (i=0; i<Counter; i++){
     builder.Append("Hello " + i);
}

and after if you need complete string

builder.ToString().

Much faster then simple string manipulations.

Tigran
  • 61,654
  • 8
  • 86
  • 123