How to use multiple methods in C#?
Or in short, I can't get it.
I have been looking up and studying things in C# for the past couple of days, but there is one things I can't figure out: How I can use multiple methods and call them back to main (). (I really hope I am phrasing this right.) So I am trying to do a simple project to help me with other projects, but even the simple project isn't doing much help.
Basically this: I am making a console program in VB that can take 2 integers from the user in main (), then it has to call the sum, product, difference, and so on from a different method back into main ().
The code I currently have looks like this:
I think I have the top part right, but I keep messing up with the creation of a new method. I can do this all when it's in one method, but for the sake of a class, I have to know how to use and create new methods.
I haven't finished it yet, as I am still having trouble getting the sum of the two to work right.
If anyone can help me out, I would really appreciate any advice or tips I can get on this. I would like to settle down and learn this once and for all.
Re: How to use multiple methods in C#?
Seems to me that before you can learn to write methods, you should learn to write C functions.
Either that, or I don't understand what you're trying to do, and what you mean by "call back".
Re: How to use multiple methods in C#?
Quote:
Originally Posted by
SaptaZapta
Seems to me that before you can learn to write methods, you should learn to write C functions.
Either that, or I don't understand what you're trying to do, and what you mean by "call back".
That's how my teacher phrases it, but I barely understand him so I can't quiet say what it means either. Is there another way of saying it?
The project I am doing is an exercise from my text book and it's phrased like this:
Write a program that allows the user to enter two integers. Write separate methods that will produce the sum, product, average, and squared result of the values. Call the results with both values entered.
Re: How to use multiple methods in C#?
I may be completely wrong because it is C#, but in java, you'd have to pass x and y in as parameters.
Code:
//in main, below taking inputs
Console.WriteLine(add(x,y));
//add method
public static int add(int num1, int num2){
return num1+num2;
}
Re: How to use multiple methods in C#?
Quote:
Originally Posted by
Marksman Bryan
I may be completely wrong because it is C#, but in java, you'd have to pass x and y in as parameters.
Code:
//in main, below taking inputs
Console.WriteLine(add(x,y));
//add method
public static int add(int num1, int num2){
return num1+num2;
}
Alright, I'll look that up and give it a try. Thank you for the response.
Re: How to use multiple methods in C#?
Quote:
Originally Posted by
SaptaZapta
Seems to me that before you can learn to write methods, you should learn to write C functions.
That's what I'd like to know, because the above concepts are so pointlessly unrelated as to seem to be confusing what he's trying to do.
It really sounds like the requirements as simple as:
Code:
void main()
{
int x, y, prod, s, diff;
string inputValue;
Console.WriteLine("Enter the first number: ");
inputValue = Console.ReadLine();
x = int.Parse(inputValue);
Console.WriteLine("Please enter the second number: ");
inputValue = Console.ReadLine();
y = int.Parse(inputValue);
prod = product( x, y);
s = sum( x,y);
diff = difference( x,y);
/*
Do whatever with values.
*/
return;
}
int sum( int x, int y )
{
return x+y;
}
int product( int x, int y )
{
return x*y;
}
int difference( int x, int y )
{
return x-y;
}
And then applying that logic to classes and methods. But generally you'd learn that bit first, not as a secondary concept. :f6:
Re: How to use multiple methods in C#?
Quote:
Originally Posted by
Eos
And then applying that logic to classes and methods. But generally you'd learn that bit first, not as a secondary concept. :f6:
That would probably be because my teacher's FAVORITE saying is "We'll talk about that a bit more later in the semester" and then he never does. :f3:
And when he DOES show us stuff, he always uses visual logic, but he wants the homework done in visual basic. (And all those lovely tips and demos he gives to us are in Visual logic form too. I feel like this SHOULD be helpful but I am not sure HOW.)
Thank you for the help. I'll study this code and hope I can make a passing grade in this class (And then never take this teacher ever again. This class probably would have been a lot less confusing if I had a teacher that knew how to teach. :f7: )
Once again, thank you Eos. Thank you.
Re: How to use multiple methods in C#?
I've seen my fair share of idiot teachers, or even brilliant teachers with idiotic curriculums.
Hopefully you can see how to adapt the baseline I wrote into what I think he actually wanted.
(Equally hopeful I even interpreted that gibberish into something approximate to what he meant).