Creating overload methods [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am trying to create an overload method in visual studio with the method name getPrice(), here is the first overload method I tried to create:
private double getPrice(double price)
{
int intQty = 1;
txtQty.Text = intQty.ToString();
double dblSalesTax = 0;
lblSalesTax.Text = dblSalesTax.ToString();
double dblPrice = double.Parse(txtPrice.Text);
txtPrice.Text = dblPrice.ToString("c");
}
However my naming of it is off or something it keeps giving me an error, not all code paths return a double.. so I'm not sure how to fix that and this first overload method is supposed to only take a single parameter called price and then it is supposed to default Qty to 1 and sales tax to 0, besides the error did I do any of that other stuff correct or is the whole thing wrong or how would I fix that? Once I get this first parameter set I think I can get the other 2 working.
EDIT
Ok I changed it a bit...
private void btnCalculate_Click(object sender, EventArgs e)
{
getPrice(double price);
}
private double getPrice(double price)
{
double dblQty = 1;
double dblSalesTax = 0;
double dblPrice = double.Parse(txtPrice.Text);
double dblTotal = (dblPrice * dblQty) *dblSalesTax;
lblTotal.Text = dblTotal.ToString("c");
return dblTotal;
//lblSalesTax.Text = dblSalesTax.ToString();
//double dblPrice = double.Parse(txtPrice.Text);
//txtPrice.Text = dblPrice.ToString("c");
}
There is what I have now, how can I use the parameter price with it and why does it error when i try to put it in the btnCalculate_Click method?

You dont require parameter. You are not using the passed value inside function. You can return double value as given below:
`
private double getPrice()
{
int intQty = 1;
txtQty.Text = intQty.ToString();
double dblSalesTax = 0;
lblSalesTax.Text = dblSalesTax.ToString();
double dblPrice = double.Parse(txtPrice.Text);
txtPrice.Text = dblPrice.ToString("c");
return Convert.ToDouble(txtPrice.Text);
}
`

Related

Operator '==' cannot be applied to operands of type error [duplicate]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
private void button1_Click(object sender, EventArgs e)
{
Random random = new Random();
int getal1 = random.Next(0, 100);
Random random2 = new Random();
int getal2 = random2.Next(0, 100);
int Antwoord = getal1 + getal2;
if (Antwoord == textBox1.Text);
...
}
it says
Operator '==' cannot be applied to operands of type 'string' and 'int'
can someone help me?
if (Antwoord.ToString() == textBox1.Text);
Write it like this. You want to check int to string, this can't happen. You should convert the int value to string or the string value to int. I advice you to convert int to string in other case you can have an exception.
If you need an integer value, entered in the TextBox, you should try to parse its Text:
int textBox1Value;
if (int.TryParse(textBox1.Text, out textBox1Value))
{
// Here the text was successfully parsed to the textBox1Value variable
if (Antwoord == textBox1Value)
{
... // do your stuff
}
}

Convert a string to Double in C# [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I just wrote a method to convert a string to double. It does what it's suppose to do, but it seems too long and I'm thinking there's a better way of writing it. Please review and suggest a better way or point out why this is not good for production code.
static double ConvertStringToDouble(string input, int rounding)
{
string[] split = input.Split('.');
double wholeNumber = 0.0;
if (split.Length > 0 && Int32.TryParse(split[0], out int temp))
wholeNumber = (double)temp;
double decimalNumber = 0.0;
if (split.Length > 1)
{
string decimalString = (split[1].Length < rounding) ? split[1] : split[1].Substring(0, rounding);
if (Int32.TryParse(decimalString, out int dec))
decimalNumber = (double)dec / Math.Pow(10, decimalString.Length);
}
return wholeNumber + decimalNumber;
}
This is the updated method now. Thanks all for the contributions
static double ConvertStringToDouble(string input, int rounding)
{
if (double.TryParse(input, out double value))
return Math.Round(value, rounding);
else return 0.0;
}
.Net has built in functionality for this, its called Double.TryParse. Double.Parse also exists, but its recommended to use the Try variant, as it won't throw exceptions if the number is not parseable into a double. You can use the method like this
string stringToParse = "1.7346"
if (Double.TryParse(stringToParse, out double parsedDouble))
{
//do something with the double here
}
else
{
//failed to parse, error logic here
}
You can just use double.Parse and double.TryParse methods, I prefer to use them like this:
string myString = "1.05";
// This throws exception:
double myParsedDouble = double.Parse(myString);
// This gives you more control over the conversion:
double? myResult = null;
if (double.TryParse(myString, out double _myResult))
myResult = _myResult;
if (myResult == null)
{
throw new ArgumentException("Not a valid double!");
}

Object not getting assigned. [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm working on an asp.net MVC project and have the following code in my view model
public Division ToDivision()
{
Division d = new Division();
Name = this.Name;
Active = this.Active;
return d;
}
Then, in my controller, I have the following method:
public ActionResult Create(DivisionViewModel divisionViewModel)
{
if (ModelState.IsValid)
{
Division division;
division = divisionViewModel.ToDivision();
_divisionService.Create(division);
return RedirectToAction("Index");
}
return View(divisionViewModel);
}
Division is not getting assigned. Why is this? I have a feeling it's something very simple that I'm just not seeing
In your function ToDivision, Name = this.Name doesn't do anything because they are the same value. You need to do d.Name = this.Name. Similarly for Active. Try:
public Division ToDivision()
{
Division d = new Division();
d.Name = this.Name;
d.Active = this.Active;
return d;
}
From your code example seems that you want to use object initialization in a method with property member assignment, but assigned to the Name member instead of d.Name. Try using the following initializer:
public Division ToDivision()
{
Division d = new Division()
{
Name = this.Name;
Active = this.Active;
}
return d;
}

'Program.CalculateTax(double)':not all code paths return a value' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I' ve got error on this code. Its said "Not all code paths return Value"
Please hel me fix this. Thanks
public static double subtotal;
public static void Main(String[] args)
{
subtotal = 15.00;
Console.WriteLine($"Subtotal : {subtotal}");
double total = subtotal + CalculateTax(7);
Console.WriteLine($"Total : {total}");
}
public static double CalculateTax(double taxRate)
{
double tax = subtotal * taxRate;
Console.WriteLine($"Tax: {tax}");
}
try this
public static double CalculateTax(double taxRate)
{
double tax = subtotal * taxRate;
Console.WriteLine($"Tax: {tax}");
return tax;
}

Cannot convert lambda expression to type bool [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Cannot convert lambda expression to type bool because it is not a delegate type.
protected void button1_Click(object sender, EventArgs e)
{
int totalpoints;
Int32 realpoints;
lelpoints = 0;
totalpoints = Convert.ToInt32(dPts.Text);
totalpoints = totalpoints + 1;
totalpoints = realpoints;
dPts.Text = totalpoints.ToString();
}
protected void buyBG_Click(object sender, EventArgs e)
{
string[] rbg = new string[] { "red", "green", "blue" };
Random random = new Random();
int randomNumber = random.Next(0, 3);
string currentbg = rbg[randomNumber];
if (realpoints => 10 ){}
This is the issue I'm experiencing with the code I've provided above. The issue is showed at the If statement at the bottom.
Edit: Changing => to >= definitely resolved that issue but now it reports an error "the name realpoints does not exist in the current context". Thank you
Not =>, but >=.
This should resolve your problem.
=> is a lambda operator is not the same as the greater than or equal to operator >=
you can read => (lambda sign) as "goes to". Means you are passing a parameter to a delegate body. In your case realpoints => 10 mean realpoints is parameter and 10 is a body and this body is not bool type (10 is not bool).
For example you have a list of integer like:
List intList = new List {10, 14,5,17};
you want to find the first integer in the list which is greater than 10 then you can use lambda expression something like
int numberFound = intList.Find(x => x > 10 );
Thanks,
Sukh

Categories