Hi guys, I'm currently learning C#. I have a couple questions.
What are class properties and how are they used?
What are instance variables?
I've read some things about both, but can't seem to grasp the concept. Can anyone explain them and give an example or an analogy?
Beginner at C#... questions
Started by Programmr, Jan 26 2009 08:20 PM
4 replies to this topic
#1
Posted 26 January 2009 - 08:20 PM
|
|
|
#2
Posted 27 January 2009 - 03:37 AM
Instance variables are variables in a class that each object (instance of said class) has a copy of.
So:
I've created two instances of the person class. So I have two person objects. bob has a variable sName and a variable nAge. Then the joe object has a separate sName and nAge variables.
So if I printed them with:
The output would be:
This allows your objects to have similar properties with different values. :)
Then a class property is a variable that only one copy of the variable exists (independent of how many instances of that class there are). I can't seem to come up with an example of this though.
So:
class person {
String sName;
int nAge;
person(String sName, int nAge) {
this.sName = sName;
this.nAge = nAge;
}
}
person bob = new person("bob",16);
person joe = new person("joe",27);
I've created two instances of the person class. So I have two person objects. bob has a variable sName and a variable nAge. Then the joe object has a separate sName and nAge variables.
So if I printed them with:
System.out.println(bob.sName + " " + bob.nAge); System.out.println(joe.sName + " " + joe.nAge);
The output would be:
Quote
bob 16
joe 27
joe 27
This allows your objects to have similar properties with different values. :)
Then a class property is a variable that only one copy of the variable exists (independent of how many instances of that class there are). I can't seem to come up with an example of this though.
#3
Posted 27 January 2009 - 08:06 PM
Ok! awesome I get it. Thanks for the tips. I really appreciate it. I'm sure I'll have more questions in the future, but I'll try not to post too many haha.
#4
Posted 28 January 2009 - 03:14 AM
Good! :)
No worries, if you have any questions post them! It's what CC is for. ;)
No worries, if you have any questions post them! It's what CC is for. ;)
#5
Posted 28 January 2009 - 12:13 PM
Wow... that's great help chili5! I repped you for that... nice seeing you around helping people :D


Sign In
Create Account

Back to top









