+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 13

Thread: Objects and Files

  1. #1
    Newbie sdgaf is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    8

    Objects and Files

    I've been trying to figure this out, asking advise from people but that was just misleading me and now I'm confusing on how this should be done. Can someone help me out with this code?

    This is just for me to learn so I can get a better grasp on this. This program is supposed to read a sequential access file called auto.txt, store the values into a engine object and display it on the listbox.


    Form1.cs
    Code:
    namespace Automobile
    {
        public partial class Form1 : Form
        {
            private const String FILENAME = "auto.txt";
            public ArrayList autoArray = new ArrayList();
            private int objCounter = 0; 
    
            enum autoIndex
            {
                cylinders,
                horsepower,
                torque
            };
            private void btnDisplay_Click(object sender, EventArgs e)
            {
                try 
                { 
                openBookTitleFile(); 
                }
                catch (Exception ex) 
                { 
                  MessageBox.Show("Error " + ex.ToString()); 
                 }
              }
    
            private void openBookTitleFile()
            {
                if (File.Exists(FILENAME))
                {
                    StreamReader autoReader = new StreamReader(FILENAME);
    
                    while (autoReader.Peek() != -1) // Loop until end of file
                    {
                        String autoInfo = autoReader.ReadLine();
                        String[] autoAttribute = autoInfo.Split(',');
    
                        objCounter++;
    
                        Engine eng = new Engine();
                        eng.Cylinders = autoAttribute;
                        eng.HorsePower = autoAttribute[Convert.ToInt32(autoIndex.horsepower)];
                        eng.Torque =     autoAttribute[Convert.ToInt32(autoIndex.torque)];
    
                        autoArray.Add(eng[objCounter]);
    
                    }
                    // Close the file
                    autoReader.Close();
    
                    foreach (Engine objEngine in autoArray)
                    {
                        lstbCarSpecs.Items.Add(objEngine.ToString());
                    }
                }
                else
                {
                    Console.WriteLine("File Doesn't Exist!");
                    Application.Exit();
                }
            }
        }
    }
    Engine.cs
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Automobile
    {
        class Engine
        {
            private int cylinders;
            private int horsePower;
            private int torque;
    
    
            public int Cylinders
            {
                get { return cylinders; }
                set { cylinders = value; }
            }
            public int HorsePower
            {
                get { return horsePower; }
                set { horsePower = value; }
            }
            public int Torque
            {
                get { return torque; }
                set { torque = value; }
            }
    
            public override string ToString()
            {
                return String.Format(cylinders + ", " + 
                    horsePower + ", " + torque);
            }
        }
    }
    auto.txt
    Code:
    2zz-ge,4,180,143
    Try not to be too harsh, I'm a newb

  2. #2
    Code Warrior Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana's Avatar
    Join Date
    Oct 2008
    Posts
    4,055
    Blog Entries
    6

    Re: Objects and Files

    I haven't looked at your code, but are you getting any errors or is it compiling and running but it doesn't do what you expected it to do?
    My Site | Questions and Answers | Ask Me: Termana | Last Tutorial: Ajax innerHTML
    If you can keep your head while all around you are losing theirs, you probably have a CD writer on your desktop

  3. #3
    Newbie sdgaf is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    8

    Re: Objects and Files

    Quote Originally Posted by Termana View Post
    I haven't looked at your code, but are you getting any errors or is it compiling and running but it doesn't do what you expected it to do?

    Error 1 Cannot implicitly convert type 'string[]' to 'int' D:\Projects\Projects\C# Projects\Automobile\Automobile\Form1.cs 58 37 Automobile
    Error 2 Cannot implicitly convert type 'string' to 'int' D:\Projects\Projects\C# Projects\Automobile\Automobile\Form1.cs 59 38 Automobile
    Error 3 Cannot implicitly convert type 'string' to 'int' D:\Projects\Projects\C# Projects\Automobile\Automobile\Form1.cs 60 38 Automobile
    Error 4 Cannot apply indexing with [] to an expression of type 'Automobile.Engine' D:\Projects\Projects\C# Projects\Automobile\Automobile\Form1.cs 62 35 Automobile

  4. #4
    TcM
    TcM is offline
    Code Warrior TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM's Avatar
    Join Date
    Aug 2006
    Posts
    11,461
    Blog Entries
    6

    Re: Objects and Files

    Code:
    Engine eng = new Engine();
                        eng.Cylinders = autoAttribute;
                        eng.HorsePower = autoAttribute[Convert.ToInt32(autoIndex.horsepower)];
                        eng.Torque =     autoAttribute[Convert.ToInt32(autoIndex.torque)];
    
    autoArray.Add(eng[objCounter]);
    Error 1: Cylinders is an Int, but you are trying to store a string array in that
    Error 2: HorsePower is an int, not a string.. and when you do
    Code:
    autoAttribute[Convert.ToInt32(autoIndex.horsepower)];
    it returns a string try to put that into an int.Parse
    Error 3: Torque, same as error 2
    Error 4: eng is an Engine class, not an array so you cannot make
    Code:
    eng[objCounter]
    if you want to add the engine to the array just make
    Code:
    autoArray.Add(eng);
    because it is a new instance, so the old one will be scrapped and the new one will be saved in the memory, so when adding it to the array it will add the new one

  5. #5
    Newbie sdgaf is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    8

    Re: Objects and Files

    I'm trying to use the int.Parse but it doesn't seem to be working. What am I doing wrong?

    Code:
                        eng.Cylinders = autoAttribute[int.Parse(autoIndex.cylinders)];
    Errors:

    Error 1 The best overloaded method match for 'int.Parse(string)' has some invalid arguments D:\Projects\Projects\C# Projects\Automobile\Automobile\Form1.cs 55 51 Automobile
    Error 2 Argument '1': cannot convert from 'Automobile.Form1.autoIndex' to 'string' D:\Projects\Projects\C# Projects\Automobile\Automobile\Form1.cs 55 61 Automobile

  6. #6
    TcM
    TcM is offline
    Code Warrior TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM's Avatar
    Join Date
    Aug 2006
    Posts
    11,461
    Blog Entries
    6

    Re: Objects and Files

    No man! I told you to put that into an int.Parse!

    as in

    Code:
    eng.Cylinders = int.Parse(autoAttribute[autoIndex.cylinders]);
    Did this fix the issue? What about the others?

  7. #7
    Newbie sdgaf is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    8

    Re: Objects and Files

    Quote Originally Posted by TcM View Post
    No man! I told you to put that into an int.Parse!

    as in

    Code:
    eng.Cylinders = int.Parse(autoAttribute[autoIndex.cylinders]);
    Did this fix the issue? What about the others?
    No that didn't fix the error. Now the compiler is complaining about integers

    Here's the code
    Code:
                        eng.Cylinders = int.Parse(autoAttribute[autoIndex.cylinders]);
                        eng.HorsePower = int.Parse(autoAttribute[autoIndex.horsepower]);
                        eng.Torque = int.Parse(autoAttribute[autoIndex.torque]);
    Errors

    Code:
    Error	1	Cannot implicitly convert type 'Automobile.Form1.autoIndex' to 'int'. An explicit conversion exists (are you missing a cast?)	D:\Projects\Projects\C# Projects\Automobile\Automobile\Form1.cs	55	61	Automobile
    Error	2	Cannot implicitly convert type 'Automobile.Form1.autoIndex' to 'int'. An explicit conversion exists (are you missing a cast?)	D:\Projects\Projects\C# Projects\Automobile\Automobile\Form1.cs	56	62	Automobile
    Error	3	Cannot implicitly convert type 'Automobile.Form1.autoIndex' to 'int'. An explicit conversion exists (are you missing a cast?)	D:\Projects\Projects\C# Projects\Automobile\Automobile\Form1.cs	57	58	Automobile

  8. #8
    TcM
    TcM is offline
    Code Warrior TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM's Avatar
    Join Date
    Aug 2006
    Posts
    11,461
    Blog Entries
    6

    Re: Objects and Files

    Ok man, did you ever program in C#?

    Just add a ToString();

    like

    Code:
    eng.Cylinders = int.Parse(autoAttribute[autoIndex.cylinders].ToString());

  9. #9
    Newbie sdgaf is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    8

    Re: Objects and Files

    Quote Originally Posted by TcM View Post
    Ok man, did you ever program in C#?

    Just add a ToString();

    like

    Code:
    eng.Cylinders = int.Parse(autoAttribute[autoIndex.cylinders].ToString());
    I actually just started learning C#... and the ToString() doesn't work

    I somehow was able to get it to work! Just deleted enum!

    Code:
               
    eng.Cylinders  = int.Parse(autoAttribute[0]);
    eng.HorsePower = int.Parse(autoAttribute[1]);
    eng.Torque     = int.Parse(autoAttribute[2]);
    I really appreciate your help, thanks!

  10. #10
    TcM
    TcM is offline
    Code Warrior TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM's Avatar
    Join Date
    Aug 2006
    Posts
    11,461
    Blog Entries
    6

    Re: Objects and Files

    Ow yeah, I did not read the error because I assumed it was the ToString() BUT the enum was the problem lol, because the array index needs an int and not an enum.. although you can get a value of an enum.. anyways, any more problems?

+ Reply to Thread
Page 1 of 2
1 2 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Tutorial: Visual Studio 2008 C# Compressing
    By Jordan in forum CSharp Tutorials
    Replies: 5
    Last Post: 08-30-2009, 07:25 AM
  2. Replies: 0
    Last Post: 10-16-2008, 07:21 PM
  3. Visual Studio 2008: C# Reading Files
    By Jordan in forum CSharp Tutorials
    Replies: 8
    Last Post: 06-20-2008, 10:54 AM
  4. DataGridView objects and MySQL databases
    By mholt in forum C# Programming
    Replies: 1
    Last Post: 04-01-2008, 09:46 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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