i am trying to recreate some thing here but i am not able to understand a part of the code. the class has a constructor like so:
public polygon(vector2 hull, vector2 holes)
{
// do something…
}
then it has another function that says:
public polygon(vector2 hull) : this(hull, new vector2[0])
{
// nothing here, its blank…
}
can someone please explain the second function. what it does and how it does it. or maybe simplify it?
Also, what does the Vector2[0] mean?
Please use code tags in future.
Constructors can call other constructors, or the constructor of the class they’re derived from, either by using : base()
for the base class, or : this()
for the same class. For example:
public class BaseClass
{
public BaseClass(int someInt)
{
SomeInt = someInt;
}
public int SomeInt { get; set; }
}
public class DervivedClass : BaseClass
{
public DervivedClass(float someFloat, int someInt) : base(someInt) //calls the base class constructor
{
SomeFloat = someFloat;
}
public DervivedClass(float someFloat) : this(someFloat, 10) //calls construtor on same class
{
}
public float SomeFloat { get; set; }
}
Consider it a way to reuse code rather than have a bunch of constructors that all do the same slightly different thing.
What the second constructor is doing is calling another constructor in the same class, and supplying some hard coded information into the parameters. new Vector2[0][ ]
is just a new instance of a jagged array.
2 Likes
By jagged array, do you mean a 2D array?
No, a jagged array in this case Vector2[0][ ]
would be a 1 dimensional array of 1 dimensional arrays.
Then how can the X size be 1 and the Y size not defined?
For an array to have at least 1 element it must have a Y size of 1, right?
The Y size is determined by whatever you pass in to the method as a parameter.
The size does not need to be explicitly defined in the method definition because you may pass arrays of different sizes to the same method.
But when I call the derived constructor, I just pass 1 parameter because it takes inly one parameter and its not a jagged array.
I am just the hull array as a parameter
In C# it’s acceptable to declare an array without an explicit size. In that case it starts as null.
For example, I can declare an array like this:
// int array of indeterminate size
int [ ] intArray;
intArray is just null at this point so it doesn’t have a size.
If I wanted to actually use the array, though, I would need to assign an actual instance of an array, and the array instance will have a specific size such as
intArray = new int[6];
So in this case
public polygon(vector2[ ] hull) : this(hull, new vector2[0][ ])
{
// nothing here, its blank...
}
It looks like this constructor is calling the other constructor with hull as the first parameter and the second parameter is an array of length 0, containing 0 instances of a null-value array of vector2. Basically, it’s calling the constructor with the hull and an array of holes but there are no holes, so it’s just sending an array with the length of 0.
I’m guessing the reason that they pass in new vector2[0] for holes instead of vector2[ ][ ] is because somewhere in the code it probably has something like
if (holes.Count()>0)
or maybe
for (int i =0; i < holes.Count(); i++)
So in that case vector2[0][ ] would work but vector2[ ][ ] would give a null reference exception.
1 Like
Oh okay. Now I understand. It was a really good explanation, thank you very much!