p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   BOOK: Beginning Object-Oriented Programming with C# (http://p2p.wrox.com/book-beginning-object-oriented-programming-c-721/)
-   -   Understanding c# Object orientated program syntax (http://p2p.wrox.com/book-beginning-object-oriented-programming-c/94730-understanding-c-object-orientated-program-syntax.html)

TheOnlyPaulV July 19th, 2015 05:06 PM

Understanding c# Object orientated program syntax
Hi every one, the book suggested that I should come here if I struggle so here I am!
Firstly I am already a coder at VB and Oracle SQL.
I understand and use Object orientated programming but I never formally picked it up, I just learned as I went.
The idea is to pick up C# but at the same time formalise my knowledge of OOP theory; to learn what I am actually doing instead of just knowing why I should.
I know im head over heels and backward (know and LOVE linq, but unsure of real OOP theory?) but its just the way my knowledge has developed.
So, hope some one can help!
under chapter 2, understanding objects, the author states that a statement should be written as 'clsPerson myFriend' then 'myFriend = new clsPerson();'.
so step by step with the authors code.
Firstly the author created a pointer called 'myFriend' which will point to an instance of the class clsPerson.
Now, what confuses is that in all programming languages I have used thus far, you follow the syntax 'varName var Type', this is what im used to.
Dim myString as string
myString varchar2(255)
etc etc etc.
Its probably my inexperience with C#, but why is it swapped in this case? why is the varType first, then the varName second? Are we saying 'create a generic pointer, which will point to a clsPerson type? Therefore would 'string myString' -in theory- be valid as its a pointer?
I am struggling to wrap my head around this requirement.
Websites suggest writing the line combined as 'clsPerson myFriend = new clsPerson' but tht is no clearer even though I know it does the same thing.
by my understanding, when I create a 'myString varchar(255)' I am saying 'reserve a block of ram, the size and type of a char char, 256 long. when ever I refer to 'myString' I want you to refer to that varchar memory'.
by that same logic, in the above line I am saying 'a type of clsPerson, called myFriend, reserve memory for a clsPerson' and its that which breaks me down .
I hope I make sense.
P.s. The cookie cutter analogy, whilst good is maybe confusing me a little as its saying, to me, make a copy of the cookie cutter, then copy it again, then use the copy to punch out a space of ram and call it my friend. why make a copy of a copy?

Okay thinking about it a lot more, im starting to get a handle on it a little more now.
Ive gone about it the wrong way round. When you use the 'new' keyword and declare a class, it creates the memory space, but throws back a memory location. without some where to store the memory location it will fail. So we give it a place to store the memory location, myFriend. therefore we have to create that pointer first, which we do via 'clsPerson myFriend'.
The end result is that calling 'myFriend' calls the memory location created by the 'new' keyword earlier.
Still don't understand why the varType comes before the varName however or is this combination just the C# syntax for a pointer?

DrPurdum July 27th, 2015 08:50 PM

Understanding C# Syntax
C# is not Visual Basic and its syntax is very different. The language is modeled after C, not any dialect of Basic. Defining a variable in C# is of the form:

Data_Type Variable_Name;

You keep complaining that it's different that VB. True, it is, but why are you complaining about it? If you want to code in VB, code in VB. If you want to code in C#, learn its syntax and quit complaining that it's different. Complaining is a complete waste of time.

As to the Cookie Cutter example, it is used to drive home the idea that a class is a model, or blueprint, for something. A set of blueprints is not a house, just like a class is not an object. To create an object, you use the set of plans dictated by the specifics detailed in the class code, starting with the constructor. The process of moving from a class specification to an object of that class is called object instantiation. The properties of the class determine the final "shape" the object has.

All times are GMT -4. The time now is 10:09 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.