I’m still learning, so please excuse if this is a dumb question.
If I have a class called SimpleVec2, which is a simplified Vector2 with just x and y, and basic methods,and…
In a different class, If i have a field, SimpleVec2 _position, and I need a getter, which obviously just returns _position, and a setter, which needs to set _position.x and _position.y…
Is there some way of doing something like:
public Position
{
get => _position;
set(int x, int y)
{
this._position.x = x;
this._position.y = y;
}
}
Edited: Clarified that Position is in a different class.
You could make the setter private and add another method to the class that takes coordinates. By that, I mean something like this:
The set
takes the same type as the property. Using your example:
The value
you pass to the setter is a SimpleVec2 object, not a pair of int
values.
But don’t just copy and paste that property code… it’s equivalent to public SimpleVec2 Position { get; set; }
, which is much easier to write and maintain.
If you want to set the values within the SimpleVec2 at the same time as setting the Position value, all in a single line, the caller should use the syntax: thingy.Position = new SimpleVec2() { x = whatever, y = otherWhatever };
This instantiates the SimpleVec2 object, setting its x and y properties, then sets the Position property to the finished SimpleVec2.
Thanks, very useful. Merry Christmas.
While both of the answers by u/VGPowerlord and u/UninformedPleb are correct as far as they go, conceptually, your class doesn’t make any sense.
You’re suggesting (based on your original question and the responses you’ve made elsewhere) that you have
What would this even mean to the calling code? (all the examples I’ve seen in this thread exhibit similar issues to the above example… just moving the goalpost a little bit for each different example)
Okay, how do we initialize this or set some values? Position
is still null
.
um… that doesn’t help. vector.Position
has a value but we still can’t see some x
or y
data.
This kind of thing continues indefinitely because you’re nesting the class and property inside itself.
You say you’re creating your own version of a vector. Typically a vector is a value type represented by a struct–not a class–that you likely want to be immutable. We’ll avoid that little bit today.
The Position
property you had created before doesn’t need to exist. The vector itself IS the position. And this position has an X
component and a Y
component.
You can add other properties to the class if you need them. You can also add methods to the class such as Add
like so:
There are a LOT of other ways to do this kind of thing. That Add method could be static and could look like something like this:
Many, many ways to do the same thing. If you’re unsure of how the language is supposed to be used, read the documentation on: class, struct, properties, methods, static, initializers, etc.
The problem with your example is that Position isn’t a member of SimpleVec2. It’s a member of a different class and is of type SimpleVec2.
either set both properties separately or create
public void SetPosition(int x, int y){…}
Better question–why are you altering the state of this object with values from some other object.
C# devs
null reference exceptions