C
C#13mo ago
Dinny

I can't figure out how to fix this error for my input file

if( nextLine != null)
{
string[] nextItem = nextLine.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
if (nextItem.Length == 0) { continue; }

//BOXCAR
if (nextItem[0].Equals("Box"))
{
//do something
boxcar = Convert.ToInt32(nextItem[1], nextItem[2], nextItem[3]);
writer.WriteLine(boxcar.Volume());
}

//TANKCAR
if (nextItem[0].Equals("Tank"))
{
//do something
tankcar = Convert.ToInt32(nextItem[1], nextItem[2]);
writer.WriteLine(tankcar.Volume());
}

//REFRIGERATOR
if (nextItem[0].Equals("Refrigerator"))
{
//do something
refrig = Convert.ToInt32(nextItem[1], nextItem[2], nextItem[3], nextItem[4]);
writer.WriteLine(refrig.Volume());
}
}
if( nextLine != null)
{
string[] nextItem = nextLine.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
if (nextItem.Length == 0) { continue; }

//BOXCAR
if (nextItem[0].Equals("Box"))
{
//do something
boxcar = Convert.ToInt32(nextItem[1], nextItem[2], nextItem[3]);
writer.WriteLine(boxcar.Volume());
}

//TANKCAR
if (nextItem[0].Equals("Tank"))
{
//do something
tankcar = Convert.ToInt32(nextItem[1], nextItem[2]);
writer.WriteLine(tankcar.Volume());
}

//REFRIGERATOR
if (nextItem[0].Equals("Refrigerator"))
{
//do something
refrig = Convert.ToInt32(nextItem[1], nextItem[2], nextItem[3], nextItem[4]);
writer.WriteLine(refrig.Volume());
}
}
47 Replies
Dinny
DinnyOP13mo ago
on the lines where it says "Convert.To........" there's errors saying i can't do that, but i don't know how to work around it because i need those numbers in the files to be numbers, not strings i am using an abstract class as a base and reated derived classes and then made instances of those derived classes
RailroadCar tankcar = new TankCar();
RailroadCar boxcar = new BoxCar();
RailroadCar refrig = new Refrigerator();
RailroadCar tankcar = new TankCar();
RailroadCar boxcar = new BoxCar();
RailroadCar refrig = new Refrigerator();
Angius
Angius13mo ago
What are you trying to convert to that integer? Three values all at once? Or are you hoping that by passing '3', '4', '5' it will somehow figure out you want to convert "345"? Not to mention, you're converting to int, like the name of the method implies... and trying to save the result in a variable of type BoxCar None of that makes any modicum of sense, I'm afraid
Dinny
DinnyOP13mo ago
hell, i need a cig at first i had it as, for example, "boxcar = Convert.ToInt32(nextItem[1]); because i wanted to take the value read from the input file and make it an int instead of a string
boxcar = Convert.ToInt32(nextItem[1]);
boxcar = Convert.ToInt32(nextItem[1]);
Angius
Angius13mo ago
Yes, that's how you can use Convert.ToInt32(). It turns a string into an integer, correct
Dinny
DinnyOP13mo ago
i did that but i still kept getting an error
Angius
Angius13mo ago
Because boxcar is of type RailroadCar Not int Square peg does not fit round hole
Dinny
DinnyOP13mo ago
i get that, but i am unsure how to work around it because i need them to be int and then i need to take those values and run them thru my Volume() method am i to create just a random variables in my main program ?
Angius
Angius13mo ago
Try... using a different variable? Don't assign an integer to a railroad car? Make another variable for that integer Then you can do boxcar.Something = theNewVariable And boxcar.SomethingElse = anotherNewIntegerVariable Alternatively, you can skip that step
boxcar.ThingOne = Convert.ToInt32(...);
boxcar.ThingTwo = Convert.ToInt32(...);
boxcar.ThingThree = Convert.ToInt32(...);
boxcar.ThingOne = Convert.ToInt32(...);
boxcar.ThingTwo = Convert.ToInt32(...);
boxcar.ThingThree = Convert.ToInt32(...);
Dinny
DinnyOP13mo ago
okay, i will try that, pleadse allow me a moment okay so still using boxcar as the exmaple i already have the instance
RailroadCar boxcar = new BoxCar();
RailroadCar boxcar = new BoxCar();
do i keep this and use the boxcar.ThingOne example, or completely get rid of that instance ? because I am running into an error still since it is a method group
Angius
Angius13mo ago
You keep that Alternatively, you can use initializer syntax
RailroadCar boxCar = new BoxCar {
ThingOne = Convert.ToInt32(...),
ThingTwo = Convert.ToInt32(...),
ThingThree = Convert.ToInt32(...)
};
RailroadCar boxCar = new BoxCar {
ThingOne = Convert.ToInt32(...),
ThingTwo = Convert.ToInt32(...),
ThingThree = Convert.ToInt32(...)
};
And initialize all three properties in one go That said, unless you're not calling a method somewhere but are tryint to access it like a property, you should not be getting any errors related to method groups
Dinny
DinnyOP13mo ago
i still don’t get what i’m doing wrong 😭😭
No description
Angius
Angius13mo ago
GetLength is a method A getter at that You cannot assign values to methods You can only assign values to properties The car should have properties for width, height, and length
public abstract class RailroadCar
{
public int Length { get; set; }
public int Width { get; set; }
public int Height { get; set; }
}
public abstract class RailroadCar
{
public int Length { get; set; }
public int Width { get; set; }
public int Height { get; set; }
}
With that, you can do
public class BoxCar : RailroadCar
{ }
public class BoxCar : RailroadCar
{ }
and
RailroadCar car = new BoxCar();
car.Length = 69;
RailroadCar car = new BoxCar();
car.Length = 69;
Dinny
DinnyOP13mo ago
namespace Module_9
{
public abstract class RailroadCar
{
//data fields
private double length;

//constr
protected RailroadCar(double length = 0.00)
{
SetLength(length);
}

//GETTERS
/** Returns length of solid.
@return: solid length
*/
public double GetLength() { return length; }

/** Updates length of solid.
@return: updated length.
*/
//SETTERS
public void SetLength(double length) { this.length = length;}

/** Calculates the volume of the solid.
@return: calculated volume of the solid
*/
public abstract double Volume();
}
}
namespace Module_9
{
public abstract class RailroadCar
{
//data fields
private double length;

//constr
protected RailroadCar(double length = 0.00)
{
SetLength(length);
}

//GETTERS
/** Returns length of solid.
@return: solid length
*/
public double GetLength() { return length; }

/** Updates length of solid.
@return: updated length.
*/
//SETTERS
public void SetLength(double length) { this.length = length;}

/** Calculates the volume of the solid.
@return: calculated volume of the solid
*/
public abstract double Volume();
}
}
this is my railroad car file he wanted it to look specifically like this
Angius
Angius13mo ago
Then you need to use .SetLength() car.SetLength(69) You're doing a lot of useless work here, but sure, if that's what you need go ahead
Dinny
DinnyOP13mo ago
i've been tild my prof is insaely out of date and makes us do a lot of unnecessary work 😭 but issn't that still a method group ? and we won't know the specific numbers he's using because he uses his own input fil file
Angius
Angius13mo ago
You were trying to assign to a method This is calling the method And instead of 69 use your converted number I used a hardcoded number for brevity
Dinny
DinnyOP13mo ago
so i'd do
boxcar.SetLength(Convert.ToInt32(nextItem[1]));
boxcar.SetLength(Convert.ToInt32(nextItem[1]));
Angius
Angius13mo ago
Yep
Dinny
DinnyOP13mo ago
or no okay ill try that plz hold okay that;s working, but that's not working for width and height
Angius
Angius13mo ago
Do you have setter methods for width and height?
Dinny
DinnyOP13mo ago
i do yes
namespace Module_9
{
public class BoxCar : RailroadCar
{
//data fields
private double width;
private double height;

public BoxCar(double width = 0.0, double height = 0.0, double length = 0.0)
{
SetWidth(width);
SetHeight(height);
SetLength(length);
}

//GETTERS
/** Returns width of solid.
@return: solid width.
*/
public double GetWidth() { return width; }

/** Returns width of solid.
@return: solid width.
*/
public double GetHeight() { return height; }


//SETTERS
/** Updates width of solid.
@return: updated width.
*/
public void SetWidth(double width) { this.width = width; }

/** Updates height of solid.
@return: updated height.
*/
public void SetHeight(double height) { this.height = height; }

/** Calculates the volume of the solid.
@return: calculated volume of the solid
*/
public override double Volume() { return GetLength() * width * height; }
}
}
namespace Module_9
{
public class BoxCar : RailroadCar
{
//data fields
private double width;
private double height;

public BoxCar(double width = 0.0, double height = 0.0, double length = 0.0)
{
SetWidth(width);
SetHeight(height);
SetLength(length);
}

//GETTERS
/** Returns width of solid.
@return: solid width.
*/
public double GetWidth() { return width; }

/** Returns width of solid.
@return: solid width.
*/
public double GetHeight() { return height; }


//SETTERS
/** Updates width of solid.
@return: updated width.
*/
public void SetWidth(double width) { this.width = width; }

/** Updates height of solid.
@return: updated height.
*/
public void SetHeight(double height) { this.height = height; }

/** Calculates the volume of the solid.
@return: calculated volume of the solid
*/
public override double Volume() { return GetLength() * width * height; }
}
}
my boxcar file
Angius
Angius13mo ago
Ah, see, those required double Not int
Dinny
DinnyOP13mo ago
im gonna throw up 😭 it's like he enjpys being difficult
Angius
Angius13mo ago
It's not difficult
Dinny
DinnyOP13mo ago
even then tho
Angius
Angius13mo ago
Convert to double instead of it ¯\_(ツ)_/¯
Dinny
DinnyOP13mo ago
I can't call SetWidth or height ik that but i cant call width ot height i keep getting an error
Angius
Angius13mo ago
That error being...?
Dinny
DinnyOP13mo ago
oh wait hang on i think i figured it out i did NOT figure it out LMAO it's saying that there's no defintion for SetWidth in the base class ik boxcar is derived from the base class, but boxcar has width and height
Angius
Angius13mo ago
Probably because the variable is of RailroadCar type, not BoxCar
Dinny
DinnyOP13mo ago
:00000 ok plz hold okay so i fixed that issue, now when i try writing the actual volumes to the output file, it's blank
Angius
Angius13mo ago
How are you writing the file?
Dinny
DinnyOP13mo ago
writer.WriteLine(boxcar4.Volume());
writer.WriteLine(boxcar4.Volume());
i can't put parameters either bc ill get an error
Angius
Angius13mo ago
Again What error
Dinny
DinnyOP13mo ago
itll say there's no parameters for the method Volume()
Angius
Angius13mo ago
Does the method take any parameters?
Dinny
DinnyOP13mo ago
no, that;s why i left it blank, but i don't know how else to return the volume
Angius
Angius13mo ago
What is the error exactly?
Dinny
DinnyOP13mo ago
when i try adding parameters, it says "no overload for method 'Volume' takes 3 arguments" otherwise there's no errors
Angius
Angius13mo ago
You said the method doesn't take any parameters Why are you trying to give it 3 parameters then?
Dinny
DinnyOP13mo ago
yes i know that;s why i left it blank im saying im teying to find out how to use those number to calc the volume, but don't know how since there's no parameters the method only returns the volume
Angius
Angius13mo ago
You do it inside the volume method And you use the properties of the class to do that Or, well, fields in your case
Dinny
DinnyOP13mo ago
could u explain a bit more please, im not following
MODiX
MODiX13mo ago
Angius
REPL Result: Success
class Rectangle
{
public int Width { get; set; }
public int Height { get; set; }

public int CalculateArea()
{
return Width * Height;
}
}

var rect = new Rectangle();
rect.Width = 69;
rect.Height = 420;
Console.Write($"The area of this rectangle is {rect.CalculateArea()}");
class Rectangle
{
public int Width { get; set; }
public int Height { get; set; }

public int CalculateArea()
{
return Width * Height;
}
}

var rect = new Rectangle();
rect.Width = 69;
rect.Height = 420;
Console.Write($"The area of this rectangle is {rect.CalculateArea()}");
Console Output
The area of this rectangle is 28980
The area of this rectangle is 28980
Compile: 781.215ms | Execution: 65.533ms | React with ❌ to remove this embed.
Dinny
DinnyOP13mo ago
what i have in the volume method is already there tho in all the files this is all so confusing what's up there is already in my files , hto not exactly to fit what i have to do for my assignment
public override double Volume() { return GetLength() * width * height; }
public override double Volume() { return GetLength() * width * height; }
Angius
Angius13mo ago
Yeah, this looks like it would calculate the volume
Dinny
DinnyOP13mo ago
okay good if that's right then, idk why my output file is still blank 😭
Want results from more Discord servers?
Add your server