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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. can delegate return multiple values with different datatypes

can delegate return multiple values with different datatypes

Scheduled Pinned Locked Moved C#
5 Posts 5 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.
  • S Offline
    S Offline
    suni_dotnet
    wrote on last edited by
    #1

    hi, iam using delegates... where iam calling delegate from methods(3 methods and returns single value) and the values set by these methods are used in another method. Now i need to retun 2 values from delegate with two different datatypes... i used object[] datatype, but unable to retrieve multiple values.. if is possible with what datatype delegate should be declared and how the values can be set and retrieved.. thanks in advance

    R L B J 4 Replies Last reply
    0
    • S suni_dotnet

      hi, iam using delegates... where iam calling delegate from methods(3 methods and returns single value) and the values set by these methods are used in another method. Now i need to retun 2 values from delegate with two different datatypes... i used object[] datatype, but unable to retrieve multiple values.. if is possible with what datatype delegate should be declared and how the values can be set and retrieved.. thanks in advance

      R Offline
      R Offline
      Rutvik Dave
      wrote on last edited by
      #2

      For that you need to create a Class with 2 Data Members with appropriate data type. and in the delegate create a object of that class and set both the variable some values and return the Object. i.e. you can also use property instead of public variables used in this example.

      Class C
      {
      public int i;
      public string s;
      }

      private C Method1()
      {
      C c1 = new C();

      c1.i = 10;
      c1.s = "test";

      return c1;

      }

      1 Reply Last reply
      0
      • S suni_dotnet

        hi, iam using delegates... where iam calling delegate from methods(3 methods and returns single value) and the values set by these methods are used in another method. Now i need to retun 2 values from delegate with two different datatypes... i used object[] datatype, but unable to retrieve multiple values.. if is possible with what datatype delegate should be declared and how the values can be set and retrieved.. thanks in advance

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You can create a new class, encapsulating both values and return that. Or read up on the out-parameter.

        I are troll :)

        1 Reply Last reply
        0
        • S suni_dotnet

          hi, iam using delegates... where iam calling delegate from methods(3 methods and returns single value) and the values set by these methods are used in another method. Now i need to retun 2 values from delegate with two different datatypes... i used object[] datatype, but unable to retrieve multiple values.. if is possible with what datatype delegate should be declared and how the values can be set and retrieved.. thanks in advance

          B Offline
          B Offline
          Ben Fair
          wrote on last edited by
          #4

          Well it should work using object[] return value from the delegate, but you may need to cast to the proper type. If your delegate it returning object, but it's actually an object[] that's been boxed, just cast the returned object to object[] like so:

          public delegate object ExecuteDelegate(int i, string s);
          ...
          object[] values = (object[])ExecuteDelegate(int i, string s);

          In this case you just unbox the object[] from the return value into the propery type. Or, if your delegate is returning object[] they should be accessible right away:

          public delegate object[] ExecuteDelegate(int i, string s);
          ...
          object[] values = ExecuteDelegate(int i, string s);

          It is sometimes helpful to create a class (as others have suggested) to represent the delegate's result values that have meaningful property names, etc., but it really depends on your situation. I'd say if the result data is complex or the delegate is used in multiple places to create a class for it; otherwise, just stick with object[].

          Keep It Simple Stupid! (KISS)

          1 Reply Last reply
          0
          • S suni_dotnet

            hi, iam using delegates... where iam calling delegate from methods(3 methods and returns single value) and the values set by these methods are used in another method. Now i need to retun 2 values from delegate with two different datatypes... i used object[] datatype, but unable to retrieve multiple values.. if is possible with what datatype delegate should be declared and how the values can be set and retrieved.. thanks in advance

            J Offline
            J Offline
            Jon Rista
            wrote on last edited by
            #5

            You can use the out parameter modifier:

            void MyCallback(int input, out string output1, out int output2);

            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