- Home /
The question is answered, right answer was accepted
Return a custom class from function
I have a variable that is of type class. Within that class is a variable of another class type.
I need to change the values of the class within the class, and I thought I could do that by creating a function that can return the type class. But I can't seem to figure out how.
I know I could do this with a constructor, and pass in the values I need, but it would be so much nicer if you could create a function that returns the class instead.
Can you do this?
Thanks for your help,
EDIT : sorry, yea reading this back it doesn't really make a lot of sense.
so I have a couple of classes, with one of them holding the other class as a variable.
public class bezier {
public Vector3[] controlPoint;
public float length;
public boundingbox boundary;
}
public class road {
public int typeIndex;
public int[] intersection;
public bezier[] spline;
}
I then declare a new variable of type "road". I later need to change the information in spline. There's a lot of information in spline, and I was hoping I could do something like this :
example.spline = SetSplines(some inputs...);
with SetSplines being a function, that returns the data type spline.
Spline SetSplines (parameters){
....using the paramaters, add values to the spline class
return theSplineInfo
}
didn't add what Id like to do in the function, its not really relevant*
Now you cant do this, because you cant make the type of the function some custom type. The work around would be to make it a void function, and have it save to some temporary variable of type spline, and then copy the contents of this temporary variable into example.spline . But that's really messy, and I was hoping there was a workaround. :) Thanks
you need to give us some example code - i've got no idea what do you want to achieve
Answer by Bunny83 · Jul 13, 2016 at 06:01 PM
edit
Well, so lets start over. Since your "spline" variable is of type bezier[]
your SetSplines
method has to return an array of bezier, not a single bezier.
bezier[] SetSplines()
{
// create a bezier array which you return at the end
}
Since you said it doesn't matter what your method does we're done here.
so within the SomeOtherClass, GetOther() is a constructor right? like a function within the class? Is that how I could do it? Could I write : Example.Spline = GetOther(parameters...);
No, that wasn't a constructor, it was just a method that returns the reference which was stored insode the outer class. However that's pretty irrelevant as your question was misleading and my old example has no relevance here.
I've edited my answer.
yea, it does work. Thanks so much, saved the day xD
$$anonymous$$onodevelop doesnt try to autocorrect it, so i assumed you couldnt do that. Thanks, ill go try it!
Follow this Question
Related Questions
type function not returning. c# 2 Answers
Custom Documentation for Monodevelop 1 Answer
Magic Spell Types - SpellType.Fire/Water/Earth? 1 Answer
Access a class on another script? 1 Answer