File.IO issues with class

I am having issues writting to a file with my c# class.

Page 1 of 1

7 Replies - 1937 Views - Last Post: 04 October 2010 - 04:32 PM Rate Topic: -----

#1 darkness3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-May 10

File.IO issues with class

Posted 04 October 2010 - 01:10 PM

Hello,

I am wanting to write the text from my text box to a text file. I have used stream writers and stream readers before, even this same code and it has worked fine in a windows form. But this time I am using it in a class file, and its not working right.

This is my class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Pets_Class
{
    class PetReadWriteClass
    {
        // Declaring the Class Vars
        private String StrPetName;
        private String StrPetColor;
        private String StrPetBreed;
        private String StrPetType;
        int indexIntName;
        int indexIntColor;
        int indexIntBreed;
        int indexIntType;       
        // My Methods Propertys For My Class
        
        private String PetName
        {
            get
            {
                return StrPetName;
            }
           set
            {
               StrPetName = value;
            }
        }
        public String PetColor
        {
            get
            {
                return StrPetColor;
            }
            set
            {
                StrPetColor = value;
            }
        }
        public String PetBreed
        {
            get
            {
                return StrPetBreed;
            }
            set
            {
                StrPetBreed = value;
            }
        }
        public String PetType
        {
            get
            {
                return StrPetType;
            }
            set
            {
                StrPetType = value;
            }
        }
        // My Constructors For My Class
        public PetReadWriteClass(String StrPetName, String StrPetType, String StrPetColor, String StrPetBreed)
        {
            PetName = StrPetName;
            PetColor = StrPetColor;
            PetType = StrPetType;
            PetBreed = StrPetBreed;
            
        }

        public PetReadWriteClass()
        {
            // TODO: Complete member initialization
        }

        // My Method To Handle The IO For This Class
        public void WriteToFiles()
        {
            // Setting the method to read the textbox.text method from the form
            // Then write the string to the txt files
            // My arrays for the txt files, with index ints
            String[] ArrayPetName = new String[150];
            
            String[] ArrayPetColor= new String[150];
            
            String[] ArrayPetBreed = new String[150];
            
            String[] ArrayPetType= new String[150];

            
            ArrayPetName[indexIntName++] = PetName;
            File.WriteAllLines(@"C:\Visual Studio 2010\Projects\Pets Class\Pets Class\bin\Debug\PetName.txt", ArrayPetName);
            ArrayPetColor[indexIntColor++] = PetColor;
            File.WriteAllLines(@"C:\Visual Studio 2010\Projects\Pets Class\Pets Class\bin\Debug\PetColor.txt", ArrayPetColor);
            ArrayPetBreed[indexIntBreed++] = PetBreed;
            File.WriteAllLines(@"C:\Visual Studio 2010\Projects\Pets Class\Pets Class\bin\Debug\PetBreed.txt", ArrayPetBreed);
            ArrayPetType[indexIntType++] = PetType;
            File.WriteAllLines(@"C:\Visual Studio 2010\Projects\Pets Class\Pets Class\bin\Debug\PetType.txt", ArrayPetType);
            
            
        }
        
    }
}




And My button in my form fires the WriteToFiles() Method

Its like the array is getting reset every time the button is getting fired, because the last string typed into the text box is the only one that is going into the text file.

Any help will be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: File.IO issues with class

#2 Skaggles   User is offline

  • THE PEN IS MIGHTIER
  • member icon





Reputation: 255
  • View blog
  • Posts: 641
  • Joined: 01-March 09

Re: File.IO issues with class

Posted 04 October 2010 - 01:15 PM

