0

in my windows forms application I need for example a variable amount of strings. I am just using strings to simplify here. Actually I want to create a variable amount of Webbrowsers to access multiple URLs at once. So to use an Array isn't the answer in this case...

I tried to solve this with the use of a for-clause like this:

int amount = 12
for (int i = 0; i < amount; i++)
{
   string variable_i = new string();
}

this didn't work because the variable, i in this case, is not being handled as a variable and it is only trying to create the string variable_i twelfth times.

If there is any way to accomplish that please let me know :)

abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • 2
    What is the actual requirement? what would be the expected outcome of the snippet that you are looking for? I don't see any evidence for `I want to create a variable amount of Webbrowsers to access multiple URLs at once` in your code – sujith karivelil Mar 22 '17 at 01:19
  • Create multiple web browser instances and assign url to each at load time... – Dayan Mar 22 '17 at 01:20
  • My aim is to quicky access multiple urls... It's all about speed. So I want to create for example 7 webbrowsers and make each individual one access a different url @un-lucky – Friedrich Rehren Mar 22 '17 at 01:22
  • Sounds like you probably want to use a Collection: https://msdn.microsoft.com/en-us/library/mt654013.aspx – NoxelNyx Mar 22 '17 at 01:23
  • your question somehow is ambiguous. – A Farmanbar Mar 22 '17 at 01:23
  • @dayan the problem here is that it can be up to 30 different urls but mist the time it will be just around 5.. So I don't want to waste processing power and therefore time on making 30 webbrowsers in advance when i only need 5... – Friedrich Rehren Mar 22 '17 at 01:24
  • Sorry for that... @Mr.AF I tried to not get to specific to make it relevant to other users as well... But feel free to tell me what I shall improve.. – Friedrich Rehren Mar 22 '17 at 01:26
  • What do you mean by variable? As in, you don't know the exact number of webbrowsers you will need to create and therefore need a dynamic array? – NoxelNyx Mar 22 '17 at 01:30
  • Yes exactly. I don't know how many webbrowsers I need to create @NorianNyx – Friedrich Rehren Mar 22 '17 at 01:31
  • Are you wanting to create a "random" number of browsers or a "variable" number? If it's variable then you have some way of determining how many you want to start. You might also take a look at this answer http://stackoverflow.com/a/22748248/1454658 for a way to do this with async – James Durda Mar 22 '17 at 01:34
  • @XaverXor Please see my earlier comment on Collections. They are what you want. Collections do not require a predetermined amount of memory and grow as they are added to. – NoxelNyx Mar 22 '17 at 01:38
  • Thanks a lot to all of you. My problem was solved by using `Collection` and the "marked as duplicate" (@un-lucky, @Grant Winney, @Alexei Levenkov) helped me to understand the (for me quite complicate) Microsoft Help provided by @NorianNyx . – Friedrich Rehren Mar 22 '17 at 09:53

1 Answers1

1

You sound like you're asking about an array that automatically grows as you add items to it. A List will do the trick. For example:

int amount = 12;
List<string> strings = new List<string>();
for (int i = 0; i < amount; i++)
{
   strings.Add(new string());
}
Khelvaster
  • 852
  • 1
  • 6
  • 18
  • I needed to use `Dictionary webbrowsers = new Dictionary();` since I wanted to add a specific named webbrowser but my question was answered by you greatly. Thanks a lot! – Friedrich Rehren Mar 22 '17 at 09:48