Welcome Guest.   Make a donation to an author on the site April 24, 2014, 05:38:20 PM  *

Please login or register.
Or did you miss your validation email?


Login with username and password (forgot your password?)
Why not become a lifetime supporting member of the site with a one-time donation of any amount? Your donation entitles you to a ton of additional benefits, including access to exclusive discounts and downloads, the ability to enter monthly free software drawings, and a single non-expiring license key for all of our programs.


You must sign up here before you can post and access some areas of the site. Registration is totally free and confidential.
 
Your Support Funds this Site: View the Supporter Yearbook.
   
   Forum Home   Thread Marks Chat! Downloads Search Login Register  
Pages: [1]   Go Down
  Reply  |  New Topic  |  Print  
Author Topic: [Solved] C# Arraylist  (Read 4871 times)
mediaguycouk
Supporting Member
**
Posts: 244


see users location on a map View Profile WWW Give some DonationCredits to this forum member
« on: March 23, 2008, 12:39:26 PM »

I'm getting stuck on the superhard c# assignment 6, but on the easy stuff.

I have this code...

Formatted for C# with the GeSHI Syntax Highlighter [copy or print]
  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  Kiss
« Last Edit: March 23, 2008, 06:09:20 PM by mediaguycouk » Logged

Learning C# - Graham Robinson
CWuestefeld
Supporting Member
**
Posts: 911



see users location on a map View Profile WWW Give some DonationCredits to this forum member
« Reply #1 on: March 23, 2008, 03:49:30 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
Logged



mediaguycouk
Supporting Member
**
Posts: 244


see users location on a map View Profile WWW Give some DonationCredits to this forum member
« Reply #2 on: March 23, 2008, 06:09:07 PM »

That's the error message that is stopping compiling, but you have fixed the error. It now compiles and I thank you smiley
Logged

Learning C# - Graham Robinson
CWuestefeld
Supporting Member
**
Posts: 911



see users location on a map View Profile WWW Give some DonationCredits to this forum member
« Reply #3 on: March 23, 2008, 06:48:00 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#.
Logged



mediaguycouk
Supporting Member
**
Posts: 244


see users location on a map View Profile WWW Give some DonationCredits to this forum member
« Reply #4 on: March 24, 2008, 05:49:40 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.
Logged

Learning C# - Graham Robinson
Renegade
Charter Member
***
Posts: 10,364



Tell me something you don't know...

see users location on a map View Profile WWW Give some DonationCredits to this forum member
« Reply #5 on: March 30, 2008, 09:08:11 PM »

...I come from an actionscript / PHP background that just doesn't have these things...

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:

Formatted for C# with the GeSHI Syntax Highlighter [copy or print]
  1. private void Something(MyCustomClass thing)
  2. {
  3.    MyCustomClass temp;
  4.    temp = thing;
  5. }

It's a similar issue to what you have above.
Logged

Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker
Pages: [1]   Go Up
  Reply  |  New Topic  |  Print  
 
Jump to:  
   Forum Home   Thread Marks Chat! Downloads Search Login Register  

DonationCoder.com | About Us
DonationCoder.com Forum | Powered by SMF
[ Page time: 0.324s | Server load: 0.02 ]