Click here to Skip to main content
15,880,651 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I've been watching tutorials on code and I have a question about changing variables.

I worked through the following example:

class Person {

var Name:String = "Initial Name"
var Age:Int = 24
var Other:String = "Other Variable"
var Decimal:Double = 4.1399

init () {
self.sayCheese()
}

func sayCheese() {
print("Cheese")
}
}


var firstPerson = Person()

firstPerson.Name = "Jones"
firstPerson.Age
firstPerson.Other
firstPerson.Decimal
firstPerson.Name



Towards the end of the code, I change the .Name string to equal "Jones", as opposed to "Initial Name". What is the point in changing the string of Name later in the code when I could go back and change the initial value? Why is it necessary to write an additional line of code to change a name and isn't this confusing?

Sorry for the ignorance of the question, but I'm new to coding and this has got me stuck! I'm sure there's a very good reason for it!

Thanks a lot!

What I have tried:

I've tried adjusting variables and adding additional values but I still can't understand the reason.
Posted
Updated 13-Jun-16 20:56pm

1 solution

Because the initial values apply to every instance of a Person, not just to the one you are talking about.
If you change the initial value to "Jones" then it would work fine - until you tried to add a second person "Smith".
The initial value is a "default placeholder" and is created as part of the "setup" of a Person rather than as part of an individual human being. It doesn't create anything that relates to any individual, it just sets up the framework for an individual to be recorded in.
When you do this:
C#
var firstPerson = Person()
You are creating an indiviidual, and you then fill in his details:
C#
var firstPerson = Person()
firstPerson.Name = "Jones"
You can also create a different person:
C#
var firstPerson = Person()
firstPerson.Name = "Jones"
var secondPerson = Person()
firstPerson.Name = "Smith"
Andthe two names are independant of each other, as you would expect in the real world.

If you assign names by creating a new variable (firstPerson, secondPerson etc.) why do you need to put .Name at all? Why can't you just write:

firstPerson = "Jones"
secondPerson = "Smith"



You could...but then the name wouldn't have any related info which is held in the Person class: Age, Address, JobTitle, or whatever.
Let's just think about cars for a moment: Here is a car, it's a Red Mercedes. It's My Car. Over there is a different car: a Blue Ford. That is Your Car.
They two are not the same object - if you put your mobile in the glove box of Your Car you don't expect to find it in the glove box of My Car - but they have a lot of info associated with them: engine size, fuel type, number of doors, model, age, registration number, VIN number, ... the list goes on. To represent that in a computer, you could just have separate variables for each attribute, but then you'd need a couple of dozen variables for My Car, and a separate couple of dozen for Your Car - and if we introduce That Car, This Car, His Car, Joes Car if makes the code very unwieldy.
So we create a Car class which holds all the related information about a single vehicle and fill in the information for each car to keep it all together (this is called an instance of the Car class to separate it from the definition of the class)
Now when we talk about My Car we can look at the class instance and we can get the Colour, engine size, left or right hand drive, wheel size, or whatever we need at that point for that one specific vehicle.
The other thing we can do is play around with it: we don't need to know whose car it is in order to do things with it. What I mean by that is that the name of the variable (in your case firstPerson) has nothing to do with the instance of the class that it contains - this is stuff your tutor will bring up later, but because the variable isn't the instance but just a reference to it you can pass the reference to a function which doesn't need to know where the instance came from, it just processes any instance.
That's probably not clear at the moment - and I don't want to tread on your tutors toes too much here - but if we go back to cars for a moment, MyCar is a variable which refers to a specific vehicle. But MyWifesCar is another variable which refers to the same vehicle, as is ThisCar, and OurCar - the individual names aren't the object itself because if something changes in the actual vehicle (I drive it into a tree perhaps) then the damage is reflected in the vehicle, regardless of which variable I use to access it: MyCar, ThisCar, OurCar, MyWifesCar all refer to the same instance of a vehicle.
You can't do that with separate variables! :laugh:

Does that make any sense?
 
Share this answer
 
v2
Comments
Member 12582260 14-Jun-16 2:57am    
That makes a lot more sense, but if I could ask one more stupid question...

If you assign names by creating a new variable (firstPerson, secondPerson etc.) why do you need to put .Name at all? Why can't you just write:

firstPerson = "Jones"
secondPerson = "Smith"

Thanks!
OriginalGriff 14-Jun-16 3:29am    
Answer updated

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900