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. Trying to Obtain Two (2) Values from Function

Trying to Obtain Two (2) Values from Function

Scheduled Pinned Locked Moved C#
csharplinqdata-structurestestingbeta-testing
7 Posts 7 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.
  • C Offline
    C Offline
    computerpublic
    wrote on last edited by
    #1

    Hello, In the following function, I am trying to return two (2) values instead of one. Can someone please say how this can be done? I was thinking possibly returning an array from the function, but I do not know if this is possible. Does anyone have an idea of how multiple values can be obtain from the same function? Thanks.

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

    namespace Testing_Functions
    {
    class Program
    {
    public static Double Test(Double Val1, Double Val2, Double Val3)
    {
    Double Test_Val = Val1 + Val2 + Val3;
    Double Second_Test = Test_Val % 2;
    return Test_Val;
    //return Second_Test;//I need to return this value also(two values, NOT ONE.)
    }
    static void Main(string[] args)
    {
    Double A = 1, B = 2, C = 3;
    Double Result = Test(A,B,C);
    //Double Result_2 = Test(A, B, C);
    }
    }
    }

    D S V K 4 Replies Last reply
    0
    • C computerpublic

      Hello, In the following function, I am trying to return two (2) values instead of one. Can someone please say how this can be done? I was thinking possibly returning an array from the function, but I do not know if this is possible. Does anyone have an idea of how multiple values can be obtain from the same function? Thanks.

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

      namespace Testing_Functions
      {
      class Program
      {
      public static Double Test(Double Val1, Double Val2, Double Val3)
      {
      Double Test_Val = Val1 + Val2 + Val3;
      Double Second_Test = Test_Val % 2;
      return Test_Val;
      //return Second_Test;//I need to return this value also(two values, NOT ONE.)
      }
      static void Main(string[] args)
      {
      Double A = 1, B = 2, C = 3;
      Double Result = Test(A,B,C);
      //Double Result_2 = Test(A, B, C);
      }
      }
      }

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      There are a few ways of obtaining multiple values. 1. Create an struct/class that holds more than one value 2. Use an array or list (the same as 1 really) 3. Use an out (or ref if needed) parameter. 1:

      public class Pair<T>
      {
      private T first;
      private T second;

      public Pair(T first, T second)
      {
          this.first = first;
          this.second = second;
      }
      
      public T First { get { return first; } }
      public T Second { get { return second; } }
      

      }

      public static Double Test(Double Val1, Double Val2, Double Val3)
      {
      Double Test_Val = Val1 + Val2 + Val3;
      Double Second_Test = Test_Val % 2;
      return new Pair<Double>(Test_Val, Second_Test);
      }

      2 should be straight forward. 3:

      public static Double Test(Double Val1, Double Val2, Double Val3, out Double secondResult)
      {
      Double Test_Val = Val1 + Val2 + Val3;
      secondResult = Test_Val % 2;
      return Test_Val;
      }

      Dave
      Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

      B K 2 Replies Last reply
      0
      • D DaveyM69

        There are a few ways of obtaining multiple values. 1. Create an struct/class that holds more than one value 2. Use an array or list (the same as 1 really) 3. Use an out (or ref if needed) parameter. 1:

        public class Pair<T>
        {
        private T first;
        private T second;

        public Pair(T first, T second)
        {
            this.first = first;
            this.second = second;
        }
        
        public T First { get { return first; } }
        public T Second { get { return second; } }
        

        }

        public static Double Test(Double Val1, Double Val2, Double Val3)
        {
        Double Test_Val = Val1 + Val2 + Val3;
        Double Second_Test = Test_Val % 2;
        return new Pair<Double>(Test_Val, Second_Test);
        }

        2 should be straight forward. 3:

        public static Double Test(Double Val1, Double Val2, Double Val3, out Double secondResult)
        {
        Double Test_Val = Val1 + Val2 + Val3;
        secondResult = Test_Val % 2;
        return Test_Val;
        }

        Dave
        Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

        B Offline
        B Offline
        BobJanova
        wrote on last edited by
        #3

        out and ref parameters are usually considered to be dirty, so stay away from that if you can. Depending on what the function does, you may want to create a wrapper class or struct for the result. That means that you get semantically useful names, instead of First and Second from the Pair class.

        1 Reply Last reply
        0
        • C computerpublic

          Hello, In the following function, I am trying to return two (2) values instead of one. Can someone please say how this can be done? I was thinking possibly returning an array from the function, but I do not know if this is possible. Does anyone have an idea of how multiple values can be obtain from the same function? Thanks.

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

          namespace Testing_Functions
          {
          class Program
          {
          public static Double Test(Double Val1, Double Val2, Double Val3)
          {
          Double Test_Val = Val1 + Val2 + Val3;
          Double Second_Test = Test_Val % 2;
          return Test_Val;
          //return Second_Test;//I need to return this value also(two values, NOT ONE.)
          }
          static void Main(string[] args)
          {
          Double A = 1, B = 2, C = 3;
          Double Result = Test(A,B,C);
          //Double Result_2 = Test(A, B, C);
          }
          }
          }

          S Offline
          S Offline
          Sangramsingh Pawar
          wrote on last edited by
          #4

          Use ref and out keyword to return multiple values from function
          return statement returns only single value.

          1 Reply Last reply
          0
          • D DaveyM69

            There are a few ways of obtaining multiple values. 1. Create an struct/class that holds more than one value 2. Use an array or list (the same as 1 really) 3. Use an out (or ref if needed) parameter. 1:

            public class Pair<T>
            {
            private T first;
            private T second;

            public Pair(T first, T second)
            {
                this.first = first;
                this.second = second;
            }
            
            public T First { get { return first; } }
            public T Second { get { return second; } }
            

            }

            public static Double Test(Double Val1, Double Val2, Double Val3)
            {
            Double Test_Val = Val1 + Val2 + Val3;
            Double Second_Test = Test_Val % 2;
            return new Pair<Double>(Test_Val, Second_Test);
            }

            2 should be straight forward. 3:

            public static Double Test(Double Val1, Double Val2, Double Val3, out Double secondResult)
            {
            Double Test_Val = Val1 + Val2 + Val3;
            secondResult = Test_Val % 2;
            return Test_Val;
            }

            Dave
            Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

            K Offline
            K Offline
            Keith Barrow
            wrote on last edited by
            #5

            Also, he could return a Tuple<_type_,_type_> but by far the best is solution 1.

            Sort of a cross between Lawrence of Arabia and Dilbert.[^]
            -Or-
            A Dead ringer for Kate Winslett[^]

            1 Reply Last reply
            0
            • C computerpublic

              Hello, In the following function, I am trying to return two (2) values instead of one. Can someone please say how this can be done? I was thinking possibly returning an array from the function, but I do not know if this is possible. Does anyone have an idea of how multiple values can be obtain from the same function? Thanks.

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

              namespace Testing_Functions
              {
              class Program
              {
              public static Double Test(Double Val1, Double Val2, Double Val3)
              {
              Double Test_Val = Val1 + Val2 + Val3;
              Double Second_Test = Test_Val % 2;
              return Test_Val;
              //return Second_Test;//I need to return this value also(two values, NOT ONE.)
              }
              static void Main(string[] args)
              {
              Double A = 1, B = 2, C = 3;
              Double Result = Test(A,B,C);
              //Double Result_2 = Test(A, B, C);
              }
              }
              }

              V Offline
              V Offline
              V 0
              wrote on last edited by
              #6

              the quick and dirty way:

              public static Double[] Test(Double Val1, Double Val2, Double Val3)
              {

                      Double Test\_Val = Val1 + Val2 + Val3;
                      Double Second\_Test = Test\_Val % 2;
                      double \[\] arr = { Test\_Val, Second\_Test };
                      return arr;
                      //return Second\_Test;//I need to return this value also(two values, NOT ONE.)
                  }
              

              Just to show you, you can return virtually anything, including Lists, Dictionaries, self made object (like the Pair example)

              V.

              1 Reply Last reply
              0
              • C computerpublic

                Hello, In the following function, I am trying to return two (2) values instead of one. Can someone please say how this can be done? I was thinking possibly returning an array from the function, but I do not know if this is possible. Does anyone have an idea of how multiple values can be obtain from the same function? Thanks.

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

                namespace Testing_Functions
                {
                class Program
                {
                public static Double Test(Double Val1, Double Val2, Double Val3)
                {
                Double Test_Val = Val1 + Val2 + Val3;
                Double Second_Test = Test_Val % 2;
                return Test_Val;
                //return Second_Test;//I need to return this value also(two values, NOT ONE.)
                }
                static void Main(string[] args)
                {
                Double A = 1, B = 2, C = 3;
                Double Result = Test(A,B,C);
                //Double Result_2 = Test(A, B, C);
                }
                }
                }

                K Offline
                K Offline
                Kuthuparakkal
                wrote on last edited by
                #7

                A Class (object) is the best way. But if you do not have more than two values, you can always use a Dictionary<>, or KeyValuePair etc System.Collections.Generic.Dictionary : http://msdn.microsoft.com/en-us/library/xfhwa508.aspx KeyValuePair : http://msdn.microsoft.com/en-us/library/5tbh8a42.aspx

                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