0

I have a function that returns an object:

public PathDetailsMessage GetDataFromDatabase()
{
    PathDetailsMessage pathDetailsMessage = new PathDetailsMessage();
    pathDetailsMessage.MessageTypeVersion = "5.3";

    PlannedJourneyLocation plannedJourneyLocation = new PlannedJourneyLocation();
    plannedJourneyLocation.CountryCodeISO.Value = "RO";

    return pathDetailsMessage;
}

I have two public partial classes: PathDetailsMessage and PlannedJourneyLocation.

To test, I have the following code:

static void Main(string[] args)
{
    PathDetailsBLL train = new PathDetailsBLL();
    PathDetailsMessage pdm = train.GetDataFromDatabase();
}

Using Console.Write(), I can see the value of MessageTypeVersion but I can't see the value from object plannedJourneyLocation.

Can you help me?

Thanks.

littleO
  • 33
  • 6
  • 1
    You can create an additional class that has `PathDetailsMessage` and `PlannedJourneyLocation` as properties. – Qwertyluk Apr 21 '21 at 11:10
  • 1
    Also if it is a class object you can put in the parameter list. Any changes to a class object in the parameter list also get changed in the parent method. – jdweng Apr 21 '21 at 11:22

3 Answers3

2

You can return a tuple depending on your version of C# (7.0):

public (PathDetailsMessage Message,PlannedJourneyLocation Location) GetDataFromDatabase()
{
    PathDetailsMessage pathDetailsMessage = new PathDetailsMessage();
    pathDetailsMessage.MessageTypeVersion = "5.3";

    PlannedJourneyLocation plannedJourneyLocation = new PlannedJourneyLocation();
    plannedJourneyLocation.CountryCodeISO.Value = "RO";

    return (pathDetailsMessage, plannedJourneyLocation);
}

You can then call this from you Main:

static void Main(string[] args)
{
    var data = GetDataFromDatabase();
    PathDetailsMessage pdm = data.Message;
    plannedJourneyLocation journey = data.Location;
}

Aage
  • 5,932
  • 2
  • 32
  • 57
1

You can create a object that includes that 2 objects mentioned.

    public class MyObject
{
   public PathDetailsMessage PathDetailsMessage {get;set;}
   public PlannedJourneyLocation PlannedJourneyLocation {get; set;}
}

Then you have to modify the method :

public MyObject GetDataFromDatabase()
{    
    MyObject myObject = new MyObject();

    myObject.PathDetailsMessage = = new PathDetailsMessage();
    myObject.PathDetailsMessage.MessageTypeVersion = "5.3";

    myObject.PlannedJourneyLocation = new PlannedJourneyLocation();
    myObject.PlannedJourneyLocation.CountryCodeISO.Value = "RO";

    return MyObject;
}

Then in main:

static void Main(string[] args)
{
    PathDetailsBLL train = new PathDetailsBLL();
    MyObject pdm = train.GetDataFromDatabase();
    Console.WriteLine(pdm.PathDetailsMessage.MessageTypeVersion);
    Console.WriteLine(pdm.PlannedJourneyLocation.CountryCodeISO.Value); 
}

Don`t forget to rename "MyObject" to your object ;)

0

You can use OUT params:

public void GetTwoObjects(out Object object1,out Object object2)
{
    object1 = new object();
    object2 = new object();
}

static void Main(string[] args)
{
    Object object1 = null;
    Object object2 = null;
    GetTwoObjects(out object1, out object2);
}
Freddy
  • 75
  • 7