CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2011
    Posts
    41

    I am brand new to C-sharp and need a little guidance

    I am using Visual Studio C# 2010.
    I am working on a homework assignment and need a little help. I am not necessarily looking for the answer, but for some guidance to figure out what I am doing wrong. I had to write a program that took a txt file. (DecodeThis.txt) and write it to the screen, but it was also written backwards. This is the code I used to accomplish this:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace Day2Homework
    {
        class Program
        {
            static void Main(string[] args)
            {
                StreamReader myReader = newStreamReader("DecodeThis.txt");
                string line = "";
    
             
    
                while (line != null)
                {
                    line = myReader.ReadLine();
                    if (line != null)
                    {
                        char[] charArray = line.ToCharArray();
                        Array.Reverse(charArray);
                        Console.WriteLine(charArray);
    
    
                    }
             
                }
                  
                myReader.Close();
                Console.ReadLine();
            }
        }
    }
    Then I needed to write this reversed text to a text file. This is where I am stumped. I looked up an article on MSDN but when I tried that approach I couldn't get it to work. Here is what I tried:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace Day2Homework
    {
        class Program
        {
            static void Main(string[] args)
            {
                StreamReader myReader = new StreamReader("DecodeThis.txt");
                string line = "";
    
             
    
                while (line != null)
                {
                    line = myReader.ReadLine();
                    if (line != null)
                    {
                        char[] charArray = line.ToCharArray();
                        Array.Reverse(charArray);
                        
                        using (StreamWriter sw = new StreamWriter("Decoded.txt"))
                        {
                            foreach(char array in charArray)
                            
                                sw.WriteLine(array.Name);
                            }
                        
    
    
    
    
                    }
             
                }
                  
                myReader.Close();
                Console.ReadLine();
            }
        }
    }
    the compiler is telling me that char does not have a definition for name. Where do I go from here? Am I close?

    Thanks in advance

  2. #2
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    Re: I am brand new to C-sharp and need a little guidance

    Thank you for being honest about an assignment and seeking just the help you need to fight your way through it!

    As to your current issue with the compiler, the 'char' data type is a single character and does not have a "Name" property. Using WriteLine(), you should be able to simply pass your char variable and it's content will be written.

    Code:
    foreach(char  array in charArray)
    {                        
        sw.WriteLine(array);
    }
    But a couple of tips, look a little further into the WriteLine() method of streamwriter, and see if there is a better way to output your character array.

    Also, when naming your variables, try to use meaningful names as not to get yourself confused when following your code. When you "foreach()" through the array, you are interating over each individual character in the collection, so calling each character an "array" is confusing... I would call it something like "thisChar" or "thisCharacter".

    There are many other ways to "fancify" your project, but it is best you find those with practice and researching .Net objects and their methods/properties... best way to learn.

    Best of luck!

  3. #3
    Join Date
    Oct 2004
    Location
    Rocket City
    Posts
    220

    Re: I am brand new to C-sharp and need a little guidance

    You made a good start with this project. Another suggestion:
    using (StreamWriter sw = new StreamWriter("Decoded.txt"))
    You might not want to create a new Streamwriter (and file) for each line read in. The final result might have only the last line in the output file.

  4. #4
    Join Date
    Dec 2009
    Posts
    18

    Re: I am brand new to C-sharp and need a little guidance

    Its also good to remove items you will not use.
    Your line
    Code:
    using System.Linq;
    I'm sure you are not using Linq so you can remove it. Also in your references you can remove
    System.Xml.Linq and you probably wont be using System.Xml either.

  5. #5
    Join Date
    May 2011
    Posts
    41

    Re: I am brand new to C-sharp and need a little guidance

    Thanks for everyone's help. fcronin got me thinking. I found a better article on msdn. This is what I came up with and it actually worked. I was a little surprised. I guess a little luck never hurt. I am posting the code for you guys to look at. The knowledge is the most important thing to me, but also good habits and "clean" code. Any tips or adulation would be great.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    
    namespace Day2Homework
    {
        class Program
        {
            static void Main(string[] args)
            {
                StreamReader myReader = new StreamReader("DecodeThis.txt");
                string line = "";
    
                //Article used for writing to file http://msdn.microsoft.com/en-us/library/aa287548.aspx
               
                
                StreamWriter myWriter = new StreamWriter("Decoded.txt");
    
                Console.WriteLine("Starting to write to file.");
    
    
    
    
                while (line != null)
                {
                    line = myReader.ReadLine();                       //This while loop reads each line until null
                    if (line != null)
                    {
                        char[] charArray = line.ToCharArray();    //Converts a string to Character array
                        Array.Reverse(charArray);                      //Reverses the out put
    
                        myWriter.WriteLine(charArray);
    
                        //Console.WriteLine(charArray); //Writes it to the screen new code for writing to file*
    
    
                    }
    
                }
    
                myReader.Close();
                myWriter.Close();
    
                Console.WriteLine("Finished writing to file.");  //Using to acknowledge file has been written*
    
                Console.ReadLine();
            }
        }
    }

  6. #6
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    Re: I am brand new to C-sharp and need a little guidance

    Kudos! Much improved solution, clean and easy to follow, outstanding!

    Couple quick questions... a bit rhetorical actually, you can think on it more than need to answer them... but anyhow...

    Is the character array a requirement in your design? Since ReadLine() provides you with a string, and you can reverse a string, and you can write a string to your stream... why convert to an array?

    If there is a carriage return at the end of each line, are you getting a blank line at the beginning of your new file?

    Sometimes, you can compress code a bit by combining functions into a still easy to read line, it never huts to play with in-line statements to gain berevity and clean blocks...

    Code:
                    StreamReader myReader = new StreamReader("file.txt");
                    string thisLine = string.Empty;
                    while ((thisLine = myReader.ReadLine()) != null)
                    {
                        // do stuff with my thisLine...
                    }
    ... this while() loop takes care of your while(), readline(), and if() at once...

  7. #7
    Join Date
    Dec 2009
    Posts
    18

    Re: I am brand new to C-sharp and need a little guidance

    Here is another trick I like to use.

    For Example say you are making extensive use of Environment.NewLine
    Code:
    MessageBox.Show("blah" + Environment.NewLine + "blah" + Environment.NewLine + "blah");
    you can cheat and do this
    Code:
    using nl = System.Environment;
    
    MessageBox.Show("blah" + nl.NewLine + "blah" + nl.NewLine + "blah");

  8. #8
    Join Date
    Jun 2008
    Posts
    2,477

    Re: I am brand new to C-sharp and need a little guidance

    Or you could just do what you should be doing anyway for concatenations like that and use String.Format:

    Code:
    var formatted = String.Format( "blah{0}blah{0}blah{0}blah", Environment.NewLine );

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured