CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2011
    Posts
    4

    Help needed creating methods

    I have a question, I have a program created where I basically have a few text boxes and a button that, when clicked, calculates the averages from the test scores from the inputs in the text boxes.

    My professor now wants us to create methods within our program. We are supposed to use a format of
    public string variable
    double (variable)

    One of the things I need help creating a method for is this if structure. A method for getting the letterGrade.

    if (courseAverage >= 90)
    {
    letterGrade = "A";
    }
    else if (courseAverage > 80)
    {
    letterGrade = "B";
    }
    else if (courseAverage > 70)
    {
    letterGrade = "C";
    }
    else if (courseAverage > 60)
    {
    letterGrade = "D";
    }
    else
    {
    letterGrade = "F";
    }

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Help needed creating methods

    So a method (a/k/a subroutine, function) is just a procedure that takes some inputs and returns an output (just like any function you have encountered in math, except it doesn't always have to use numbers as inputs and outputs, though it can). Since it's a homework, I won't answer using your case, but will show you an example on something else instead:

    Suppose I have a textbox (txtInput) and and a button (btnCalculate) and I want to show a Message Box that displays ('Hooray') if and only if the textbox has the string 'Victory!' in it (otherwise 'Drat!'). One method of doing this is what you did previously (I suspect):

    Code:
    public void btnCalculate_Click()
    {
        if( txtInput.Text = "Victory!" )
            MessageBox.Show("Hooray!");
        else
            MessageBox.Show("Drat!");
    }
    Another way to do this would be to do this:

    Code:
    public void btnCalculate_Click()
    {
        string textToDisplay = getMessageText(textInput.Text);
        MessageBox.Show(textToDisplay);
    }
    
    public string getMessageText(string input)
    {
        if( input == "Victory!" )
            return "Hooray";
        else
            return "Drat!";
    }
    So here we have two methods. The btnCalculate_Click method orders the getMessageText method to give it a string based on the value of textInput.Text which it passes to the method. The return keyword in getMessageText causes the method to (a) immediate halt it's calculations and (b) give the returned value back to the method that called this subroutine.

    If we wanted to get a number from this method, instead of a string, we could change it so that it would look like:

    Code:
    public int getMessageText(string input)
    {
        if( input == "Victory!" )
            return 1;
        else
            return 0;
    }
    See how the line that declares the method (called the function signature) now contains int which indicates that this method returns an integer now. Accordingly, the return values were changed in the body of method so that it's returning integers instead of strings.

    Does that make sense? This is a very basic aspect of the language, so it is consequently kind of hard to explain. Please ask further questions if you are still confused.

    P.S. if you use the [code] and [/code] tags to wrap your code, it will be formatted correctly.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured