• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

learning C#

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Wathnix

Member
Joined
Feb 8, 2004
Hi all,

I used to do a lot of programming waaaaay back with commodore 64 BASIC and now I am just starting a course in C# (visual studio), we are making simple console applications. Im stuck on a problem. I'm trying to make a program that will calls on a subroutine to display a short list of options then input a choice from the user and return that value to the main body of the program. the subroutine works fine, but does not return the value of the variable to the main body of the program. This was really easy in BASIC and seems really hard in C#. I'm including the program I have so far.

using System;

class MainBodyOfProgram
{
static void Main (string [] args)
{

Console.WriteLine("Welcome to the calculator\n");
SubRoutine1 menu = new SubRoutine1(); //compiles subroutine1 for use
menu.DisplayMenu(); // displays the menu choices and gets input choice from user
Console.WriteLine("You chose option: {0}", a);
}
}



// start of subroutine1, display a menu, user selects a choice and subroutine puts it to variable 'a'
class SubRoutine1
{
public void DisplayMenu()
{
int a = 0;
Console.WriteLine("\n\nCalculator Menu");
Console.WriteLine("1)\tAdd");
Console.WriteLine("2)\tSubtract");
Console.WriteLine("3)\tMultiply");
Console.WriteLine("4)\tDivide");
Console.WriteLine("5)\tModulous");
Console.WriteLine("6)\tQuit");
Console.WriteLine("------------------------------------");
Console.Write("Enter Your Selection [1-6]: ");
a = Convert.ToInt32(Console.ReadLine());
}
}
 
You are using a local variable ("a") in "DisplayMenu", but it is never returned. Additionally, you are using "a" in WriteLine, but that variable has no meaning in that scope (you didn't declare a variable "a" and the code you gave doesn't compile). You have a few options to pull that back out. You can simply return the value of DisplayMenu as an integer, and dump it right to console.

Code:
using System;

class MainBodyOfProgram {
    static void Main(string[] args) {

        Console.WriteLine("Welcome to the calculator\n");
        SubRoutine1 menu = new SubRoutine1(); //compiles subroutine1 for use
        Console.WriteLine("You chose option: {0}", [COLOR=#ff0000]menu.DisplayMenu()[/COLOR]);
        Console.ReadKey();
    }
}



// start of subroutine1, display a menu, user selects a choice and subroutine puts it to variable 'a'
class SubRoutine1 {
    public [COLOR=#ff0000]int[/COLOR] DisplayMenu() {
        Console.WriteLine("\n\nCalculator Menu");
        Console.WriteLine("1)\tAdd");
        Console.WriteLine("2)\tSubtract");
        Console.WriteLine("3)\tMultiply");
        Console.WriteLine("4)\tDivide");
        Console.WriteLine("5)\tModulous");
        Console.WriteLine("6)\tQuit");
        Console.WriteLine("------------------------------------");
        Console.Write("Enter Your Selection [1-6]: ");
        [COLOR=#ff0000]return Convert.ToInt32(Console.ReadLine());[/COLOR]
    }
}
This is a quick and dirty solution. If you wanted to display it in two places, or do something useful, you'd need to store the value, like so:

Code:
using System;

class MainBodyOfProgram {
    static void Main(string[] args) {

        Console.WriteLine("Welcome to the calculator\n");
        SubRoutine1 menu = new SubRoutine1(); //compiles subroutine1 for use
        [COLOR=#ff0000]int GivenAnswer = menu.DisplayMenu();[/COLOR] // displays the menu choices and gets input choice from user
        Console.WriteLine("You chose option: {0}", [COLOR=#ff0000]GivenAnswer[/COLOR]);
        Console.ReadKey();
    }
}



// start of subroutine1, display a menu, user selects a choice and subroutine puts it to variable 'a'
class SubRoutine1 {
    public [COLOR=#ff0000]int[/COLOR] DisplayMenu() {
        Console.WriteLine("\n\nCalculator Menu");
        Console.WriteLine("1)\tAdd");
        Console.WriteLine("2)\tSubtract");
        Console.WriteLine("3)\tMultiply");
        Console.WriteLine("4)\tDivide");
        Console.WriteLine("5)\tModulous");
        Console.WriteLine("6)\tQuit");
        Console.WriteLine("------------------------------------");
        Console.Write("Enter Your Selection [1-6]: ");
        [COLOR=#ff0000]return Convert.ToInt32(Console.ReadLine());[/COLOR]
    }
}
However, you may want to take that a step further and create variables within the "SubRoutine1" option that stores and retrieves the variable, like so:

Code:
using System;

class MainBodyOfProgram {
    static void Main(string[] args) {

        Console.WriteLine("Welcome to the calculator\n");
        SubRoutine1 menu = new SubRoutine1(); //compiles subroutine1 for use
        menu.DisplayMenu(); // displays the menu choices and gets input choice from user
        Console.WriteLine("You chose option: {0}", [COLOR=#ff0000]menu.StoredAnswer[/COLOR]);
        Console.ReadKey();
    }
}



// start of subroutine1, display a menu, user selects a choice and subroutine puts it to variable 'a'
class SubRoutine1 {

   [COLOR=#ff0000] public int StoredAnswer { get; private set; } //Parameter to store the answer[/COLOR]

    public void DisplayMenu() {
        Console.WriteLine("\n\nCalculator Menu");
        Console.WriteLine("1)\tAdd");
        Console.WriteLine("2)\tSubtract");
        Console.WriteLine("3)\tMultiply");
        Console.WriteLine("4)\tDivide");
        Console.WriteLine("5)\tModulous");
        Console.WriteLine("6)\tQuit");
        Console.WriteLine("------------------------------------");
        Console.Write("Enter Your Selection [1-6]: ");
        [COLOR=#ff0000]StoredAnswer = Convert.ToInt32(Console.ReadLine());[/COLOR]
    }
}
It all depends on what you are trying to do. It is a very good idea to store like objects (and their parameters, variables) in the same object, like my last example here. This not only makes the program easier to follow, you can prevent accidental modification of variables that shouldn't change by disallowing external objects to modify them, which greatly reduces the number of possible bugs you can have.
 
Last edited:
Back