Im trying to use this method called GetDouble but i keep getting the error "no overload for method 'GetDouble' takes 1 arguments" and i have no idea how to fix it.
public static bool GetDouble(string StringToConvert, out double dblOutValue)
{
return double.TryParse(StringToConvert, out dblOutValue);
}
im calling it from this method
private bool ReadAndValidatePrice(out double price)
{
price = 0.0;
if (!InputUtility.GetDouble(txtPrice.Text))
{
}
}
Any help trying to fix it is appriciated, thanks.
As the error is trying to tell you, GetDouble() takes two arguments.
The second argument is an out parameter, meaning that it will set the variable you pass to it as a result.
When calling it, you must pass a variable with the out keyword.
You have a few issues here. The first is your GetDouble method. What are you trying to do? If you are trying to just get the double (as the name implies) then it should be more like
public static double GetDouble(string StringToConvert)
{
double dblOutValue;
double.TryParse(StringToConvert, out dblOutValue);
return dblOutValue;
}
Then look at your ReadandValidatePrice method. With the above change it should be more like:
private bool ReadAndValidatePrice(out double price)
{
price = InputUtility.GetDouble(txtPrice.Text)
//not sure if the IF statement is needed anymore so it is omitted
}
How is that? Where my assumptions completely wrong?
You miss one parameter ,GetDouble() takes two parameters. But you have pass one parameter, So you got that error . Now try below method instead of your method .
private bool ReadAndValidatePrice(out double price)
{
price = 0.0;
if (!InputUtility.GetDouble(txtPrice.Text, out price ))
{
}
}
Your method should look like follow, because your GetDouble() method defines two parameter:
private bool ReadAndValidatePrice(out double price)
{
price = 0.0;
if (!InputUtility.GetDouble(txtPrice.Text, out price))
{
}
}
Related
I created a structure which contains several fields.
In that structure I have a property which calls a method which then creates a string out of those number for logging purposes.
When I use the property in the structure I get different decimal points Vs when I directly call the actual method which creates the log-string!
Suppose I have 9990M, if I use the structure it is 9990.0000 and when I call the method directly it prints 9990.00
This is my Method :
private static string CreateLog(
long userId, decimal amount,
long transactionID, decimal totalCash)
{
Values = string.Format("{0}{1}{2}{3}",
amount,
userId,
transactionId,
totalCash);
return Values;
}
And my structure looks like this:
public struct AccountStruct
{
public long USER_ID;
public decimal AMOUNT;
public long TRANSACTION_ID;
public decimal CURRENT_CASH;
string ValuesToBeLoged
{
get
{
return CreateLog(this);
}
}
}
And CreateLog looks like this (calls the former method)
private static string CreateLog(AccountStruct accinfo)
{
return CreateLog( accinfo.USER_ID,
accinfo.AMOUNT,
accinfo.TRANSACTION_ID,
accinfo.CURRENT_CASH);
}
Why is it like this ? whats the problem here?
Probably some sort of internal normalization is going on. If you care about such matters, you need to specify how many decimal places you want in the ToString or String.Format method.
Look up Decimal.ToString(String) for your options. Usually I use the "Nx" where x is the number of decimal places.
I have the following code:
private double Input1
{
get
{
double x;
return double.TryParse(Input1TextBox.Text, out x) ?
x * (Prefix1ComboBox.SelectedItem as MetricPrefix).Multiplier : double.NaN;
}
}
I was wondering if there's anyway to omit the variable declaration and get this all on one line, or if this is the only possible way.
You can create your own Util class with custom TryParse which would return double? instead:
public static class Util
{
public static double? TryParse(string source)
{
double x;
if (double.TryParse(source, out x))
return x;
return null;
}
}
and then use it:
return (Util.TryParse(Input1TextBox.Text) ?? double.NaN) * (Prefix1ComboBox.SelectedItem as MetricPrefix).Multiplier;
It uses the fact that double.NaN multiplied by other number gives NaN as well.
Unfortunately, no. However, is this a major problem? You can mask this by creating an extension method to do it for you though, if it's not aesthetically pleasing:
get
{
return Input1TextBox.Text.ConvertTo<double>(double.Nan);
}
And implement the extension method there to do the defaulting.
This is the only possible way, unless you decide to cheat by making x a class member instead of a local. Or if you decide to cheat by hiding everything inside another method.
There is no real benefit in cutting down the number of newlines though.
No you need to declare the variable first to use TryParse.
It can as of 2021 in C#7 (possible earlier, unconfirmed), using modern notation.
class MainForm
{
//...
private double Input1 =>
double.TryParse(Input1TextBox.Text, out double x) ? x * (Prefix1Combo.SelectedItem as MetricPrefix).Multiplier : double.NaN;
//...
}
You simply denote the type (double in this example) along with the out parameter modifier, which will effectively create a local variable of the name passed in the parameter modifier to be used anywhere in the code AFTER called, in-line.
Note that the type could also be inferred with var instead of the type, and the legacy notation which is more along the lines of how it was originally written:
class MainForm
{
//...
private double Input1
{
return double.TryParse(Input1TextBox.Text, out var x) ?
x * (Prefix1ComboBox.SelectedItem as MetricPrefix).Multiplier : double.NaN;
}
//...
}
I have a basic class with this method including
public class Account
{
//MEMBERS
private int acctNo;
protected double balance;
public double deposit;
// CONSTRUCTORS
public Account() //member intitilization
{
acctNo = 54534190;
balance = 7500;
deposit= 1500;
}
//PROPERTIES
public int AcctNo
{
get {return acctNo; }
set {acctNo = value; }
}
public double Balance
{
get { return balance; }
set { balance = value; }
}
public double Deposit
{
get {return deposit; }
set {deposit = value; }
}
public virtual double getDeposit (double amount)
{
double transactionAmt=0.00;
if (amount>0)
{
balance+=amount;
transactionAmt= amount;
}
return transactionAmt;
}
Now in my actual program I am trying to output this method. What would my writeline look like?
I tried to write this:
static void Main(string[] args)
{
Console.WriteLine("CREATING ACCOUNT");
Account myAcctDefault = new Account();
DumpContents(myAcctDefault);
Pause();
}
static void DumpContents(Account account)
{
Console.WriteLine(" output {0}", account.getDeposit());
}
I am getting an error saying:
no overload for method 'getDeposit' takes 0 arguments.
What am I doing wrong, am I trying to output this method incorrect?
Any help, insight or suggestions would be extremely helpful.
I am new to c# as I'm sure you can tell. What is the proper process to output a method in this context?
I am getting an error saying "no overload for method 'getDeposit' takes 0 arguments". What am I doing wrong
Exactly what it says. Here's your method call:
Console.WriteLine(" output {0}", account.getDeposit());
... and here's the method declaration:
public virtual double getDeposit (double amount)
Note how the method declares a parameter - but you're not providing an argument. Either you need to get rid of the parameter, or you need to add an argument to the method call. Or you need to change to using a different method - one which doesn't change the balance of the account. (It seems unlikely that you want to do that in this case.) Perhaps you should add a Balance property:
// Please note that this should probably be decimal - see below
public double Balance { get { return balance; } }
Then call it with:
Console.WriteLine(" output {0}", account.Balance);
Additionally:
For financial quantities, it's generally better to use decimal than double. Read my articles on decimal floating point and binary floating point for more information.
Your getDeposit method doesn't follow .NET naming conventions, where (at least public) methods are named in PascalCase, with a leading capital letter
Your getDeposit method is oddly named as it isn't "getting" a deposit - it's making a deposit (and returning the balance)
Your getDeposit method always returns the value passed into it, unless it's negative. That seems odd to me - if it's going to return anything, shouldn't it return the balance?
Your getDeposit method silently ignores negative deposits. I'd expect this to throw an error, as trying to make a negative deposit indicates a programming error IMO.
Your getDeposit method takes one argument that you are not passing to it. Depends what you want to achieve either pass a value to method:
static void DumpContents(Account account)
{
double deposit = 1000;
Console.WriteLine(" output {0}", account.getDeposit(deposit));
}
or remove this argumentparameter from the method signature.
//You have to pass a double value into the method, because there is only one method
//and wants a double paramater:
//this is what you created:
public double getDeposit(double amount) // <-
{
double transactionAmt = 0.00;
if (amount > 0)
{
balance += amount;
transactionAmt = amount;
}
return transactionAmt;
}
//This how you should call it:
static void DumpContents(Account account)
{
Console.WriteLine(" output {0}", account.getDeposit(34.90)); //<-
}
I have 3 methods.
1 method holding the value of 3000
1 method holding the value of 0.13
and I have created another method that I want to multiply these two figures together.
public override int FishPerTank()
{
return 3000;
}
public override double WeightOut(double weightOut, double weightIn)
{
return 0.13;
}
public override int HowMuchTheFishWeighOutKG()
{
return (FishPerTank() * WeightOut());
}
I am receiving the syntax error on the WeightOut here:
public override int HowMuchTheFishWeighOutKG()
{
return (FishPerTank() * WeightOut());
}
WeightOut expect 2 parameters and you're not providing them
WeightOut(double weightOut, double weightIn) is declared with two parameters and you're calling it with none. Hence the error.
WeightOut()
expects two parameters. But why ? You don't use them.
Rewrite your method without the 2 parameters
public double WeightOut()
{
return 0.13;
}
You probably want to change
public override double WeightOut(double weightOut, double weightIn)
{
return 0.13;
}
to
public override double WeightOut()
{
return 0.13;
}
as you aren't using the parameters.
also why the override? May need to remove that if removing the parameters causes another syntax error, or fix it in the base class as well.
i created an extension method for decimal AsCurrency(this decimal amount)
when i build i will get an error
The call is ambiguous between the following methods or properties:
'CurrencyHelper.AsCurrency(decimal)' and
'CurrencyHelper.AsCurrency(decimal)'
whate hell? if i clear solution, it will build again. next time it will fail again.
Is the problem with decimal or has there gone something wrong with my solution?
Has anyone encountered same problem?
Edit
Extensions are in one assembly. CurrencyHelper has only one definition.
here's the code:
public static class CurrencyHelper
{
public static string AsCurrency(this decimal amount)
{
return ((decimal?) nr).AsCurrency();
}
public static string AsCurrency(this decimal? amount)
{
var cultureInfo = ...
return (nr ?? 0).ToString("c2", cultureInfo);
}
}
The idea is to format decimal value with needed cultureinfo and if amount is null, 0 is still provided
Ok. problem solved
seems like someone in our team added reference to project itself. Thanks for the tip, Hans