Reputation: 77
Sorry if this seems dumb, but I've looked through other questions regarding this issue and I can't find anything. I'm transitioning from Java to C#, so I'm probably missing something to do with properties that I would have just done using methods in Java.
In Monodevelop, I keep getting
A get or set accessor expected
as an error when I compile.
Here's my code.
Program.cs:
class MainClass
{
public static void Main (string[] args)
{
Transaction myTransaction = new Transaction ();
string[] data = Console.ReadLine ().Split(',');
while (data[0] != "#") {
myTransaction.Name = data [0];
Double.TryParse (data [1], out myTransaction.Cost);
Int32.TryParse (data [2], out myTransaction.Quantity);
Console.WriteLine (Transaction);
data = Console.ReadLine ().Split(',');
}
}
}
}
Transaction Class:
using System;
public class Transaction
{
//private vars//
//string name;
//double cost;
//int quantity;
//Constructor//
public Transaction ()
{
}
//Properties//
public string Name{ get; set;}
public double Cost{ get; set;}
public int Quantity{ get; set;}
//methods//
public double CalcTotal(){
return Quantity * Cost;
}
public override String ToString{
return String.format("{0:0.00} x {1:0.00} @ ${2:0.00} Total: ${3:0.00}", Name, Quantity, Cost, CalcTotal());
}
}
}
Upvotes: 0
Views: 147
Reputation: 391724
There are several problems with your code:
ToString
method is lacking its parenthesis, the return
statement inside is likely the one giving you the error you refer to in your questionString.Format
is written with a lowercase f
, String.format
, which won't compile.ref
or out
parameters and target properties, you must target variables, like local variables or fields.Console.WriteLine(Transaction)
cannot write the class, it must write an object, so you likely want to use Console.WriteLine(myTransaction);
.Upvotes: 2
Reputation: 3938
The problem is in your ToString method, you forgot ()
public override string ToString () {
return string.Format("{0:0.00} x {1:0.00} @ ${2:0.00} Total: ${3:0.00}", Name, Quantity, Cost, CalcTotal());
}
Also you have couple more problems:
Console.WriteLine(Transaction);
should be
Console.WriteLine(myTransaction);
And properties can't be ref
and out
parameters
See working fiddle
Upvotes: 2
Reputation: 68685
In your code you are passing a property of the class as a ref
and out
. You can't do this, because C# can't know is there a backend field behind the Property.Why ??? You can simply have this property like
public double Cost
{
get { return 4.5; }
set { /* empty */ }
}
And here what will do CLR
with your out
and ref
parameters?It can't set any value to the property.
Double.TryParse (data [1], out myTransaction.Cost);
Int32.TryParse (data [2], out myTransaction.Quantity);
And here you need
Console.WriteLine (myTransaction);
not
Console.WriteLine (Transaction);
and the ToString(
) must be
public override string ToString(){
return String.format("{0:0.00} x {1:0.00} @ ${2:0.00} Total: ${3:0.00}", Name, Quantity, Cost, CalcTotal());
}
Upvotes: 1
Reputation: 12683
The issue you are having is with (I am assuming) is with your parse statements. You cann't pass an out
or ref
as a property or indexer of a class.
Transaction myTransaction = new Transaction();
string[] data = Console.ReadLine().Split(',');
while (data[0] != "#")
{
myTransaction.Name = data[0];
Double.TryParse(data[1], out myTransaction.Cost); //<-- this is the issue
Int32.TryParse(data[2], out myTransaction.Quantity); //<-- here again
Console.WriteLine(myTransaction);
data = Console.ReadLine().Split(',');
}
What you will need to do is hold the value in a seperate variable and then assign it to the Transaction
object as.
Transaction myTransaction = new Transaction();
string[] data = Console.ReadLine().Split(',');
while (data[0] != "#")
{
myTransaction.Name = data[0];
double dCost;
int dQty;
Double.TryParse(data[1], out dCost); //<-- this is hte issue
Int32.TryParse(data[2], out dQty); //<-- here again
myTransaction.Cost = dCost;
myTransaction.Quantity = dQty;
Console.WriteLine(myTransaction);
data = Console.ReadLine().Split(',');
}
So as you see we have created two additional variables in the loop dCost
& dQty
during the loop the values are parsed to the local variables, then these variables are set to the instance of the Transaction myTransaction
.
In addition there are two other small issues with the code.
ToString
should be a method as ToString()
string.format(..)
is actually string.Format()
Finally as others pointed out Console.WriteLine(Transaction);
should be Console.WriteLine(myTransaction);
Upvotes: 2