3 Replies - 757 Views - Last Post: 25 September 2008 - 01:40 PM Rate Topic: -----

#1 Armanian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 08-September 07

Console: Classes

Post icon  Posted 25 September 2008 - 12:11 PM

Hi,

I need help with classes in the console project type... i'm experimenting with classes but my program only runs half way and i dont know why.

Any improvements on the code would be greatly appreciated, and also advice on ways to lay it out would also be appreciated.


Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Task_3
{
	class first
	{
	   
		public void Display()
		{
			Console.WriteLine("  ********************************** \n");
			Console.WriteLine("			  Menu \n");
			Console.WriteLine("  **********************************");

			Console.Write("  1. ");
			Console.WriteLine("Add number A to number B");
			Console.Write("  2. ");
			Console.WriteLine("Subtract number A from number B");
			Console.Write("  3. ");
			Console.WriteLine("Multiply number A by number B");
			Console.Write("  4. ");
			Console.WriteLine("Divide number A by number B");

			Console.WriteLine("  ********************************** \n");

			Console.WriteLine("  Please Select a number: ");
			int C = Convert.ToInt32(Console.ReadLine());
		}

	}
	 
	class second
		{

		public int A = 8, B = 17, D;

		public void Display()
			{

				if (C == 1)
				{
					Console.WriteLine("  ****************** \n");
					Console.WriteLine("  Option 1 Add \n");
					D = A + B;
					Console.WriteLine(D);
				}
				/*else if (C == "2")
				{
					Console.WriteLine("  ****************** \n");
					Console.WriteLine("  Option 2 Subtract \n");
				}
				*/
			}
		}
	

	class Program
	{
		static void Main(string[] args)
		{
			first f1;		// f1 = first 1
			second s1;   // s1 = second 1

			f1 = new first();
			s1 = new second();

			f1.Display();
			s1.Display();

			Console.ReadLine();
		}
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Console: Classes

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Console: Classes

Posted 25 September 2008 - 01:07 PM

the variable that you are using to store the number that they choose, "int C", it a local variable to class "first". In it's current context, it can't be used outside of class "first".

change it to this....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Task_3
{
    class first
    {
       
        public int Display()
        {
            Console.WriteLine("  ********************************** \n");
            Console.WriteLine("              Menu \n");
            Console.WriteLine("  **********************************");

            Console.Write("  1. ");
            Console.WriteLine("Add number A to number B");
            Console.Write("  2. ");
            Console.WriteLine("Subtract number A from number B");
            Console.Write("  3. ");
            Console.WriteLine("Multiply number A by number B");
            Console.Write("  4. ");
            Console.WriteLine("Divide number A by number B");

            Console.WriteLine("  ********************************** \n");

            Console.WriteLine("  Please Select a number: ");
            int C = Convert.ToInt32(Console.ReadLine());

            return C;
        }

    }
     
    class second
        {

        public int A = 8, B = 17, D;

        public void Display(int C)
            {

                if (C == 1)
                {
                    Console.WriteLine("  ****************** \n");
                    Console.WriteLine("  Option 1 Add \n");
                    D = A + B;
                    Console.WriteLine(D);
                }
                /*else if (C == "2")
                {
                    Console.WriteLine("  ****************** \n");
                    Console.WriteLine("  Option 2 Subtract \n");
                }
                */
            }
        }
    

    class Program
    {
        static void Main(string[] args)
        {
            first f1;        // f1 = first 1
            second s1;   // s1 = second 1

            f1 = new first();
            s1 = new second();

            int numberChosen = f1.Display();
            s1.Display(numberChosen);

            Console.ReadLine();
        }
    }
}



that should work.

EDITED: here is what I changed:

line 11 & 31: the "Display" method of class "first" will now return the number that was chosen
line 41: the "Display" method of class "second" will now accept an integer as the number that was chosen
line 71: catching the returned value from class "first"
line 72: sending the returned value from class "first" to class "second".

This post has been edited by eclipsed4utoo: 25 September 2008 - 01:39 PM

Was This Post Helpful? 0
  • +
  • -

#3 Armanian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 08-September 07

Re: Console: Classes

Posted 25 September 2008 - 01:26 PM

Works Great Thanks very much =]

I'll research the bits you added in so that i can understand them
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Console: Classes

Posted 25 September 2008 - 01:40 PM

I know that you are just playing around and testing, but using more descriptive names(class names and variable names) will help out also.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1