Senior Member
Posts: 703
Threads: 15
Joined: 2011-07
Gender: Female
IGN: Luminous3417
Server: El Nido
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.
Posting Freak
Posts: 12,000
Threads: 634
Joined: 2009-07
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".
Senior Member
Posts: 703
Threads: 15
Joined: 2011-07
Gender: Female
IGN: Luminous3417
Server: El Nido
SaptaZapta Wrote: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.
Posting Freak
Posts: 5,744
Threads: 96
Joined: 2009-01
Gender: Male
Sexual Orientation: Straight
Country Flag: New_York
IGN: iCrossBolt
Server: MS2
Job: Ranger
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;
}
Senior Member
Posts: 703
Threads: 15
Joined: 2011-07
Gender: Female
IGN: Luminous3417
Server: El Nido
Marksman Bryan Wrote: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.
Administrator
Posts: 17,190
Threads: 2,153
Joined: 2008-09
Gender: Male
Sexual Orientation: Gay
IGN: Aesilyn
Server: Mardia
Level: 200
Job: I/L ArchMage
Guild: Animus
SaptaZapta Wrote: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:
It's not having what you want - It's wanting what you've got.
Senior Member
Posts: 703
Threads: 15
Joined: 2011-07
Gender: Female
IGN: Luminous3417
Server: El Nido
Eos Wrote: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.
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.  )
Once again, thank you Eos. Thank you.
Administrator
Posts: 17,190
Threads: 2,153
Joined: 2008-09
Gender: Male
Sexual Orientation: Gay
IGN: Aesilyn
Server: Mardia
Level: 200
Job: I/L ArchMage
Guild: Animus
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).
It's not having what you want - It's wanting what you've got.
|