Rewriting my code as a Method, using C# Visual Studio












0















I am currently having trouble understanding Methods and how they work in C#. I currently have code written for a car cost calculator program I created, I want to rearrange or break my code down using methods. I am unsure how or where to begin doing so as it pertains to my program. Here is my code, clarification would be helpful! Thank you!



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

//constants for the Zone entered by user
const decimal ZoneCostN = 27;
const decimal ZoneCostS = 36;
const decimal ZoneCostE = 45;
const decimal ZoneCostW = 54;

private void CalcButton_Click(object sender, EventArgs e)
{
//set the variables
decimal PackWeight = 0;
decimal CostZone = 0;
decimal CostWeight = 0;
decimal ShippingTot = 0;
decimal Net = 0;
const decimal PerPound = 18;

//parses the entry into the textboxes
decimal.TryParse(WeightText.Text, out PackWeight); ;

//algorithm for variables
CostWeight = PackWeight * PerPound;
Zonelbl.Text = "";
CostZone = 0;

//if else statement to get the zone cost
{
if (NorthButton.Checked)
{
CostZone = ZoneCostN;
}
else if (SouthButton.Checked)
{
CostZone = ZoneCostS;
}
else if (EastButton.Checked)
{
CostZone = ZoneCostE;
}
else if (WestButton.Checked)
{
CostZone = ZoneCostW;
}
else
{
MessageBox.Show("Select a zone!");
}
}
//algorithm to get total and net
ShippingTot = CostZone + CostWeight;
Net = ShippingTot / CostWeight;

//if condition for CAPPED label
if (ShippingTot >= 100)
{
CAPPEDlbl.Visible = true;
}
else
{
CAPPEDlbl.Visible = false;
}

//output for all the data
Zonelbl.Text = CostZone.ToString("c");
Weightlbl.Text = CostWeight.ToString("c");
Totallbl.Text = ShippingTot.ToString("c");
Netlbl.Text = Net.ToString("c");
}

private void ClearButton_Click(object sender, EventArgs e)
{
//clears the form
Zonelbl.Text = "";
Weightlbl.Text = "";
Totallbl.Text = "";
Netlbl.Text = "";
WeightText.Text = "";
CAPPEDlbl.Visible = false;
WeightText.Focus();
}
}









share|improve this question

























  • The Click event handlers are both examples of methods.

    – Handbag Crab
    Nov 14 '18 at 22:46











  • Reading this should be helpful: docs.microsoft.com/en-us/dotnet/csharp/methods

    – elgonzo
    Nov 14 '18 at 22:50











  • @Handbag Crab Right, but that was created for me as an event when I created the "calculate" button. I guess for example. How would I put the first if/else statements into it's own method?

    – Idealprinciple
    Nov 14 '18 at 22:50













  • @elgonzo I have read through that, I guess maybe I'm not understanding how I could break down my code using a method. The concept seems straight forward, but I can't see how it would apply here.

    – Idealprinciple
    Nov 14 '18 at 22:56






  • 1





    If you can't see how you can break down your code in different methods then don't force yourself. I don't know and i might misunderstand what you want, but it feels like you insist on refactoring your code into multiple methods while lacking a clear motivation/reason (the "why") for doing so...

    – elgonzo
    Nov 14 '18 at 23:00


















0















I am currently having trouble understanding Methods and how they work in C#. I currently have code written for a car cost calculator program I created, I want to rearrange or break my code down using methods. I am unsure how or where to begin doing so as it pertains to my program. Here is my code, clarification would be helpful! Thank you!



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

//constants for the Zone entered by user
const decimal ZoneCostN = 27;
const decimal ZoneCostS = 36;
const decimal ZoneCostE = 45;
const decimal ZoneCostW = 54;

private void CalcButton_Click(object sender, EventArgs e)
{
//set the variables
decimal PackWeight = 0;
decimal CostZone = 0;
decimal CostWeight = 0;
decimal ShippingTot = 0;
decimal Net = 0;
const decimal PerPound = 18;

//parses the entry into the textboxes
decimal.TryParse(WeightText.Text, out PackWeight); ;

//algorithm for variables
CostWeight = PackWeight * PerPound;
Zonelbl.Text = "";
CostZone = 0;

//if else statement to get the zone cost
{
if (NorthButton.Checked)
{
CostZone = ZoneCostN;
}
else if (SouthButton.Checked)
{
CostZone = ZoneCostS;
}
else if (EastButton.Checked)
{
CostZone = ZoneCostE;
}
else if (WestButton.Checked)
{
CostZone = ZoneCostW;
}
else
{
MessageBox.Show("Select a zone!");
}
}
//algorithm to get total and net
ShippingTot = CostZone + CostWeight;
Net = ShippingTot / CostWeight;

//if condition for CAPPED label
if (ShippingTot >= 100)
{
CAPPEDlbl.Visible = true;
}
else
{
CAPPEDlbl.Visible = false;
}

//output for all the data
Zonelbl.Text = CostZone.ToString("c");
Weightlbl.Text = CostWeight.ToString("c");
Totallbl.Text = ShippingTot.ToString("c");
Netlbl.Text = Net.ToString("c");
}

private void ClearButton_Click(object sender, EventArgs e)
{
//clears the form
Zonelbl.Text = "";
Weightlbl.Text = "";
Totallbl.Text = "";
Netlbl.Text = "";
WeightText.Text = "";
CAPPEDlbl.Visible = false;
WeightText.Focus();
}
}









share|improve this question

























  • The Click event handlers are both examples of methods.

    – Handbag Crab
    Nov 14 '18 at 22:46











  • Reading this should be helpful: docs.microsoft.com/en-us/dotnet/csharp/methods

    – elgonzo
    Nov 14 '18 at 22:50











  • @Handbag Crab Right, but that was created for me as an event when I created the "calculate" button. I guess for example. How would I put the first if/else statements into it's own method?

    – Idealprinciple
    Nov 14 '18 at 22:50













  • @elgonzo I have read through that, I guess maybe I'm not understanding how I could break down my code using a method. The concept seems straight forward, but I can't see how it would apply here.

    – Idealprinciple
    Nov 14 '18 at 22:56






  • 1





    If you can't see how you can break down your code in different methods then don't force yourself. I don't know and i might misunderstand what you want, but it feels like you insist on refactoring your code into multiple methods while lacking a clear motivation/reason (the "why") for doing so...

    – elgonzo
    Nov 14 '18 at 23:00
















0












0








0








I am currently having trouble understanding Methods and how they work in C#. I currently have code written for a car cost calculator program I created, I want to rearrange or break my code down using methods. I am unsure how or where to begin doing so as it pertains to my program. Here is my code, clarification would be helpful! Thank you!



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

//constants for the Zone entered by user
const decimal ZoneCostN = 27;
const decimal ZoneCostS = 36;
const decimal ZoneCostE = 45;
const decimal ZoneCostW = 54;

private void CalcButton_Click(object sender, EventArgs e)
{
//set the variables
decimal PackWeight = 0;
decimal CostZone = 0;
decimal CostWeight = 0;
decimal ShippingTot = 0;
decimal Net = 0;
const decimal PerPound = 18;

//parses the entry into the textboxes
decimal.TryParse(WeightText.Text, out PackWeight); ;

//algorithm for variables
CostWeight = PackWeight * PerPound;
Zonelbl.Text = "";
CostZone = 0;

//if else statement to get the zone cost
{
if (NorthButton.Checked)
{
CostZone = ZoneCostN;
}
else if (SouthButton.Checked)
{
CostZone = ZoneCostS;
}
else if (EastButton.Checked)
{
CostZone = ZoneCostE;
}
else if (WestButton.Checked)
{
CostZone = ZoneCostW;
}
else
{
MessageBox.Show("Select a zone!");
}
}
//algorithm to get total and net
ShippingTot = CostZone + CostWeight;
Net = ShippingTot / CostWeight;

//if condition for CAPPED label
if (ShippingTot >= 100)
{
CAPPEDlbl.Visible = true;
}
else
{
CAPPEDlbl.Visible = false;
}

//output for all the data
Zonelbl.Text = CostZone.ToString("c");
Weightlbl.Text = CostWeight.ToString("c");
Totallbl.Text = ShippingTot.ToString("c");
Netlbl.Text = Net.ToString("c");
}

private void ClearButton_Click(object sender, EventArgs e)
{
//clears the form
Zonelbl.Text = "";
Weightlbl.Text = "";
Totallbl.Text = "";
Netlbl.Text = "";
WeightText.Text = "";
CAPPEDlbl.Visible = false;
WeightText.Focus();
}
}









share|improve this question
















I am currently having trouble understanding Methods and how they work in C#. I currently have code written for a car cost calculator program I created, I want to rearrange or break my code down using methods. I am unsure how or where to begin doing so as it pertains to my program. Here is my code, clarification would be helpful! Thank you!



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

//constants for the Zone entered by user
const decimal ZoneCostN = 27;
const decimal ZoneCostS = 36;
const decimal ZoneCostE = 45;
const decimal ZoneCostW = 54;

private void CalcButton_Click(object sender, EventArgs e)
{
//set the variables
decimal PackWeight = 0;
decimal CostZone = 0;
decimal CostWeight = 0;
decimal ShippingTot = 0;
decimal Net = 0;
const decimal PerPound = 18;

//parses the entry into the textboxes
decimal.TryParse(WeightText.Text, out PackWeight); ;

//algorithm for variables
CostWeight = PackWeight * PerPound;
Zonelbl.Text = "";
CostZone = 0;

//if else statement to get the zone cost
{
if (NorthButton.Checked)
{
CostZone = ZoneCostN;
}
else if (SouthButton.Checked)
{
CostZone = ZoneCostS;
}
else if (EastButton.Checked)
{
CostZone = ZoneCostE;
}
else if (WestButton.Checked)
{
CostZone = ZoneCostW;
}
else
{
MessageBox.Show("Select a zone!");
}
}
//algorithm to get total and net
ShippingTot = CostZone + CostWeight;
Net = ShippingTot / CostWeight;

//if condition for CAPPED label
if (ShippingTot >= 100)
{
CAPPEDlbl.Visible = true;
}
else
{
CAPPEDlbl.Visible = false;
}

//output for all the data
Zonelbl.Text = CostZone.ToString("c");
Weightlbl.Text = CostWeight.ToString("c");
Totallbl.Text = ShippingTot.ToString("c");
Netlbl.Text = Net.ToString("c");
}

private void ClearButton_Click(object sender, EventArgs e)
{
//clears the form
Zonelbl.Text = "";
Weightlbl.Text = "";
Totallbl.Text = "";
Netlbl.Text = "";
WeightText.Text = "";
CAPPEDlbl.Visible = false;
WeightText.Focus();
}
}






c# visual-studio winforms methods parameter-passing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 22:46









Rufus L

18.6k31631




18.6k31631










asked Nov 14 '18 at 22:41









IdealprincipleIdealprinciple

165




165













  • The Click event handlers are both examples of methods.

    – Handbag Crab
    Nov 14 '18 at 22:46











  • Reading this should be helpful: docs.microsoft.com/en-us/dotnet/csharp/methods

    – elgonzo
    Nov 14 '18 at 22:50











  • @Handbag Crab Right, but that was created for me as an event when I created the "calculate" button. I guess for example. How would I put the first if/else statements into it's own method?

    – Idealprinciple
    Nov 14 '18 at 22:50













  • @elgonzo I have read through that, I guess maybe I'm not understanding how I could break down my code using a method. The concept seems straight forward, but I can't see how it would apply here.

    – Idealprinciple
    Nov 14 '18 at 22:56






  • 1





    If you can't see how you can break down your code in different methods then don't force yourself. I don't know and i might misunderstand what you want, but it feels like you insist on refactoring your code into multiple methods while lacking a clear motivation/reason (the "why") for doing so...

    – elgonzo
    Nov 14 '18 at 23:00





















  • The Click event handlers are both examples of methods.

    – Handbag Crab
    Nov 14 '18 at 22:46











  • Reading this should be helpful: docs.microsoft.com/en-us/dotnet/csharp/methods

    – elgonzo
    Nov 14 '18 at 22:50











  • @Handbag Crab Right, but that was created for me as an event when I created the "calculate" button. I guess for example. How would I put the first if/else statements into it's own method?

    – Idealprinciple
    Nov 14 '18 at 22:50













  • @elgonzo I have read through that, I guess maybe I'm not understanding how I could break down my code using a method. The concept seems straight forward, but I can't see how it would apply here.

    – Idealprinciple
    Nov 14 '18 at 22:56






  • 1





    If you can't see how you can break down your code in different methods then don't force yourself. I don't know and i might misunderstand what you want, but it feels like you insist on refactoring your code into multiple methods while lacking a clear motivation/reason (the "why") for doing so...

    – elgonzo
    Nov 14 '18 at 23:00



















The Click event handlers are both examples of methods.

– Handbag Crab
Nov 14 '18 at 22:46





The Click event handlers are both examples of methods.

– Handbag Crab
Nov 14 '18 at 22:46













Reading this should be helpful: docs.microsoft.com/en-us/dotnet/csharp/methods

– elgonzo
Nov 14 '18 at 22:50





Reading this should be helpful: docs.microsoft.com/en-us/dotnet/csharp/methods

– elgonzo
Nov 14 '18 at 22:50













@Handbag Crab Right, but that was created for me as an event when I created the "calculate" button. I guess for example. How would I put the first if/else statements into it's own method?

– Idealprinciple
Nov 14 '18 at 22:50







@Handbag Crab Right, but that was created for me as an event when I created the "calculate" button. I guess for example. How would I put the first if/else statements into it's own method?

– Idealprinciple
Nov 14 '18 at 22:50















@elgonzo I have read through that, I guess maybe I'm not understanding how I could break down my code using a method. The concept seems straight forward, but I can't see how it would apply here.

– Idealprinciple
Nov 14 '18 at 22:56





@elgonzo I have read through that, I guess maybe I'm not understanding how I could break down my code using a method. The concept seems straight forward, but I can't see how it would apply here.

– Idealprinciple
Nov 14 '18 at 22:56




1




1





If you can't see how you can break down your code in different methods then don't force yourself. I don't know and i might misunderstand what you want, but it feels like you insist on refactoring your code into multiple methods while lacking a clear motivation/reason (the "why") for doing so...

– elgonzo
Nov 14 '18 at 23:00







If you can't see how you can break down your code in different methods then don't force yourself. I don't know and i might misunderstand what you want, but it feels like you insist on refactoring your code into multiple methods while lacking a clear motivation/reason (the "why") for doing so...

– elgonzo
Nov 14 '18 at 23:00














1 Answer
1






active

oldest

votes


















0














Usually, we create methods when we need to reuse a code. In your case, you should see which part of your code will be reused in the future. If it is a simple form you may don't need to change anything but imagine you want to use your clear functionality somewhere else, create a method and call it everywhere you need



        void clear()
{
Zonelbl.Text = "";
Weightlbl.Text = "";
Totallbl.Text = "";
Netlbl.Text = "";
WeightText.Text = "";
CAPPEDlbl.Visible = false;
WeightText.Focus();
}


private void ClearButton_Click(object sender, EventArgs e)
{
clear();
}


Now you can reuse clear() and in case you needed to change it you only need to change the method. It's the concept and you can apply it wherever you need.






share|improve this answer























    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%2f53309824%2frewriting-my-code-as-a-method-using-c-sharp-visual-studio%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Usually, we create methods when we need to reuse a code. In your case, you should see which part of your code will be reused in the future. If it is a simple form you may don't need to change anything but imagine you want to use your clear functionality somewhere else, create a method and call it everywhere you need



            void clear()
    {
    Zonelbl.Text = "";
    Weightlbl.Text = "";
    Totallbl.Text = "";
    Netlbl.Text = "";
    WeightText.Text = "";
    CAPPEDlbl.Visible = false;
    WeightText.Focus();
    }


    private void ClearButton_Click(object sender, EventArgs e)
    {
    clear();
    }


    Now you can reuse clear() and in case you needed to change it you only need to change the method. It's the concept and you can apply it wherever you need.






    share|improve this answer




























      0














      Usually, we create methods when we need to reuse a code. In your case, you should see which part of your code will be reused in the future. If it is a simple form you may don't need to change anything but imagine you want to use your clear functionality somewhere else, create a method and call it everywhere you need



              void clear()
      {
      Zonelbl.Text = "";
      Weightlbl.Text = "";
      Totallbl.Text = "";
      Netlbl.Text = "";
      WeightText.Text = "";
      CAPPEDlbl.Visible = false;
      WeightText.Focus();
      }


      private void ClearButton_Click(object sender, EventArgs e)
      {
      clear();
      }


      Now you can reuse clear() and in case you needed to change it you only need to change the method. It's the concept and you can apply it wherever you need.






      share|improve this answer


























        0












        0








        0







        Usually, we create methods when we need to reuse a code. In your case, you should see which part of your code will be reused in the future. If it is a simple form you may don't need to change anything but imagine you want to use your clear functionality somewhere else, create a method and call it everywhere you need



                void clear()
        {
        Zonelbl.Text = "";
        Weightlbl.Text = "";
        Totallbl.Text = "";
        Netlbl.Text = "";
        WeightText.Text = "";
        CAPPEDlbl.Visible = false;
        WeightText.Focus();
        }


        private void ClearButton_Click(object sender, EventArgs e)
        {
        clear();
        }


        Now you can reuse clear() and in case you needed to change it you only need to change the method. It's the concept and you can apply it wherever you need.






        share|improve this answer













        Usually, we create methods when we need to reuse a code. In your case, you should see which part of your code will be reused in the future. If it is a simple form you may don't need to change anything but imagine you want to use your clear functionality somewhere else, create a method and call it everywhere you need



                void clear()
        {
        Zonelbl.Text = "";
        Weightlbl.Text = "";
        Totallbl.Text = "";
        Netlbl.Text = "";
        WeightText.Text = "";
        CAPPEDlbl.Visible = false;
        WeightText.Focus();
        }


        private void ClearButton_Click(object sender, EventArgs e)
        {
        clear();
        }


        Now you can reuse clear() and in case you needed to change it you only need to change the method. It's the concept and you can apply it wherever you need.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 23:33









        HamidHamid

        539




        539
































            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%2f53309824%2frewriting-my-code-as-a-method-using-c-sharp-visual-studio%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

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma