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. Function

Function

Scheduled Pinned Locked Moved C#
data-structuresquestion
4 Posts 4 Posters 5 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Is it possible to write a function that return 2 different data types? e.g. if it's TRUE, it returns a string, and if it's FALSE, returns an array of string. Thanks alot! ;)

    C A E 3 Replies Last reply
    0
    • L Lost User

      Is it possible to write a function that return 2 different data types? e.g. if it's TRUE, it returns a string, and if it's FALSE, returns an array of string. Thanks alot! ;)

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

      If C# supports a VARIANT you could do it that way, so long as both types were able to be stuffed into a VARIANT. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001

      Sonork ID 100.10002:MeanManOz

      I live in Bob's HungOut now

      1 Reply Last reply
      0
      • L Lost User

        Is it possible to write a function that return 2 different data types? e.g. if it's TRUE, it returns a string, and if it's FALSE, returns an array of string. Thanks alot! ;)

        A Offline
        A Offline
        Andreas Philipson
        wrote on last edited by
        #3

        You could also use the out keyword.

        public bool OurFunction (out object value2)
        {
        bool value; //return value;

        //do whatever...

        if (value)
        value2 = "This object returned true!!!";
        else
        value2 = new string [] {"This object", "returned", "true", "!!!"};

        return value;
        }

        Or you could simply just return an object and then check the type wheter it's true of false...

        public object OurFunction2 ()
        {
        bool value;
        object value2;

        // do whatever...

        if (value)
        value2 = "This object returned true!!!";
        else
        value2 = new string [] {"This object", "returned", "true", "!!!"};

        return value2;
        }

        public void MainFunction ()
        {
        object obj = this.OurFunction2 ();
        if (obj.GetType () == typeof (string)) //true
        Console.WriteLine ("TRUE!!!")
        else // false...
        Console.WriteLine ("FALSE!!!");
        }

        :) Andreas Philipson

        1 Reply Last reply
        0
        • L Lost User

          Is it possible to write a function that return 2 different data types? e.g. if it's TRUE, it returns a string, and if it's FALSE, returns an array of string. Thanks alot! ;)

          E Offline
          E Offline
          Eric Gunnerson msft
          wrote on last edited by
          #4

          It could return object, which could either be a string or an array of string. In general, however, I think that's a bad idea. Why not return an array with a single item for the first case?

          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