C# Constructors with similar arguments

Recommended Posts

I am working on a script that takes 3-4 inputs and creates a line formula from this. The formula is then used in line calculations, like collisions. The problem is the line formula has 4 versions that allows you to use 3 floats to construct it. These constructors are conflicting with each other.

I found some old code of mine where I did the same thing but with single integer inputs. So I copied my code and it is working but I don't know if what I am doing is correct.Debugging proved it works, Code:

Spoiler


//This is the class that is used for a line formula
public class LineFormula {
// The formula is Y = Slope*X + B where B is the Y Intersect
public float Y,Slope,X,B;

//A empty one can also be made
private LineFormula()
{
//Empty class only for constructing inside  constructor
}

//When we make the formula we can store all the data
public LineFormula(float InX,float InY,float InSlope, float InB)
{
Y = InY;
Slope = InSlope;
X = InX;
B = InB;
}
//Find Y on construct
//Y = Slope*X + B // 66 = (30*2) + 6
public static LineFormula FindY(float InX,float InSlope, float InB)
{
var LineFormula = new LineFormula();
LineFormula.Y = (InSlope*InX) + InB;
LineFormula.Slope = InSlope;
LineFormula.X = InX;
LineFormula.B = InB;

return LineFormula;
}
//Find X on construct
// X = (Y-B) / Slope; // 2 = (66-6) /30
public static LineFormula FindX(float InY,float InSlope, float InB)
{
var LineFormula = new LineFormula();
LineFormula.Y = InY;
LineFormula.Slope = InSlope;
LineFormula.X = (InY - InB) / InSlope;
LineFormula.B = InB;

return LineFormula;
}
//Find slope on construct
//Slope = (Y-B)/X 	//30 = (66 -6) /2   // 1 = (1-0)/1
public static LineFormula FindSlope(float InX,float InY, float InB)
{
var LineFormula = new LineFormula();
LineFormula.Y = InY ;
LineFormula.Slope = (InY - InB) / InX;
LineFormula.X = InX;
LineFormula.B = InB;

return LineFormula;
}

//Find Y intersect(B) on construct, doesn't look like I need this one.
//B = Y - (Slope*X) //6 = 66 - (30*2)
public static LineFormula FindB(float InX,float InY, float InSlope)
{
var LineFormula = new LineFormula();
LineFormula.Y = InY ;
LineFormula.Slope = InSlope;
LineFormula.X = InX;
LineFormula.B = InY - (InSlope * InX);

return LineFormula;
}
}

I don't understand my own code here:

var LineFormula = new LineFormula(); This tells me I made a new empty version of the class, inside of the class so there is 2 now? If there is two then what happens to one after return, is it waiting for garbage collection now? Why the static?

Is this even a valid way to create similar arguments? Is there better ways? I checked online and found programmers using interfaces to make similar arguments.

I think it made sense to me back then, but I have forgotten it as I never needed to use it again.

Share on other sites
1 hour ago, Scouting Ninja said:

This tells me I made a new empty version of the class, inside of the class so there is 2 now?

No - you only call 'new LineFormula' inside static functions, which are basically much the same as global functions in that they don't require an instance of the class, except they exist within the class's scope. i.e. LineFormula.FindY doesn't require an instance of LineFormula to run, but it does return a new instance when it completes.

This is pretty much just 3 'factory' functions, which is a common pattern for constructing objects that have complex parameters and therefore constructors alone may not suffice.

Share on other sites
1 hour ago, Kylotan said:

No - you only call 'new LineFormula' inside static functions... LineFormula.FindY doesn't require an instance of LineFormula to run

Thanks, this alone clears up a lot of my concerns with using this. So now I know I am not making a extra copy and the static makes it similar to a global function.

1 hour ago, Kylotan said:

This is pretty much just 3 'factory' functions

What is meant by "Factory" functions? I see this term a lot while researching how to make similar arguments. Please in layman terms.

Sorry if my questions seam obvious, I learned coding in a weird way where I know advance stuff but am missing some of the basics.

Share on other sites
Posted (edited)

factory functions make things. Think: factory. Like China Factory.

Edited by h8CplusplusGuru

Share on other sites
Posted (edited)
8 hours ago, Scouting Ninja said:

The problem is the line formula has 4 versions that allows you to use 3 floats to construct it. These constructors are conflicting with each other.

Assuming I understand this correctly, you would't want it as different constructors anyway. When constructing such an object, it would be highly ambiguous anyway! I suppose your old code fixes this with having these as the functions you've presented here, which is a much better approach: calling FindX shows me that what it is calculating is X from the given parameters.

As for yoru code, most has been explained already: you've got several factory functions (i.e. those functions create an instance based on the provided arguments and do require an instance of the class to call them on). That being said the comments at the start of the first constructor are very confusing

8 hours ago, Scouting Ninja said:

//A empty one can also be made private LineFormula() { //Empty class only for constructing inside constructor

An empty one can be made, but obviously only from inside the class as it's private. The second comment should probably read 'Empty constructor for constructing inside class', although I'd find 'Zero parameter constructor' or something alike a better wording. I'm not sure if you've added these for this post or if these were in code already, but either way, it's really quite redundant.

6 hours ago, Scouting Ninja said:

static makes it similar to a global function.

In c# there's no actual notion of global functions, making static more or less the replacement. They're still functions of a class, but they do not run on an instance; in fact iirc they're not allowed to either. So you can't do

class Foo
{
static void Bar();
}

...

Foo x;
x.Bar();

And have to call Foo::Bar explicitly.

Code looks correct other than that, but it's more of a calculation result than a LineFormula. In all honesty, having the class be returned is already quite redundant: these functions only require you to return a single float, all the other information is what you passed into the function already

Edited by AthosVG

Share on other sites
8 minutes ago, AthosVG said:

Assuming I understand this correctly, you would't want it as different constructors anyway.

The thing is I do want it as a constructor for use. These line formulas create theoretical infinity lines, that is what the formula is for.

So once a line is made I want to store it for use but to never change it again, every line instance will then be a line in the world and can only be read from or destroyed. Since this posts the variables are private with get functions.

12 minutes ago, AthosVG said:

calling FindX shows me that what it is calculating is X from the given para﻿meters.

I agree now that I have been using it this looks cleaner, I know what part is calculated where if it was only constructs it would have been impossible to know what I was calculating.

13 minutes ago, AthosVG said:

I'm not sure if you've added these for this post or if these were in code already

I am not a programmer, so I need to keep notes of what every little thing does or I could delete or change a important part of a script. All of my code has these redundant text that describes each and every step.

I could work on a piece of code today, get it working, then next week I have no idea what it does or how.

18 minutes ago, AthosVG said:

In c# there's no actual notion of global functions, making static more or less the replacement.

Thanks for explaining this to me, makes things a little clearer.

Create an account

Register a new account

1. 1
2. 2
3. 3
4. 4
Rutin
15
5. 5

• 14
• 9
• 10
• 12
• 17
• Forum Statistics

• Total Topics
632910
• Total Posts
3009174
• Who's Online (See full list)

There are no registered users currently online

×