I am new to C# and I am working on classes. What is the best way to learn classes so that a person can understand it.
I do understand to get a class you go up to the bar and click on add new item and then selected class. You can name that class such as AddValue.cs so that way that is what your class is being called.
I am trying to understand this I am working with number like a calculator.
I made my class and it is called CalculateValue.cs. In this I need need to create 2 private variables, _num1 and _num2.
Which I wrote
int _num1;
int _num2;
I wrote that under the class name. But my whole problem is I don’t really know how to code for the class. I am trying to understand it but its not working. What are some ways that help you understand how to code using classes?
It’s not the kind of question SO like you asking here to be honest taylor. And to be fair – a book probably is your best solution anyway. There must be hundreds of books out there (many free) that will acquaint you with principles of OOP. I’ll have a look for a link to a few in a minute if I get a chance.
As a sort of attempt to answer your question – I feel like you’ve got two main issues here – you’re not exactly familiar with OOP principles, and you’re stuck on how to use the IDE (visual studio). In which case, I’d advise you buy a book which deals with teaching OOP principles for a particular language so that the use of the IDE is also covered. As you’ve mentioned C#, go for that one. C# for dummies or something?
Think of OOP almost as an approach to programming. It’s, amongst many other things, a way of implementing encapsulation. It’s important for us to be organised when developing, very organised, and encapsulation is a way of you also organising your code. I don’t mean just for organisation purposes (i.e. to keep it all clean and tidy), but to tackle many other issues aswell.
What you’ve just done there (with your two int variables), is created two private properties of your class. Think of those almost as attributes to your class, just as height is to person, it’s a property. Another type of member can be a method. A method DOES something. Say, for example, you wanted to add those two numbers together, you may have your two variables and a 3rd member (a method) called Add(). This class you’re talking about then is capable of storing two numbers, and capable of performing something you’ve called Add() (it may be to add the two numbers). I could go on about the basics of then creating and instance of your class and using it but I’d be here all day. And besides – there’s a lot more to take into consideration. For example, you’ve created PRIVATE variables, these won’t be accessible outside of the class, and depending on what you want to do with them (i.e. how/where you would like to access them) they may need some sort of public access modifier. There’s loads to learn mate, get reading. It’s worth it.