Reputation: 7601
I have read the article about multiple returns a number of times. But I cannot figure out how to apply it when the values that I am trying to get out are not be destructed into simple scalars, but rather object properties. (I can use C# V7 if needed).
Also the out keyword is not allowed when with parameters that are object properties. So how can I do this example:
Tools.orientation(ent.esr, out e.heading, out e.pitch, out e.roll);
//...
public static void orientation(EntityStateRepository esr, out double heading, out double pitch, out double roll)
{
TaitBryan topoEuler = topoToGeoc.eulerTrans(esr.worldOrienation);
heading = MathHelper.RadiansToDegrees(topoEuler.psi);
pitch = MathHelper.RadiansToDegrees(topoEuler.theta);
roll = MathHelper.RadiansToDegrees(topoEuler.phi);
}
Upvotes: 0
Views: 98
Reputation: 2452
Just for completeness sake, you mentioned multiple return types.
With C# 7 features, and the NuGet Package System.ValueTuple
you could write the following, and it would be equivalent to Igor's answer unless you are going to re-use the SomeContainer
class
public static (double heading, double pitch, double roll) orientation(EntityStateRepository esr)
{
TaitBryan topoEuler = topoToGeoc.eulerTrans(esr.worldOrienation);
var h = MathHelper.RadiansToDegrees(topoEuler.psi);
var p = MathHelper.RadiansToDegrees(topoEuler.theta);
var r = MathHelper.RadiansToDegrees(topoEuler.phi);
return (h, p, r);
}
And access with
var res = orientation(...);
DoAThing(res.heading);
DoAThing(res.pitch);
DoAThing(res.roll);
Upvotes: 2
Reputation: 62298
I think you are over thinking this. Define a new type with those properties and return an instance of that type. For example:
public static OrientationModel orientation(EntityStateRepository esr)
{
TaitBryan topoEuler = topoToGeoc.eulerTrans(esr.worldOrienation);
var container = new OrientationModel();
container.heading = MathHelper.RadiansToDegrees(topoEuler.psi);
container.pitch = MathHelper.RadiansToDegrees(topoEuler.theta);
container.roll = MathHelper.RadiansToDegrees(topoEuler.phi);
return container;
}
public sealed class OrientationModel {
public decimal heading {get;set;}
public decimal pitch {get;set;}
public decimal roll {get;set;}
}
Upvotes: 3
Reputation: 152634
You can't pass properties by reference, so your best bet is to pass in the parent object instead:
Tools.orientation(ent.esr, e);
//...
public static void orientation(EntityStateRepository esr, TypeOfE e)
{
TaitBryan topoEuler = topoToGeoc.eulerTrans(esr.worldOrienation);
e.heading = MathHelper.RadiansToDegrees(topoEuler.psi);
e.pitch = MathHelper.RadiansToDegrees(topoEuler.theta);
e.roll = MathHelper.RadiansToDegrees(topoEuler.phi);
}
Or if you want to keep the version with out
parameters and add an overload instead:
public static void orientation(EntityStateRepository esr, TypeOfE e)
{
double heading;
double pitch;
double roll;
Tools.orientation(ent.esr, out heading, out pitch, out roll);
e.heading = heading;
e.pitch = pitch;
e.roll = roll;
}
//...
public static void orientation(EntityStateRepository esr, out double heading, out double pitch, out double roll)
{
TaitBryan topoEuler = topoToGeoc.eulerTrans(esr.worldOrienation);
heading = MathHelper.RadiansToDegrees(topoEuler.psi);
pitch = MathHelper.RadiansToDegrees(topoEuler.theta);
roll = MathHelper.RadiansToDegrees(topoEuler.phi);
}
Upvotes: 2