Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problems getting method called correctly

Problems getting method called correctly

Scheduled Pinned Locked Moved C#
csharphelpquestionlearning
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JMOdom
    wrote on last edited by
    #1

    :sigh:I am just learning C#, and am having a problem with it that I don't know what I'm doing wrong with. I have a method that asks for and recieves two numbers. These are parsed into two int's, and then stored. The next method is supposed to add the two numbers together, and give a return, i.e. sum. I also have outside the methods but inside the class "int firstNum, secondNum" declared. When I try to call the method using; DetermineNumberSum(firstNum, secondNum); I am told that my firstNum and secondNum are errors because of "Use of unassigned local variable 'firstNum/secondNum". I have not got a clue as to what is going on. Can anyone point me in the right direction so that I can solve this problem. Thank you in advance for your input.:confused::~

    K U 2 Replies Last reply
    0
    • J JMOdom

      :sigh:I am just learning C#, and am having a problem with it that I don't know what I'm doing wrong with. I have a method that asks for and recieves two numbers. These are parsed into two int's, and then stored. The next method is supposed to add the two numbers together, and give a return, i.e. sum. I also have outside the methods but inside the class "int firstNum, secondNum" declared. When I try to call the method using; DetermineNumberSum(firstNum, secondNum); I am told that my firstNum and secondNum are errors because of "Use of unassigned local variable 'firstNum/secondNum". I have not got a clue as to what is going on. Can anyone point me in the right direction so that I can solve this problem. Thank you in advance for your input.:confused::~

      K Offline
      K Offline
      Kamal Afridi
      wrote on last edited by
      #2

      just Initialize like below. firstNum =0,secondNum=0; i hope it will work

      Know Yourself Then Grow Yourself.

      J 1 Reply Last reply
      0
      • J JMOdom

        :sigh:I am just learning C#, and am having a problem with it that I don't know what I'm doing wrong with. I have a method that asks for and recieves two numbers. These are parsed into two int's, and then stored. The next method is supposed to add the two numbers together, and give a return, i.e. sum. I also have outside the methods but inside the class "int firstNum, secondNum" declared. When I try to call the method using; DetermineNumberSum(firstNum, secondNum); I am told that my firstNum and secondNum are errors because of "Use of unassigned local variable 'firstNum/secondNum". I have not got a clue as to what is going on. Can anyone point me in the right direction so that I can solve this problem. Thank you in advance for your input.:confused::~

        U Offline
        U Offline
        U P G R A Y E D D
        wrote on last edited by
        #3

        You are using the variables firstNum and secondNum, and have not assigned them a value. Local variables must have a value assigned to them before they can be used. You said something receives two numbers and parses them into ints, I can only assume it looks like this: Int32.Parse(strFirstNumber); Int32.Parse(strSecondNumber); Well, when you do that you must not be storing those values into either firstNum or secondNum. So after declaring firstNum and secondNum, aka "int firstNum, secondNum;" you need to say something like: firstNum = Int32.Parse(strFirstNumber); secondNum = Int32.Parse(strSecondNumber); Then call: DetermineNumberSum(firstNum, secondNum);

        J 1 Reply Last reply
        0
        • K Kamal Afridi

          just Initialize like below. firstNum =0,secondNum=0; i hope it will work

          Know Yourself Then Grow Yourself.

          J Offline
          J Offline
          JMOdom
          wrote on last edited by
          #4

          I'm sorry, I think that I didn't express myself correctly. the two numbers are initialized by whatever the user puts into the the original string. The string is then parsed into an "int". public static void fetchTwoNumbers(out int firstNum, out int secondNum) { Console.WriteLine("Type in the first number."); string strFirst = Console.ReadLine(); firstNum = int.Parse(strFirst); Console.WriteLine("Type in the second number:"); string strSecond = Console.ReadLine(); secondNum = int.Parse(strSecond); } As you can see some input is needed to initialize them. Thank you though.

          1 Reply Last reply
          0
          • U U P G R A Y E D D

            You are using the variables firstNum and secondNum, and have not assigned them a value. Local variables must have a value assigned to them before they can be used. You said something receives two numbers and parses them into ints, I can only assume it looks like this: Int32.Parse(strFirstNumber); Int32.Parse(strSecondNumber); Well, when you do that you must not be storing those values into either firstNum or secondNum. So after declaring firstNum and secondNum, aka "int firstNum, secondNum;" you need to say something like: firstNum = Int32.Parse(strFirstNumber); secondNum = Int32.Parse(strSecondNumber); Then call: DetermineNumberSum(firstNum, secondNum);

            J Offline
            J Offline
            JMOdom
            wrote on last edited by
            #5

            This is the coding I have at present. I think it is what you are saying. public static void Main() { int firstNum, secondNum; DetermineNumberSum(firstNum, secondNum); fetchTwoNumbers(out firstNum, out secondNum); } public static void fetchTwoNumbers(out int firstNum, out int secondNum) { Console.WriteLine("Type in the first number:"); string strFirst = Console.ReadLine(); firstNum = int.Parse(strFirst); Console.WriteLine("Type in the second number:"); string strSecond = Console.ReadLine(); secondNum = int.Parse(strSecond); } public static int DetermineNumberSum(int firstNum, int secondNum) { int numberSum; int first = firstNum, second = secondNum; numberSum = first + second; Console.WriteLine(); Console.WriteLine("The sum of the numbers is: {0}", numberSum); return first + second; } It should work but, I am still getting the "Use of unassigned local variable 'firstNum/secondNum'. I don't know why. Thank you for your input though.

            C 1 Reply Last reply
            0
            • J JMOdom

              This is the coding I have at present. I think it is what you are saying. public static void Main() { int firstNum, secondNum; DetermineNumberSum(firstNum, secondNum); fetchTwoNumbers(out firstNum, out secondNum); } public static void fetchTwoNumbers(out int firstNum, out int secondNum) { Console.WriteLine("Type in the first number:"); string strFirst = Console.ReadLine(); firstNum = int.Parse(strFirst); Console.WriteLine("Type in the second number:"); string strSecond = Console.ReadLine(); secondNum = int.Parse(strSecond); } public static int DetermineNumberSum(int firstNum, int secondNum) { int numberSum; int first = firstNum, second = secondNum; numberSum = first + second; Console.WriteLine(); Console.WriteLine("The sum of the numbers is: {0}", numberSum); return first + second; } It should work but, I am still getting the "Use of unassigned local variable 'firstNum/secondNum'. I don't know why. Thank you for your input though.

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              JMOdom wrote:

              int firstNum, secondNum; DetermineNumberSum(firstNum, secondNum);

              No, this cannot work. You need to assign values to firstNum and secondNum, OR use them as out parameters. You try to use them BEFORE using them as out parameters, and you didn't give them values to start with, which is always bad programming. You should also create one variable per line, for readability.

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups