Code Issue for defining correct methods and formulas in C# in winforms












0















I am currently working with C# in winform application for my school assignment and I am having difficulty seeing and fixing my errors within my code.
I am creating a windows form application which makes these calculations and has these methods as well:




  • FatCalories– This method accepts the input number from user and then returns the number of calories from that amount of fat with the given formula:
    Calories from fat = Fat grams × 9

  • CarbCalories–This method should accept a number of carbohydrate grams as an argument and return the number of calories from that amount of carbohydrates.
    Calories from carbs = Carbs grams × 4


Here is my [updated] code below:



namespace Exercise_8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void fatConversionButton_Click(object sender, EventArgs e)
{
try
{
//Variables here
float calories;
float fatCalories;

fatCalories = float.Parse(textBox1.Text);

calories = fatCalories * 9;
MessageBox.Show("Your total calories from the converted input of your fatCalories is" + calories);
}
catch
{
MessageBox.Show("You have entered an invalid input");
}
}

private void calorieConversionButton_Click(object sender, EventArgs e)
{
float calories;
float carbCalories;

carbCalories = float.Parse(textBox2.Text);

calories = carbCalories * 4;
MessageBox.Show("Your total calories from the converted input of your CalorieCalories is" + calories);
}

private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}









share|improve this question

























  • Well, looks like you are assigning a text value to a float.

    – sjb-sjb
    Nov 26 '18 at 4:02











  • What does the code do? What do you expect it to do?

    – mjwills
    Nov 26 '18 at 4:04











  • as @sjb-sjb said before, you are trying to assign a string to float variable. try using carbCalories =float.Parse(textBox2.Text());

    – Ehsan.Saradar
    Nov 26 '18 at 4:08






  • 1





    Text is property. Please remove ()

    – Nakul
    Nov 26 '18 at 4:18






  • 1





    Note that float.TryParse exists for a reason (it doesn't throw exceptions for bad input, it instead indicates parsing success via a boolean).

    – John
    Nov 26 '18 at 4:32


















0















I am currently working with C# in winform application for my school assignment and I am having difficulty seeing and fixing my errors within my code.
I am creating a windows form application which makes these calculations and has these methods as well:




  • FatCalories– This method accepts the input number from user and then returns the number of calories from that amount of fat with the given formula:
    Calories from fat = Fat grams × 9

  • CarbCalories–This method should accept a number of carbohydrate grams as an argument and return the number of calories from that amount of carbohydrates.
    Calories from carbs = Carbs grams × 4


Here is my [updated] code below:



namespace Exercise_8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void fatConversionButton_Click(object sender, EventArgs e)
{
try
{
//Variables here
float calories;
float fatCalories;

fatCalories = float.Parse(textBox1.Text);

calories = fatCalories * 9;
MessageBox.Show("Your total calories from the converted input of your fatCalories is" + calories);
}
catch
{
MessageBox.Show("You have entered an invalid input");
}
}

private void calorieConversionButton_Click(object sender, EventArgs e)
{
float calories;
float carbCalories;

carbCalories = float.Parse(textBox2.Text);

calories = carbCalories * 4;
MessageBox.Show("Your total calories from the converted input of your CalorieCalories is" + calories);
}

private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}









share|improve this question

























  • Well, looks like you are assigning a text value to a float.

    – sjb-sjb
    Nov 26 '18 at 4:02











  • What does the code do? What do you expect it to do?

    – mjwills
    Nov 26 '18 at 4:04











  • as @sjb-sjb said before, you are trying to assign a string to float variable. try using carbCalories =float.Parse(textBox2.Text());

    – Ehsan.Saradar
    Nov 26 '18 at 4:08






  • 1





    Text is property. Please remove ()

    – Nakul
    Nov 26 '18 at 4:18






  • 1





    Note that float.TryParse exists for a reason (it doesn't throw exceptions for bad input, it instead indicates parsing success via a boolean).

    – John
    Nov 26 '18 at 4:32
















0












0








0








I am currently working with C# in winform application for my school assignment and I am having difficulty seeing and fixing my errors within my code.
I am creating a windows form application which makes these calculations and has these methods as well:




  • FatCalories– This method accepts the input number from user and then returns the number of calories from that amount of fat with the given formula:
    Calories from fat = Fat grams × 9

  • CarbCalories–This method should accept a number of carbohydrate grams as an argument and return the number of calories from that amount of carbohydrates.
    Calories from carbs = Carbs grams × 4


Here is my [updated] code below:



namespace Exercise_8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void fatConversionButton_Click(object sender, EventArgs e)
{
try
{
//Variables here
float calories;
float fatCalories;

fatCalories = float.Parse(textBox1.Text);

calories = fatCalories * 9;
MessageBox.Show("Your total calories from the converted input of your fatCalories is" + calories);
}
catch
{
MessageBox.Show("You have entered an invalid input");
}
}

private void calorieConversionButton_Click(object sender, EventArgs e)
{
float calories;
float carbCalories;

carbCalories = float.Parse(textBox2.Text);

calories = carbCalories * 4;
MessageBox.Show("Your total calories from the converted input of your CalorieCalories is" + calories);
}

private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}









share|improve this question
















I am currently working with C# in winform application for my school assignment and I am having difficulty seeing and fixing my errors within my code.
I am creating a windows form application which makes these calculations and has these methods as well:




  • FatCalories– This method accepts the input number from user and then returns the number of calories from that amount of fat with the given formula:
    Calories from fat = Fat grams × 9

  • CarbCalories–This method should accept a number of carbohydrate grams as an argument and return the number of calories from that amount of carbohydrates.
    Calories from carbs = Carbs grams × 4


Here is my [updated] code below:



namespace Exercise_8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void fatConversionButton_Click(object sender, EventArgs e)
{
try
{
//Variables here
float calories;
float fatCalories;

fatCalories = float.Parse(textBox1.Text);

calories = fatCalories * 9;
MessageBox.Show("Your total calories from the converted input of your fatCalories is" + calories);
}
catch
{
MessageBox.Show("You have entered an invalid input");
}
}

private void calorieConversionButton_Click(object sender, EventArgs e)
{
float calories;
float carbCalories;

carbCalories = float.Parse(textBox2.Text);

calories = carbCalories * 4;
MessageBox.Show("Your total calories from the converted input of your CalorieCalories is" + calories);
}

private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}






c# winforms methods






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 5:38









Richardissimo

4,4202827




4,4202827










asked Nov 26 '18 at 3:48









Mike JonesMike Jones

73




73













  • Well, looks like you are assigning a text value to a float.

    – sjb-sjb
    Nov 26 '18 at 4:02











  • What does the code do? What do you expect it to do?

    – mjwills
    Nov 26 '18 at 4:04











  • as @sjb-sjb said before, you are trying to assign a string to float variable. try using carbCalories =float.Parse(textBox2.Text());

    – Ehsan.Saradar
    Nov 26 '18 at 4:08






  • 1





    Text is property. Please remove ()

    – Nakul
    Nov 26 '18 at 4:18






  • 1





    Note that float.TryParse exists for a reason (it doesn't throw exceptions for bad input, it instead indicates parsing success via a boolean).

    – John
    Nov 26 '18 at 4:32





















  • Well, looks like you are assigning a text value to a float.

    – sjb-sjb
    Nov 26 '18 at 4:02











  • What does the code do? What do you expect it to do?

    – mjwills
    Nov 26 '18 at 4:04











  • as @sjb-sjb said before, you are trying to assign a string to float variable. try using carbCalories =float.Parse(textBox2.Text());

    – Ehsan.Saradar
    Nov 26 '18 at 4:08






  • 1





    Text is property. Please remove ()

    – Nakul
    Nov 26 '18 at 4:18






  • 1





    Note that float.TryParse exists for a reason (it doesn't throw exceptions for bad input, it instead indicates parsing success via a boolean).

    – John
    Nov 26 '18 at 4:32



















Well, looks like you are assigning a text value to a float.

– sjb-sjb
Nov 26 '18 at 4:02





Well, looks like you are assigning a text value to a float.

– sjb-sjb
Nov 26 '18 at 4:02













What does the code do? What do you expect it to do?

– mjwills
Nov 26 '18 at 4:04





What does the code do? What do you expect it to do?

– mjwills
Nov 26 '18 at 4:04













as @sjb-sjb said before, you are trying to assign a string to float variable. try using carbCalories =float.Parse(textBox2.Text());

– Ehsan.Saradar
Nov 26 '18 at 4:08





as @sjb-sjb said before, you are trying to assign a string to float variable. try using carbCalories =float.Parse(textBox2.Text());

– Ehsan.Saradar
Nov 26 '18 at 4:08




1




1





Text is property. Please remove ()

– Nakul
Nov 26 '18 at 4:18





Text is property. Please remove ()

– Nakul
Nov 26 '18 at 4:18




1




1





Note that float.TryParse exists for a reason (it doesn't throw exceptions for bad input, it instead indicates parsing success via a boolean).

– John
Nov 26 '18 at 4:32







Note that float.TryParse exists for a reason (it doesn't throw exceptions for bad input, it instead indicates parsing success via a boolean).

– John
Nov 26 '18 at 4:32














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474557%2fcode-issue-for-defining-correct-methods-and-formulas-in-c-sharp-in-winforms%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474557%2fcode-issue-for-defining-correct-methods-and-formulas-in-c-sharp-in-winforms%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin