Jump to content

Beginner at C#... questions

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Programmr

Programmr

    Newbie

  • Members
  • Pip
  • 2 posts
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?

#2
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Instance variables are variables in a class that each object (instance of said class) has a copy of.

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

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
Programmr

Programmr

    Newbie

  • Members
  • Pip
  • 2 posts
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
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Good! :)

No worries, if you have any questions post them! It's what CC is for. ;)

#5
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
Wow... that's great help chili5! I repped you for that... nice seeing you around helping people :D