Link to home
Start Free TrialLog in
Avatar of slightlyoff
slightlyoff

asked on

C# Static, Public, Private

I have some experience in VB.NET.  I'd like to learn C# - but there are some concepts that I can't quite get my head around.  The current issue is Static vs Public vs Private when dealing with functions.

Take the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Variables
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 24;
            AddFive(ref number);
            Console.WriteLine("This is the number...: {0}", number);

            Console.ReadLine();
        }

        public void AddFive(ref int theNum)
        {
            theNum += 5;
        }


    }
}

Open in new window


The error I get is "An object reference is required for the non-static field, method or property 'Variables.Program.AddFive(ref int)"

So I need help understanding this error - how do I do an object reference from inside itself?
If AddFive is a method of Program, perhaps me.AddFive would work?  (it doesn't)

It seems that Public and Private are used inside instantiated classes, while static is in the main class - but for some reason (maybe it's my procedural upbringing), it doesn't seem right.

I know that changing "public void AddFive" to "static void AddFive" will fix the error - but I don't understand why.
I understand public and private (for the most part) from working with VB.NET - but static, not so much.  

In my main class (Program), I've learned I have to have "static void Main(string[] args)" as an entry point to my program - so does that mean that ANY and ALL functions in my main class also have to be static?

Any help you can provide on the subject is appreciated.  I did do a search on Google - but the responses I saw were basically "that's just the way it is" - and that's not super helpful.

Thank you for your time!

-Steve
SOLUTION
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Paul Jackson
Paul Jackson
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi kaufmed, I didn't see your comment. Otherwise I would have kept quite.

Mike
-------------------
Steve,

I commented to the best of my knowledge which is not much. So, please wait until the good experts add more comments and do any necessary correction.

BTW, regarding: "will give you 200; " from above post.

You may also reference this same static property by the class name Car as in:

Car.mileage = 500;   now   var mileage = myCar1.mileage;  will give you 500.


Thank you,

Mike
Avatar of slightlyoff
slightlyoff

ASKER

Thank you all for your help.  I split the points because all three responses helped.  
I appreciate your time - I think i'm getting it. :)