none
calling a method from another class

    Question

  • I have the following method:

    public void CorrectInputCheckNumber1(int number1)
            {
                if (number1 >= 2 && number1 <= 8)
                {
                    UserInputNumber2()
                }
                else
                {
                    Console.WriteLine("Invalid number");
                }
            }

    The problem is that the

    UserInputNumber2()

    method is in another class and so I get a "The name 'UserInputNumber2' does not exist in the current context"   error.

    How can I call that method into this one?  I know I could move this method into the other one and it would probably work, but my professor tells us that we should not have logical expressions or computations in our main class.  That should only be for instance objects and input/output methods and statements.

    The purpose if this method is to take the user input from another method, check it, and if it is within the parameters, call another method to request the user input a second number.  If it fails the check, an error message is displayed.

    Sunday, February 12, 2012 9:42 PM

Answers

  • 1) Create Public class within same namespace (e.g MyClass)

    2) Add public method "UserInputNumber2()

    3) Create instance of MyClass within MainClass

    4) Call your method

    So your code look like this

    Public class MyClass
    {
    
        public void UserInputNumber2()
        {
            //Add your code here
        }
    
    }
    
    
     public void CorrectInputCheckNumber1(int number1)
     {
         if (number1 >= 2 && number1 <= 8)
         {
             MyClass obj = new MyClass();
             obj.UserInputNumber2();
         }
         else
         {
             Console.WriteLine("Invalid number");
         }
    }


    Sunday, February 12, 2012 10:02 PM

All replies

  • Read this article.  It sounds like your professor wants you to declare the methods in a class, and create an instance object of the class so that you can call the methods that you defined in the class. 

    Object-Oriented Programming (C#and Visual Basic)  

    http://msdn.microsoft.com/en-us/library/dd460654.aspx#InstantiatingClasses

    It's a long story, and we are not sure how much know and/or understand.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/


    Sunday, February 12, 2012 9:55 PM
    Moderator
  • 1) Create Public class within same namespace (e.g MyClass)

    2) Add public method "UserInputNumber2()

    3) Create instance of MyClass within MainClass

    4) Call your method

    So your code look like this

    Public class MyClass
    {
    
        public void UserInputNumber2()
        {
            //Add your code here
        }
    
    }
    
    
     public void CorrectInputCheckNumber1(int number1)
     {
         if (number1 >= 2 && number1 <= 8)
         {
             MyClass obj = new MyClass();
             obj.UserInputNumber2();
         }
         else
         {
             Console.WriteLine("Invalid number");
         }
    }


    Sunday, February 12, 2012 10:02 PM
  • You need to create an instance of an object and invoke it's methods through that object.

    If the method is static, then you should call the method through the class.

    It seems to be your homework. You should play around with the OOPS concepts to learn them.

    Consider watching On-Demand webcasts on .NET fundamentals and OOPS, C# basics. Books are always the best choice.

    Learn the fundamentals to master OOPS.


    Welcome to MSDN Forums.
    Feel free to ask your questions and Please Note to Vote helpful topics and Mark answering posts.

    You Value Community's efforts and Community values your faith.
    - Arun Kumar Allu

    Sunday, February 12, 2012 11:25 PM