I am in Visual Studio but this code should be easy to consume. In the first example i can access clearInfo() no problem but why can't i access clearInfo() in the second example? i would appreciate answers. Code (CSharp): namespace ConsoleApp.day1.Example { struct demo { public int maxHitPoints; public demo(int hp) { maxHitPoints = hp; } public void clearInfo() { maxHitPoints = 0; } } class ExampleOne { static void Main() { demo dem1 = new demo(20); dem1.clearInfo(); Console.WriteLine("The max hitpoint is {0}",dem1.maxHitPoints); } } } Code (CSharp): namespace ConsoleApp.day1.Example { struct demo { public int maxHitPoints; public demo(int hp) { maxHitPoints = hp; } public void clearInfo() { maxHitPoints = 0; } } class ExampleOne { static void Main() { demo dem1; dem1.clearInfo(); Console.WriteLine("The max hitpoint is {0}",dem1.maxHitPoints); } } }
Because in the second example, dem1 is hasn't been initialized yet, and ClearInfo() isn't a static function- it needs an actual object to call the function.
The variable is like a parking spot. Just because the spot exists doesn't mean you've parked a car in there yet, and you can't call OpenDoor() on an empty parking spot. EDIT: Misunderstood what you meant by "variable" there maybe as I thought you meant dem1 as a whole. No, if the struct hasn't been created yet, then you can't use the members (variables) or methods that are "supposed" to exist in it- it's just a big empty space where an object might go later.
I thought the same thing at first but i tried this in visual studio earlier and it worked. It can access the variables but for some reason i can't access the function clearInfo() Code (CSharp): namespace ConsoleApp.day1.Example { struct demo { public int maxHitPoints; public demo(int hp) { maxHitPoints = hp; } public void clearInfo() { maxHitPoints = 0; } } class ExampleOne { static void Main() { demo dem1; dem1.maxHitPoints = 15; Console.WriteLine("The max hitpoint is {0}",dem1.maxHitPoints); } } }
Is there some particular reason why you're seeming to try and avoid initializing it like normal? Just say "demo dem1 = new demo();" Also, how are you figuring out that you can't access clearInfo()? Is there an error or are you basing that on the value output by maxHitPoints?
The value output to the console shows maxHitPoints as 15 in the script above. There is not a particular as to why i'm doing this. I am just interested in this occurrence. Yes there would be an error if i were to access clearInfo() without creating an object i would get the error of use of unassigned local variable dem1 but that is not the case when i access a variable in dem1.
Okay, so apparently structs work a little differently than I thought. Even if you don't specify "new", it will auto-initialize it to a default state using the implicit parameterless constructor. The problem is that until all fields have been assigned, you can't access properties or methods for the struct in question. That means that just by assigning maxHitPoints = 0, you should then be able to call the function like normal. Very interesting.
I just watched a vid on this very interesting, and you can actually call a function in a struct after all the variables have been initialized.
Sorry for the barrage of replies but i just want to make something clear. The "new" keyword does not make "something"(for lack of a better word) an object. What the new keyword does: it allocates memory on the heap (this only applies to reference types) constructors are invoked immediately after memory is allocated if you do not provide a constructor for your object or the constructor is a private one. C# compiler will create a default constructor which sets member values to their defaults(zeros everything out so stuff is not null) constructors that take no paramaters are called default-constructors. You can make your own default constructors. This wont work with constructors of a struct because they cannot be parameter-less unless it's a default one made by the compiler When it comes to structs you have to use the new keyword if you want to utilize it's automatic default constructor but please note a default constructor takes no parameters whatsoever Value types: value types such as structs and primitive types are created on the stack so the "new" keyword is not necessary unless you want to initialize members of a struct in a constructor or set the member values to their defaults all value types have a default value https://msdn.microsoft.com/en-us/library/83fhsxwc.aspx The following will produce an error: Code (CSharp): int i; Console.WriteLine(i); To fix this you do: Code (CSharp): int i = new int(); Console.WriteLine(i); This is a great example as to why you would use the new keyword albeit unecessary.
That's exactly what I said. You even quoted me saying it in this very post. *shakes head* You really need to just use the "new" keyword anyways- it's like not putting "f" on your float values in the few instances you can get away with not doing so. There's no real point in not doing it, and it'll get you in trouble at some point if you make a habit of flaunting the exceptions. Indeed.
"dem1 is hasn't been initialized yet, and ClearInfo() isn't a static function- it needs an actual object to call the function" Before you were implying you have to do demo dem1 = new demo() in order to be able to call the function which is false and demo dem1 is already an object. Plus you can call function clearInfo() with just demo dem1 you just have to initialize your variables before you do it. I don't know why you comment on me making a habit of not utilizing the new keyword. When i told you previously "There is not a particular as to why i'm doing this. I am just interested in this occurrence" but you should be able to recognize what is a object and what is not considered a object. At the end of the day there is no way you can say that's exactly what you said.
I quoted a post in which you were explaining how all you had to do was initialize all of the fields. You quoted me, in that same post, as having said the exact same thing, but didn't appear to notice. That was the reference that I was referring to. Calm down dude, no one's attacking you, I just thought it was amusing. Indeed, I did not know how structs were treated when this thread started- I admit that I use them only when I have to, like built-in rects and vectors. All of this has been fascinating, in a "more knowledge never hurts" kind of way, and as a way of diagnosing the numerous problems that likely occur from this unintuitive usage. Like the conditional operator though, I'm putting this in the "interesting, but never do it" category. *shrugs* Moving on.