Reputation: 17
i have created a very simple vector - axis class in c#.
Typically it's working like MyAxis abc = new MyAxis(p,x,y,z);
(p point) and (x,y,z doubles);
However i want to call the constructor my own way like for example
MyAxis abc = new MyAxis([0 0 0],[0 0 1]); // Looks like MatLab;
I know i can do this with string manipulation or lists or whatever but i want to avoid to create new objects and pass them to the constructor.
I want also to avoid something like ..new MyAxis(a,b,c,d,e,f)
or ...new MyAxis(new Point(x,y,z),...);
Is there such possibility in C# ?
Upvotes: 0
Views: 88
Reputation:
You could try something like this:
MyAxis abc = new MyAxis(new[] { 0, 0, 0 }, new[] { 0.1, 0.2, 1.1} );
And then in MyAxis's constructor just do something like this:
public MyAxis(new int[] points, new double[] doubles)
{
Point p = new Point(points[0], points[1], points[2]);
double x = doubles[0],
y = doubles[1],
z = doubles[2];
...
}
You also can create multiple constructors if you want to.
public MyAxis(int a, int b, int c, int d, int e, int f)
{
}
public MyAxis(Point a, Point b)
{
}
public MyAxis(Point p, double[] doubles)
{
}
etc.
Unfortunately there is no way to create custom syntax :(
Upvotes: 1
Reputation: 17365
How about having your constructor to look like MyAxis(double[] p, double[] v)
You can then initialize your object like this:
MyAxis abc = new MyAxis({0, 0, 0},{0, 0, 1}); // Looks _almost_ like MatLab;
Your constructor should obviously validate that the arrays contain 3 elemnts (or at least that the number of elements is equal and support N dimentional vectors)
Upvotes: 1
Reputation: 8804
Not much you can do with this, unless you pass in an array of doubles / ints
new MyAxis(new int[]{1, 2, 3},new int[]{1, 2, 3});
or like you said a new object like new Point (x, y, z) (I know you said you wanted to avoid this, but working with an OO language, it does "work")
new MyAxis (new point(1, 2, 3), new point(5, 6, 7));
Upvotes: 1
Reputation: 3485
Would that work for you ?
public someClass( int[] input1, int[] input2 )
someClass temp = new someClass(new int[] { 1, 0, 1 }, new int[] { 1, 0, 1 });
Upvotes: 0
Reputation: 2554
You're looking to change the syntax that C# uses, but that isn't possible. I believe the closest you'll get is something like
MyAxis abc = new MyAxis(new []{0,0,0}, new[]{0,0,1});
But that isn't quite what you're looking for. I think that's the best you'll get, though.
Upvotes: 1