• Forum
  • Lounge
  • Are data members the same as properties?

 
Are data members the same as properties?

Apr 24, 2012 at 2:47pm
Hi everyone!

It's been a while since I've posted in these forums. Been busy with my final year at University but all that said and done I've been revising and found some small (and some rather large) gaps in my knowledge.

I was wondering if Data Members and Properties are the same thing? If they arn't them what are the differences? This question isn't really language specific but just a general idea of what they are in a "programming" context.

Thanks in advance.

lnk2019
Apr 24, 2012 at 2:53pm
If a distinction between data members and properties is made, then data members are private instance or class data, whereas properties are data that is accessible through getters / setters. If you're talking about C#, that is how the words will probably be used, but in the end it depends on the person talking.
Apr 24, 2012 at 2:55pm
Thank you very much, that cleared it right up for me!
Apr 24, 2012 at 3:00pm
I'm pretty sure that C#'s properties and accessors (i.e., get and set) are just abstractions, i.e., the compiler probably turns
1
2
3
4
5
6
7
8
9
10
11
12
class MyClass {
        public int MyInt
        {
                get {  return myInt; }
                set { myInt = value; }
        }

        public void MyMethod()
        {
                this.MyInt = this.MyInt + 5;
        }
}

into
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class MyClass {
        public int GetMyInt()
        {
                return myInt;
        }

        public void SetMyInt(int value)
        {
                myInt = value;
        }

        public void MyMethod()
        {
                this.SetMyInt(this.GetMyInt() + 5);
        }
}
Last edited on Apr 24, 2012 at 3:01pm
Apr 24, 2012 at 3:12pm
I believed that's what I said.
Apr 24, 2012 at 3:18pm
I didn't see your post until after I posted mine and refreshed the page.
Apr 24, 2012 at 3:24pm
Oh I thought what I said was ambiguous somehow.
Apr 24, 2012 at 3:27pm
Nope, it's good.
Apr 24, 2012 at 3:46pm
Thanks to the both of you! :D
Apr 24, 2012 at 5:15pm
12
13
14
15
        public void MyMethod()
        {
                this.SetMyInt(this.GetMyInt() + 5);
        }
That's stupid, why would the compiler generate less efficient code by throwing in function calls from nowhere?
Apr 24, 2012 at 5:27pm
It wouldn't, the idea was to demonstrate that they're semantically equivalent.

[edit] I guess "the compiler turns ... into ..." was the wrong way of putting it.
Last edited on Apr 24, 2012 at 5:27pm
Apr 24, 2012 at 5:30pm
Thanks, just clearing that up.
Topic archived. No new replies allowed.