[C#] couldn't I reduce this to one statment i dont see a purpose of putting ": this" someone explain it to me please http://pastebin.com/XEaaFLwi
I'm not super-familiar with C#, so I don't know if it supports default arguments. This particular bit of code is basically chaining a constructor with fewer parameters to one with more parameters, passing in defaults for the missing arguments.
i just don't see the purpose of passing the argument
What do you mean? It gives you two constructors, one that has defaults for certain parameters that the other one requires to be passed in. Not 100% sure what you're asking. Basically, what alternative do you think makes more sense?
i guess i just don't understand what its doing to the variables this.position = position is basically equaling itself, i believe "this" is a shortcut for not using the type of variable (int, vector, etc, etc) I guess this will be used to update each variable as the animation goes
Ah, got it. No, that's not what's happening. Inside the constructor, just saying “position” refers to the passed argument named “position”. “this.position” refers to the instance variable in the Sprite class. So what's happening there is it's taking the passed in value for the position and putting it in the instance variable, so that future calls to methods on that sprite will be able to access it. Because the position parameter is named the same as the instance variable, you have to use this. to refer to the instance variable. Does that make sense?
so its kinda like passing by reference
Mostly unrelated, though many of the values there are in fact passed by reference. I'm kind of at a disadvantage here because I don't know C# syntax too well. The idea is, a given Sprite doesn't have a position until you put a value in its position instance variable. When you invoke the constructor with that position parameter set, the instance variable still has no value. The line “this.position = position” puts the value that was passed into the constructor into the instance variable, so that the Sprite now has a proper position.
ah I see, so when when the constructor gets called this will automatically define the instance variables only when there is stuff to update it with thanks bud
Sounds about right :)
This is also what happens when you use the same variable names with different scope- i'm not sure it can be avoided in this case, but many times people do this when it CAN be avoided. I like to use _ for parameter vars.
A decent point. For constructor parameters, I'm actually partial to the approach taken here. Scala attends to the problem by making constructor-declared variables instance variables by default. Works pretty well.
I was just looking at a codeacademy tutorial for objects in javascript and saw this again (having seen in way back when in java) - in looking at my response I'm not so sure i really helped answer it. When you have this code: this.textureImage = textureImage; You are assigning the local (object) variable the same value as the parameter/argument that was given to it using the same name. If you coded textureImage = textureImage it obviously wouldn't do anything, so this says, "make the texture image of THIS object equal to the texture image that was passed in as an argument." HTH d.i.
Join our real-time social learning platform and learn together with your friends!