none
Error in calling static int RRS feed

  • Question

  • //07-18-2018
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace DigitToWord
    {
        class Program
        {
            static public string NumbertoWord(int Number)
            {
                string[] words =
               {
                    "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"
                };
                if ((Number >= 1) && (Number <= 9))
                {
                    return words[Number - 1];
                }
                else
                {
                    return Number.ToString();
                }
            }
            static int Number(string promt)
            {
                Console.Write("{0}: ", promt);
                return Convert.ToInt32(Console.ReadLine());
            }
            static int Add(int a, int b, out string word)
            {
            
                word = "plus";
                return a + b;
            }
            static int Sub(int a, int b, out string word)
            {
                word = "minus";
                return a - b;
            }
            static int Mul(int a, int b, out string word)
            {
                word = "times";
                return a * b;
            }
            static int Div(int a, int b, out string word)
            {
                word = "divided by";
                if (b == 0)
                {
                    Console.WriteLine("Division by zero.");
                    return Int32.MaxValue;
                }
                return a / b;
            }
            static public void Main(string [] args)
            {
                int  answer;
                string first, firstLetter, second;
                Console.Write(" Enter first number: ");
                int a = int.Parse(Console.ReadLine());
                Console.Write(" Enter second number: ");
                int b = int.Parse(Console.ReadLine());
                first = NumbertoWord(a);
                second = NumbertoWord(b);
               
                Console.Write("Press A to add, S to subtract, M to Multply, or D to divide:");
                Console.ReadLine();
                

                {
                    firstLetter = first.Substring(0, 1).ToUpper();
                    first = firstLetter + first.Substring(1);
                }

                Console.Write("\nPress Enter to end.");
                Console.ReadLine();
            }
        }
    }

    I'm trying to call my static int but it cannot be called from my maim.

    Hope someone can help me to explain why??

    Thanks

    Thursday, July 19, 2018 4:12 AM

All replies

  • More information please. Which "static int" do you mean, and what do you mean by "cannot be called"? If there is an error, you should give us the text of the error and if possible the line it occurs on, so we know where to look. Just saying it "cannot be called" doesn't give us much to go on.
    Thursday, July 19, 2018 4:57 AM
  • You are unable to call your methods because you are not getting your option to call your method. In c#, without calling method it's impossible to perform any operation. Please check following modified code it will work.

    using System;
    
    namespace Tutlane
    {
        class Program
        {
            static public string NumbertoWord(int Number)
            {
                string[] words =
               {
                    "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"
                };
                if ((Number >= 1) && (Number <= 9))
                {
                    return words[Number - 1];
                }
                else
                {
                    return Number.ToString();
                }
            }
            static int Number(string promt)
            {
                Console.Write("{0}: ", promt);
                return Convert.ToInt32(Console.ReadLine());
            }
            static int Add(int a, int b, out string word)
            {
    
                word = "plus";
                return a + b;
            }
            static int Sub(int a, int b, out string word)
            {
                word = "minus";
                return a - b;
            }
            static int Mul(int a, int b, out string word)
            {
                word = "times";
                return a * b;
            }
            static int Div(int a, int b, out string word)
            {
                word = "divided by";
                if (b == 0)
                {
                    Console.WriteLine("Division by zero.");
                    return Int32.MaxValue;
                }
                return a / b;
            }
            static void Main(string[] args)
            {
                int answer = 0;
                string first, firstLetter, second;
                Console.Write(" Enter first number: ");
                int a = int.Parse(Console.ReadLine());
                Console.Write(" Enter second number: ");
                int b = int.Parse(Console.ReadLine());
                first = NumbertoWord(a);
                second = NumbertoWord(b);
                Console.Write("Press A to add, S to subtract, M to Multply, or D to divide:");
                string name = Console.ReadLine();
                string word = "";
                if (name == "A")
                    answer = Add(a, b, out word);
                else if (name == "S")
                    answer = Sub(a, b, out word);
                else if (name == "M")
                    answer = Mul(a, b, out word);
                else if (name == "D")
                    Div(a, b, out word);
                {
                    firstLetter = first.Substring(0, 1).ToUpper();
                    first = firstLetter + first.Substring(1);
                }
                Console.Write("Operation: {0}, Result: {1}", word, answer);
                Console.Write("\nPress Enter to end.");
                Console.ReadLine();
            }
        }
    }
    

    • Proposed as answer by Tim Roberts Thursday, July 19, 2018 4:44 PM
    Thursday, July 19, 2018 6:00 AM
  • I suspect you mean this isn't working.

    Number("Enter a value");

    That isn't going to work because you already declare the identifier Number in NumbertoWord as the parameter. Since Number is defined as a parameter it won't find the Number method you defined. In general this is a poor programming style problem. Parameters and variables are camel cased in general so simply right clicking the Number parameter and renaming to number (lowercase) will resolve the issue and allow you to call your Number method. It is important that you let VS rename the identifier for you because it'll fix up all the places you're using it. If you try to do it manually you'll then have to manually go through this method and fix up all references to the parameter as well.

    static public string NumbertoWord ( int number )
    {
       ...
       if ((number >= 1 && (number <= 9))
         ...
    }

    The other styling issue you have is that your method name is not descriptive. Methods are actions and therefore should be (following standard .NET guidelines) Pascal cased and verbs. Number is a noun and is not descriptive of what the method does. Without looking at the implementation there would be no way of knowing what it is doing. A simple rename will resolve this as well.

    //static int Number ( string promt )
    
    //Change name to action and fix misspelling in parameter name
    static int PromptForNumber ( string prompt )

     


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, July 19, 2018 1:56 PM
    Moderator
  • Of course, the OP could do as I asked and give us more information about the exact problem, so we don't have to guess. But I suppose there's no need for that while there's someone like Suresh to re-write the code.
    Friday, July 20, 2018 1:17 AM