473,323 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

May I know the difference between member and method

May I know the difference between member and method...I understand that methods are routines or functons which return values.... and members are those which stores values... Apart from ths all, is any other difference
Aug 12 '10 #1
2 1341
balabaster
797 Expert 512MB
The term "member" is a collective generic catch all name for anything that is part of a class or interface. For instance: Method, Field, Property, Constructor, Desctructor. The literal definition (according to csharp-online.net) is:

"Class element that defines a behavior or property—constructors, events, member variables, methods, properties, etc. Also known as type members."

A method is a function definition - in semantic terms a process that can be executed. i.e. DoSomething(); In loose terms, a constructor and destructor would technically fall under this definition even though semantically they're slightly different in that they're automatically initiated at class creation and class destruction time and cannot be called directly.

A property is kind of like a field, but it is in two parts, a setter and a getter. A property's setter and getter can function in different manners, but semantically they have the purpose of setting a value and retrieving the value [from a backing store which is usually a private field]. They may get more complex in what happens prior to the value being set or returned, but essentially what it boils down to is that the process of setting the data and retrieving the data is different and may have different levels of access. For instance, a property's getter may be public [can be accessed from outside the class], protected [can only be accessed by derivative classes or from within the containing class], could be private [and thus can only be accessed from within the class] without affecting how the setter is accessed. You could for instance have a public getter, but only a private setter.

A field has a single accessor, it doesn't have a setter or a getter (well, not that you have access to, underneath the hood of the programming language, it may have, it may differ from language to language). This means that whatever accessor is specified means that it can be get or set by the same objects [If it's public, it can be set or get from outside the class; protected, only by deriving classes or inside the class; private only inside the class].

A variable (I *think*, look this up to make sure I'm right, I usually use the terms variable and field interchangeably in my own life, but I think the technical definition separates them) is the same as a field, however, it's defined within a class member, i.e. inside the boundary of a property or a method declaration. A short code example to demonstrate each:

Expand|Select|Wrap|Line Numbers
  1. public class MyClass
  2. {
  3.     public int MyField; //A field - setter and getter are both public
  4.                         //it can be referenced by any code inside or
  5.                         //outside this class instance.
  6.  
  7.     private int _backingStoreForMyProperty; //A field - setter and getter are 
  8.                                             //both private.  It can only be
  9.                                             //referenced by code inside this
  10.                                             //class instance.
  11.  
  12.     public int MyProperty //A property with a separate getter/setter
  13.     { 
  14.         get //because no accessor is specified on the get, it takes the accessor
  15.             //of the property definition - in this case, public.  It is
  16.             //referenced by calling int x = MyProperty; which will get the
  17.             //value from the backing store and apply it to x.
  18.         {
  19.             return _backingStoreForMyProperty;
  20.         }
  21.         private set //can only be accessed from inside this class instance.
  22.                     //by specifying MyProperty = 2; for instance.
  23.         {
  24.             if (value <= 0) throw new exception("Must be > 0");
  25.             _backingStoreForMyProperty = value;
  26.         }
  27.     }
  28.     public MyClass() 
  29.     {
  30.         //This is a constructor, which could be called a method too...
  31.         //technically, but it's automatically called when you "new up" 
  32.         //the class, and can't be called the same way other methods can 
  33.         //be.  You can tell it's a constructor, because it has no return
  34.         //type and it takes the same name as the class.  All other methods
  35.         //excluding the destructor have a return type - even if there is no
  36.         //return type, in which case void is returned.
  37.     }
  38.     ~MyClass()
  39.     {
  40.         //The desctructor, this is called when garbage collection destroys
  41.         //your object.  It could be called a method, but you can't call it
  42.         //by conventional means.
  43.     }
  44.     public void DoSomething()
  45.     {
  46.         //A method that can be executed.  Notice how the return type is void.
  47.         //in the case the method returned something useful, you could specify
  48.         //  public int DoSomething() { /* details... */ }
  49.  
  50.         DateTime MyVal = DateTime.Now; //A variable - it's the same as a
  51.                                        //field but it's inside a method and
  52.                                        //can only be referenced by code
  53.                                        //inside this method that occurs after
  54.                                        //the variable declaration.  Code that
  55.                                        //appeared before this variable 
  56.                                        //declaration, even within this method 
  57.                                        //can't access it.
  58.     }
  59. }
All of the elements defined in the class are called members, collectively.

I haven't discussed the internal accessor, just FYI, there is one and it can be combined with protected to make "protected internal". You should look those up if you're not already familiar with them.
Aug 12 '10 #2
Frinavale
9,735 Expert Mod 8TB
balabaster: A method is a function definition - in semantic terms a process that can be executed. i.e. DoSomething();
This is why I always refer to functions/subs/etc as methods :)

It's just easier than trying to fumble around in syntax "correctness" when explaining things.

-Frinny
Aug 12 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Alex Vinokur | last post by:
========================================= Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927 (prerelease) ========================================= Here is some program...
12
by: MacFly | last post by:
Hi everyone, HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer) I want that method to be class member method so it could have access to class...
5
by: jmd | last post by:
hello. i am trying VC++ 7.1 (with vsnet 2003). my example : class Complex { public: Complex ( float re_ = 0.0, float im_ = 0.0 ) : re(re_), im(im_) {} float Re () { return ( re ); } float...
5
by: Dennis | last post by:
I have an array "t" where each element is a structure of type "testing". I want to use the FieldInfo class to set the values of the array element fields. I can read the field properties and...
1
by: Ratnakar .N | last post by:
HELLO, Please tell me the main difference between method overriding and method overloading Thank you
3
Atran
by: Atran | last post by:
What is the difference between method and function !!!
12
by: Ratko | last post by:
Hi all, I was wondering if something like this is possible. Can a base class somehow know if a certain method has been overridden by the subclass? I appreciate any ideas. Thanks, Ratko
1
by: gsreenathreddy | last post by:
What is the difference between method and event.
2
by: Omar.Khalid79 | last post by:
Hi Guys, Maybe this is silly thing to ask but I have a static function declared static void Foo(int a); defined in Foo.cpp and a member method which calls Foo
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.