4

I am reading connection strings from my App.config file and for that i have following code.

try
 {
    string[] dbnames;
    int counter = 0;
    foreach (ConnectionStringSettings connSettings in ConfigurationManager.ConnectionStrings) 
    {
        dbnames[counter] = connSettings.Name;
        counter++;
    }
    return dbnames;
 }
 catch
 {
    throw;
 }

this code giving me error use of unassigned local variable for dbnames. i will have multiple connection strings in my App.config. They can be none,1,2 and so on. Depending on the needs. so i cant statically assign the dbname size. Because there can be a scenario if they exceed the value of assigned size. eg. if i assign it a size of 5, and what if i get 6th connection string. and if i have 1, then remaining 4 will be a memory wastage.

If i am wrong then let me know.

Thanks.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Assad Nazar
  • 1,420
  • 5
  • 20
  • 40
  • 3
    Fix it with: `string[] dbnames = null;` Run the code, then understand why it does not work. – leppie Jan 27 '11 at 10:48
  • 1
    @leppie: Clever, i've +1'd. Also, one could say: uninitialized variable has no initializer. \o/ – Romain Jan 27 '11 at 10:49
  • yeah right, i tried null and it was then jumping to the exception 'catch' section – Assad Nazar Jan 27 '11 at 10:51
  • Good, first step is understanding the root problem. Now follow the answers below to solved the rest of the problem :) – leppie Jan 27 '11 at 10:53
  • If I have 3 connection strings in my appSettings tag, how can i determine which one is the default (obviously when i will select one to be taken as default)? – Assad Nazar Jan 28 '11 at 07:07

5 Answers5

13

Use this while initializing the array.

 string[] dbnames = new string[ConfigurationManager.ConnectionStrings.Count];

OR use List<string>

Adeel
  • 19,075
  • 4
  • 46
  • 60
  • How will i read app.config file which contains? [code] [/code] – Assad Nazar Jan 27 '11 at 11:16
  • @booota you are already using ConfigurationManager class. With this you can read different section in the configuration file. e.g. in your foreach loop, you can use this code.ConfigurationManager.ConnectionStrings[i].ConnectionString – Adeel Jan 27 '11 at 11:25
  • but that i giving me some mdb information when i print the output. But i am connecting it to mysql using mysql connector. and my connection string is written above. as u can see. – Assad Nazar Jan 27 '11 at 16:34
5

You can't resize a System.Array dynamically like that.

Fortunately, there's no reason to do so. Use a different type of collection, like a List<T> instead. (Make sure you've added a using declaration for the System.Collections.Generic namespace!)

Like an array, a List<T> allows you to access the elements in the list by index, but it's also dynamically resizable at run-time, which fulfills the requirements in your question. And of course, since it's a generic method, it has the additional advantage (as compared to some of your other choices) of being strongly-typed. Since you're working with string types, you would use List<string>.

EDIT: There's absolutely no need for that empty try/catch block. Why catch an exception if you're just going to immediately rethow it? Just let it bubble up. In general, you shouldn't catch exceptions unless and only unless you can fix their immediate cause.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • +1 booota also didn't even initialize the array; he just declared it. And FYI, in case booota needs the return type to be string[], List also has the ToArray() method - List dbnames; dbnames.ToArray(); – bitxwise Jan 27 '11 at 10:57
2

You're declaring dbnames as a string array, but not defining it's size.

You'll need something like:

string[] dbames = new string[4];

where "4" is the length of your array.

If, however, you need a variable length you should use List<string>. In this case you can then add to it as necessary.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
1

As others have said, you could just use a List<string>. I would use LINQ to do all of this though, if you're using .NET 3.5 or higher:

return ConfigurationManager.ConnectionStrings
                           .Cast<ConnectionStringSettings>()
                           .Select(setting => setting.Name)
                           .ToArray(); // Or ToList
  • No need for a foreach loop (in your code - obviously it's there somewher :)
  • You can easily decide whether to return a list, an array, or simply IEnumerable<string>
  • No need for try/catch
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

declare it after class e.g

i am also writing code and i used to always encounter this problem

   public class ABC{
string[] array;
ABC()
{
}
//your_function_logics
}