Daniel Jørgensen
Daniel Jørgensen

Reputation: 1202

C# - Returning mixed types in a function

I have some different data types that i need to do something with in a function. Those data needs to be processed in the function and returned as an object i believe it is called.

This is some not tested code i just wrote here, but i think it displays what im trying to do .. I hope you guys can help me out how to do it.

private void Button_Click(object sender, RoutedEventArgs e)
{
     // Here im calling the function which returns data to the object
     object thoseProcessedData = SomeTestObject(5, "ABC", SomeOtherThing);

     // When returned i want to be able to use the different data like so. 
     string useItLikeThis = thoseProcessedData.newString;
     int numbersLikeThis = thoseProcessedData.newNumber;
}

public object SomeTestObject(int numbers, string letters, AnotherType anothertype)
{

     string newString = letters.Substring(0,5);
     int newNumber = numbers + 10;
     AnotherType newType = anothertype.Something();

     return processedData;
}

Please guys dont kill me, if this is a too stupid question. Im still very new to C# ..

If you dont get what im trying to do, please ask! Since my english is not the best i thought this way would be the best to show you what i want..

Upvotes: 4

Views: 3700

Answers (6)

D Stanley
D Stanley

Reputation: 152491

Create a class to represent that data:

public class ProcessedData
{
     public string NewString {get; set;}
     public int NewNumber {get; set;}
     public AnotherType NewType {get; set;}
}

then populate an instance of that class and return it:

private void Button_Click(object sender, RoutedEventArgs e)
{
     // Here im calling the function which returns data to the object
     ProcessedData thoseProcessedData = SomeTestObject(5, "ABC", SomeOtherThing);

     // now you can access those properties 
     string useItLikeThis = thoseProcessedData.NewString;
     int numbersLikeThis = thoseProcessedData.NewNumber;
}

public ProcessedData SomeTestObject(int numbers, string letters, AnotherType anothertype)
{
     ProcessedData processedData = new ProcessedData();

     processedData.newString = letters.Substring(0,5);
     processedData.newNumber = numbers + 10;
     processedData.newType = anothertype.Something();

     return processedData;
}

There are mechanisms (anonymous types, dynamic) that would make it possible to dynamically "find" properties at run-time, but defining a class and statically typing the return type is by far safer.

Upvotes: 2

BradleyDotNET
BradleyDotNET

Reputation: 61339

Well, you are on the right track (apart from the use of the object keyword/class)! The object class is the base class of every reference type in C#, it has 3 or 4 functions, and no properties. You will very rarely directly use this class.

The simplest method to do what are you trying to accomplish what you want is to use a Tuple.

This would look like:

public Tuple<string, int, AnotherType> SomeTestObject(
    int numbers, string letters, AnotherType anothertype)
{
     string newString = letters.Substring(0,5);
     int newNumber = numbers + 10;
     AnotherType newType = anothertype.Something();

     return Tuple.Create(newString, newNumber, newType);
}

If, however, this is going to be used in other places, passed around, etc. you should create a separate object, populate it, and return it.

public MyDataClass SomeTestObject(
     int numbers, string letters, AnotherType anothertype)
{
     string newString = letters.Substring(0,5);
     int newNumber = numbers + 10;
     AnotherType newType = anothertype.Something();

     return new MyDataClass(newString, newNumber, newType);
}

//Somewhere else, probably another file
public class MyDataClass
{
    public string StringData {get; set;}
    public int NumberData {get; set;}
    public AnotherType ObjectData {get; set;}

    public MyDataClass(string myString, int, myNumber, AnotherType myObject)
    {
        StringData = myString;
        NumberData = myNumber;
        ObjectData = myObject;
    }
}

MSDN For:

Tuple

Object

Upvotes: 3

Icemanind
Icemanind

Reputation: 48686

When you need to return more than one value, you need to create your own class. A class (among other things) encapsulates or "packages together" one or more pieces of data. Here is an example:

public class MyCustomClass {
    public int MyCustomInt { get; set; }
    public string MyCustomString { get; set; }
    public bool MyCustomYesNo { get; set; }
}

This class contains three properties. Properties contain data that can be read from (get) or written to (set). You can now write a function that returns an instance of this property:

public MyCustomClass MyFunction()
{
    return new MyCustomClass() {
        MyCustomInt = 15, MyCustomString = "Hello World!",
        MyCustomYesNo = true
    };
}

This function will create a new instance of our MyCustomClass and fill each property with values. And now you can call it like this:

MyCustomClass myVar = MyFunction();
int myInt = myVar.MyCustomInt; // Contains 15
string myString = myVar.MyCustomString; // Contains "Hello World!"
bool myYesNo = myVar.MyCustomYesNo;  // Contains true

Now of course, your function can do anything it wishes. I was just providing an example. Hope this makes sense!

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236188

Create class which holds data you want to pass and return:

public class Data
{
   public string Letters { get; set; }
   public int Number { get; set; }
   public AnotherType Thing { get; set; }
}

Pass it to method:

var data = new Data { Letters = "ABC", Number = 5, Thing = SomeOtherThing };
DoSomething(data);
// here data will have modified values

Thus class is a reference type, all changes to its members inside DoSomething method, will be reflected in your data object reference. So, changes can look like:

public void DoSomething(Data data)
{
     data.Letters = data.Letters.Substring(0,5);
     data.Number += 10;
     data.Thing.Something();
}

Upvotes: 3

ChaosPandion
ChaosPandion

Reputation: 78242

For this you can create a simple class.

public class Data
{
    public string NewString { get; set; }
    public int NewNumber { get; set; }
}

Then you can return it from a method.

Data ReadData() 
{
    return new Data { 
        NewString = CalculateNewString(), 
        NewNumber = CalclulateNewNumber() 
    };
}

Upvotes: 0

Simon Whitehead
Simon Whitehead

Reputation: 65049

Firstly, your English is fine. Don't worry about that :)

Secondly, what you want to do is just create a class that has those properties on it.

class YourClass {
    public string NewString { get; set; }
    public int NewNumber { get; set; }
}

Then return an instance of this out of your method:

 return new YourClass() {
     NewString = letters.Substring(0, 5),
     NewNumber = numbers + 10
 };

Upvotes: 0

Related Questions