topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Saturday December 14, 2024, 12:05 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: [Solved] C# Arraylist  (Read 11310 times)

mediaguycouk

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 247
    • View Profile
    • Mediaguy
    • Donate to Member
[Solved] C# Arraylist
« on: March 23, 2008, 12:39 PM »
I'm getting stuck on the superhard c# assignment 6, but on the easy stuff.

I have this code...

Code: C# [Select]
  1. // Create an instance of StreamReader to read from a file.
  2. // The using statement also closes the StreamReader.
  3. using (StreamReader sr = new StreamReader(fileToEncryptText.Text))
  4. {
  5.     // Temp string for lines
  6.     string line;
  7.  
  8.     // Create an array list for the file contents
  9.     ArrayList fileContentsArrayList;
  10.  
  11.     int i = 0;
  12.  
  13.     // Read and display lines from the file until the end of
  14.     // the file is reached.
  15.     while ((line = sr.ReadLine()) != null)
  16.     {
  17.         // Place line into array
  18.         fileContentsArrayList.Add(line);
  19.  
  20.         // Increase [i]
  21.         i++;
  22.        
  23.         //encryptOutputText.Text += encrypt.EncryptSingleLine(line) + "\r\n";
  24.     }
  25.  
  26.     // Convert ArrayList into an Array
  27.     string[] fileContentsArray = (string[])fileContentsArrayList.ToArray();
  28. }

but it isn't compiling due to 'Use of unassigned local variable fileContentsArrayList'. I know this is because there is a possibility that using() doesn't run and nothing gets put into it.

My problem is that I can't really add anything into the array to initialise it that isn't part of the streamreader. So how can I make sure that it initialises without breaking what the program does?

Many thanks  :-*
Learning C# - Graham Robinson
« Last Edit: March 23, 2008, 06:09 PM by mediaguycouk »

CWuestefeld

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,009
    • View Profile
    • Donate to Member
Re: C# Arraylist
« Reply #1 on: March 23, 2008, 03:49 PM »
Are you sure that the problem is 'Use of unassigned local variable'? That message isn't an error, just a warning, so it wouldn't prevent compilation. However, I do see a different error.

What I do see is that you've only declared fileContentsArrayList at line 9. That is, you've created a variable by that name, that can hold a reference to an object of type ArrayList. But, despite your comment on line #8, you have not actually created an instance of such an object for your variable to point at.

What you want at line #9 is
Spoiler
Code: C# [Select]
  1. ArrayList fileContentsArrayList = new ArrayList();


mediaguycouk

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 247
    • View Profile
    • Mediaguy
    • Donate to Member
Re: C# Arraylist
« Reply #2 on: March 23, 2008, 06:09 PM »
That's the error message that is stopping compiling, but you have fixed the error. It now compiles and I thank you :)
Learning C# - Graham Robinson

CWuestefeld

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,009
    • View Profile
    • Donate to Member
Re: [Solved] C# Arraylist
« Reply #3 on: March 23, 2008, 06:48 PM »
You understand the difference between a variable representing a reference to the object, and thus requiring a separate step to actually create the object that it references? This is pretty key to understanding what's going on with C#.

mediaguycouk

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 247
    • View Profile
    • Mediaguy
    • Donate to Member
Re: [Solved] C# Arraylist
« Reply #4 on: March 24, 2008, 05:49 AM »
I admit that I do have a lot of trouble with that.

My GF comes from a Java background and finds all this very easy but I come from an actionscript / PHP background that just doesn't have these things.

But then that's why I love this forum. It has helpful people that don't mind sharing knowledge.
Learning C# - Graham Robinson

Renegade

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
Re: [Solved] C# Arraylist
« Reply #5 on: March 30, 2008, 09:08 PM »
...I come from an actionscript / PHP background that just doesn't have these things...
-mediaguycouk (March 24, 2008, 05:49 AM)

Once you get used to this kind of stuff in C#, and see how really well designed it is, you'll shake your head and wonder about dynamically typed languages like PHP. Not that there's anything wrong with dynamic types and the like, but static typing is really just a lot easier to deal with.

I've done both kinds of languages, and I really do prefer static typing. It just seems neater and cleaner.

Kind of as an aside, in C# you can also do things like this:

Code: C# [Select]
  1. private void Something(MyCustomClass thing)
  2. {
  3.     MyCustomClass temp;
  4.     temp = thing;
  5. }

It's a similar issue to what you have above.
Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker