1

I am returning string Subject from this class now if i want to return two more strings from this class how do i do it??

string Subject;

    public string getdata(string EmailFrom,string EmailTo, string EmailComment )
    {

        
        {

         
            scom.CommandType = CommandType.StoredProcedure;
           
           
            try
            {
                

                SqlDataReader rdr = scom.ExecuteReader();
                if (rdr.HasRows)
                {
                    while (rdr.Read())
                    {
                     

                    Subject = rdr["EmailSubject"].ToString();
                     
                         
                    
                    }
                }

                return Subject;
                 
Community
  • 1
  • 1
nitha raj
  • 139
  • 1
  • 1
  • 9

7 Answers7

3

You can use 'out' parameters.

var string1 = String.Empty;
var string2 = String.Empty;

public string MyMethod(out string string1, out string string2)
{
    string1 = "string1";
    string2 = "string2";
}

For more information on this : out (C# Reference)

You could return your own DTO (Data Transfer Object) class.

class MyResult {
   string Property1 { get; set;}
   string Property2 { get; set;}
}


public MyResult MyMethod(){
    var result = new MyResult();
    result.Property1 = "string1";
    result.Property2 = "string2";
    return result;
}

For more information on DTOs : http://en.wikipedia.org/wiki/Data_transfer_object

ChrisBint
  • 12,773
  • 6
  • 40
  • 62
1

Use out type parameters in class... for Example

static void FooClass(out int foo, out int bar)
{
    foo= (int)Math.Pow(2, 2);
    bar= (int)Math.Pow(3, 2);
}

Also you can use List<String> to get Datareader's value into it and return List collection.

Mayank Pathak
  • 3,621
  • 5
  • 39
  • 67
1

These are the available options for you.

  1. Use ref or out keywords.- Difference between ref and out parameters in .NET
  2. Create a class or struct and return the class or struct from the method
  3. If you are using .Net 4.0+ you can use Tuple class. - http://msdn.microsoft.com/en-us/library/system.tuple.aspx
Community
  • 1
  • 1
Anuraj
  • 18,859
  • 7
  • 53
  • 79
1

You can use Tuples to retun multiple parameters back as long as you dont mind the retuned params as tuple.Item1, tuple.Item2, tuple.Item3

public Tuple<string, string, string> GetData()
{

Tuple<string, string, string> tuple = new Tuple<string, string, string>("1",
        "cat", "dog");
return tuple;
}
HatSoft
  • 11,077
  • 3
  • 28
  • 43
0

You could create a class/struct with the properties you want to return or you could return an enumaration of strings (e.g. a List<string>)

Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58
0

Create an object such as

public class MailType
{
    string _subject=string.empty;
    string _string1=string.empty1;
    string _string2=string.empty2;

    public string Subject
    {
         get{
             return _subject;
         }
         set{
             _subject=value;
         }
    }

    public string String1
    {
         get{
             return _string1;
         }
         set{
             _string1=value;
         }
    }

    public string String2
    {
         get{
             return _string2;
         }
         set{
             _string2=value;
         }
    }
}

Create an object of this class in your loop, store values in Subject, String1 and String2 properties of the object and return the List from your function: getdata..

Shant
  • 237
  • 1
  • 3
  • 16
-1

You need to define a type that contains 3 fields, representing the information you want to return from the method. Than you change the signature of the method to return the type you defined. From within, you initialize an instance of this type with whatever data you fetched and return it.

Unfortunately, modern OO languages do not support returning two (or more things) from a method in the language level.

Vitaliy
  • 8,044
  • 7
  • 38
  • 66