C # - Returning mixed types to a function

I have several different types of data that I need to do with a function. This data should be processed in the function and returned as an object, which, I believe, is called.

This is some kind of untested code that I just wrote here, but I think it shows what I'm trying to do. Hope you guys can help me how to do this.

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 do not kill me if this is too stupid a question. Im still very new to C # ..

If you do not get what I'm trying to do, ask! Since my English is not the best, I thought this method would be the best to show you what I want.

+4
source share
6

, ( / object)! object #, 3 4 . .

, , Tuple.

:

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);
}

, , , .., , .

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 :

Tuple

Object

+3

, , :

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

:

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

, , DoSomething data. , :

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

:

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

:

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;
}

( , dynamic), "" , .

+1

-, . :)

-, , .

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

:

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

.

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

.

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

, . ( ) " " . :

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 (received) or written (set). Now you can 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 ours MyCustomClassand populate 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 whatever it wants. I just gave an example. Hope this makes sense!

0
source

Source: https://habr.com/ru/post/1542568/


All Articles