View Postdarkness3, on 04 October 2010 - 11:10 AM, said:

        public void WriteToFiles()
        {
            String[] ArrayPetName = new String[150];
            
            String[] ArrayPetColor= new String[150];
            
            String[] ArrayPetBreed = new String[150];
            
            String[] ArrayPetType= new String[150];



Its like the array is getting reset every time the button is getting fired, because the last string typed into the text box is the only one that is going into the text file.

You array IS being recreated with every call to WriteToFiles() because you create it within that method.

This post has been edited by Skaggles: 04 October 2010 - 01:15 PM

Was This Post Helpful? 0
  • +
  • -

#3 darkness3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-May 10

Re: File.IO issues with class

Posted 04 October 2010 - 01:31 PM

I removed the string[] StrName = new String[150] from the WriteToFiles method, and placed it at the top with the rest of the variables(I had it there before), and it still does it same thing.
Was This Post Helpful? 0
  • +
  • -

#4 Robin19   User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: File.IO issues with class

Posted 04 October 2010 - 02:19 PM

Where are you setting the values in the arrays. I have a feeling it is saving to the file just fine, but the arrays do not have the information you think they hold.
Was This Post Helpful? 0
  • +
  • -

#5 darkness3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-May 10

Re: File.IO issues with class

Posted 04 October 2010 - 02:28 PM

View PostRobin19, on 04 October 2010 - 01:19 PM, said:

Where are you setting the values in the arrays. I have a feeling it is saving to the file just fine, but the arrays do not have the information you think they hold.


I am setting them in the WriteToFiles Method, and no its not getting saved to the file right.
Was This Post Helpful? 0
  • +
  • -

#6 darkness3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-May 10

Re: File.IO issues with class

Posted 04 October 2010 - 03:54 PM

I think I got it now. Had to use File.AppendAllText instead of File.WriteAllLines, not sure why it wouldn't work since it worked last time, but its saving to file right now. I will repost the code once I am done. Thanks for the input.
Was This Post Helpful? 0
  • +
  • -

#7 darkness3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-May 10

Re: File.IO issues with class

Posted 04 October 2010 - 04:21 PM

Alright everything works on the stream writer side now. Here is the code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Pets_Class
{
    class PetReadWriteClass
    {
        // Declaring the Class Vars
        private String StrPetName;
        private String StrPetColor;
        private String StrPetBreed;
        private String StrPetType;
        String[] ArrayPetName = new String[150];
        String[] ArrayPetColor = new String[150];
        String[] ArrayPetBreed = new String[150];
        String[] ArrayPetType = new String[150];
        int indexIntName;
        int indexIntColor;
        int indexIntBreed;
        int indexIntType;       
        // My Methods Propertys For My Class
        
        public String PetName
        {
            get
            {
                return StrPetName;
            }
           set
            {
               StrPetName = value;
            }
        }
        public String PetColor
        {
            get
            {
                return StrPetColor;
            }
            set
            {
                StrPetColor = value;
            }
        }
        public String PetBreed
        {
            get
            {
                return StrPetBreed;
            }
            set
            {
                StrPetBreed = value;
            }
        }
        public String PetType
        {
            get
            {
                return StrPetType;
            }
            set
            {
                StrPetType = value;
            }
        }
        // My Constructors For My Class
        public PetReadWriteClass(String StrPetName, String StrPetType, String StrPetColor, String StrPetBreed)
        {
            PetName = StrPetName;
            PetColor = StrPetColor;
            PetType = StrPetType;
            PetBreed = StrPetBreed;
        }

        // My Method To Handle The IO For This Class
        public void WriteToFiles()
        {
            // Setting the method to read the textbox.text method from the form
            // Then write the string to the txt files
            // File name strings, they will be placed in the projects bin foulder by default
            String StrPetNameFile = "PetName.txt";
            String StrPetColorFile = "PetColor.txt";
            String StrPetTypeFile = "PetType.txt";
            String StrPetBreedFile = "PetBreed.txt";
            // The Strings That will be stored to the files, and their arguments
            String StrWriteNameToFile = PetName + "\r\n";
            String StrWriteColorToFile = PetColor + "\r\n";
            String StrWriteTypeToFile = PetType + "\r\n";
            String StrWriteBreedToFile = PetBreed + "\r\n";
            // My arrays for the txt files, with index ints
            ArrayPetName[indexIntName++] = PetName;
            File.AppendAllText(StrPetNameFile, StrWriteNameToFile);
            ArrayPetColor[indexIntColor++] = PetColor;
            File.AppendAllText(StrPetColorFile, StrWriteColorToFile);
            ArrayPetBreed[indexIntBreed++] = PetBreed;
            File.AppendAllText(StrPetBreedFile, StrWriteBreedToFile);
            ArrayPetType[indexIntType++] = PetType;
            File.AppendAllText(StrPetTypeFile, StrWriteTypeToFile);
        }        
    }
}



Now for the stream reader code, hope I won't have any issues with that.
Was This Post Helpful? 0
  • +
  • -

#8 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: File.IO issues with class

Posted 04 October 2010 - 04:32 PM

It is doing exactly what you are telling it to do. :)

Calling it:

private void button1_Click(object sender, EventArgs e)
{
    PetReadWriteClass prwc = new PetReadWriteClass("bozo", "dog", "brown", "mutt");
    prwc.WriteToFiles();
}



Produces "PetBreed.txt" contents:

C:\TEMP>od -bc petbreed.txt
0000000 155 165 164 164 015 012 015 012 015 012 015 012 015 012 015 012
          m   u   t   t  \r  \n  \r  \n  \r  \n  \r  \n  \r  \n  \r  \n
0000020 015 012 015 012 015 012 015 012 015 012 015 012 015 012 015 012
         \r  \n  \r  \n  \r  \n  \r  \n  \r  \n  \r  \n  \r  \n  \r  \n
*
0000460

C:\TEMP>



The array is empty except for the one entry- mutt.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1