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
Engine.csCode: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(); } } } }
auto.txtCode: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); } } }
Try not to be too harsh, I'm a newbCode:2zz-ge,4,180,143
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?
Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!
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
Error 1: Cylinders is an Int, but you are trying to store a string array in thatCode: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 2: HorsePower is an int, not a string.. and when you doit returns a string try to put that into an int.ParseCode:autoAttribute[Convert.ToInt32(autoIndex.horsepower)];
Error 3: Torque, same as error 2
Error 4: eng is an Engine class, not an array so you cannot makeif you want to add the engine to the array just makeCode:eng[objCounter]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 oneCode:autoArray.Add(eng);![]()
I'm trying to use the int.Parse but it doesn't seem to be working. What am I doing wrong?
Errors:Code:eng.Cylinders = autoAttribute[int.Parse(autoIndex.cylinders)];
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
No man! I told you to put that into an int.Parse!
as in
Did this fix the issue? What about the others?Code:eng.Cylinders = int.Parse(autoAttribute[autoIndex.cylinders]);
No that didn't fix the error. Now the compiler is complaining about integers
Here's the code
ErrorsCode:eng.Cylinders = int.Parse(autoAttribute[autoIndex.cylinders]); eng.HorsePower = int.Parse(autoAttribute[autoIndex.horsepower]); eng.Torque = int.Parse(autoAttribute[autoIndex.torque]);
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
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!![]()
I really appreciate your help, thanks!Code:eng.Cylinders = int.Parse(autoAttribute[0]); eng.HorsePower = int.Parse(autoAttribute[1]); eng.Torque = int.Parse(autoAttribute[2]);
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?
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks