2

I have a question in returning two values from a method.

I have a method from which I have to return two values. I did like this.

public string fileName(string rate,out string xmlName,out string xsdName)
{
    if(rate=="2013")
    {
       xmlName="abc";
       xsdName="def";
    }

    if(rate=="2014")
    {
       xmlName="pqr";
       xsdName="xyz";
    }

    //stmt 1 (Here it is asking to give default values for xmlname and xsdName.But i dont want to give any default values.)
}

Now in another class I have to call this function and assign these values of xmlname and xsdName in that class. How can I do this?

sindhu jampani
  • 504
  • 3
  • 10
  • 30
  • 8
    if your method has to return more than one object, use a class containing the data or use two methods. Rethink your logic ! – Xaruth Mar 03 '14 at 17:09

6 Answers6

7
public OtherClass fileName(string rate)
{
    OtherClass obj = new OtherClass();
    if (rate == "2013")
    {
        obj.xmlName = "abc";
        obj.xsdName = "def";
    }
    if (rate == "2014")
    {
        obj.xmlName = "pqr";
        obj.xsdName = "xyz";
    }
    return obj;
}

public class OtherClass
{
    public string xmlName { get; set; }
    public string xsdName { get; set; }
}
Seany84
  • 5,526
  • 5
  • 42
  • 67
4

You'd use it like this:

string xmlName;
string xsdName;
fileName("2014", out xmlName, out xsdName);

Your fileName() method can have a return type of void since you're not technically returning anything from the function.

itsme86
  • 19,266
  • 4
  • 41
  • 57
3

The best concept would be using the Objective-Oriented programming. Create a class that has two properties, xmlName and xsdName. Then return a new instance of the class from the method.

The code below should give you an idea.

Class implementation in file XmlFile.cs

public class XmlFile
{
    public string XmlName
    { get; set; }
    public string XsdName
    { get: set; }
}

Function implementation:

public XmlFile fileName(string rate)
{
    XmlFile file = new XmlFile();

    if (rate == "2013")
    {
        file.XmlName = "abc";
        file.XsdName = "def";
    }

    if (rate == "2014")
    {
        file.XmlName = "pqr";
        file.XsdName = "xyz";
    }

    return file;
}

Please look at OO programming in detail, you will need it with C#.

Legoless
  • 10,942
  • 7
  • 48
  • 68
2

Your question is vague, but I'll try my best.

Here's how to call fileName and receive the output for the last two parameters:

string xmlName;
string xsdName;

myInstance.fileName("2014", out xmlName, out xsdName);

I tend to shy away from using out. A better solution is to create a new class that wraps the data:

public class File
{
    public File(string fileName, string xmlName, string xsdName)
    {
        FileName = fileName;
        XmlName = xmlName;
        XsdName = xsdName;
    }

    public string FileName
    {
        get;
        private set;
    }

    public string XmlName
    {
        get;
        private set;
    }

    public string XsdName
    {
        get;
        private set;
    }
}

public class OtherClass
{
    public File FileName(string rate)
    {
        switch (rate)
        {
            case "2013":
                return new File(..., "abc", "def");
            case "2014":
                return new File(..., "pqr", "xyz");
            default:
                throw new ArgumentException(String.Format("Unexpected rate '{0}'.", rate)); // Or, simply return null
        }
    }
}
NathanAldenSr
  • 7,841
  • 4
  • 40
  • 51
2

To call a function with out parameters:

string xmlN;
string xsdN;
var result = fileName("rate value", out xmlN, out xsdN);

But one problem is that you need to assign them before the end of the function. In this case, setting them to null might be appropriate.

crashmstr
  • 28,043
  • 9
  • 61
  • 79
2

There are several possible ways to do it. First is how you do it - output parameters

string xmlName, xsdName;
filename("xxx", out xmlName, out xsdName);

Second one is to use tuples.

public Tuple<string, string> fileName(string rate) 
{
    ...
    return Tuple.Create(xmlName, xsdName)
}

Third one is to define you own class

class XmlInfo
{
    public string XmlName {get; set;}
    public string XsdName {get; set;}
}


XmlInfo filename(string rate)
{
    ...
    return new XmlInfo() { XmlName = xmlName, XsdName = xsdName };
}
Xaruth
  • 4,034
  • 3
  • 19
  • 26
Vasily Semenov
  • 302
  • 3
  • 6