VBA to C#: Public, Private and Static Variables in C#

Was answering a forum question by a VBA programmer about public, private and static variables in C#. (Create a New>Project in Visual Studio 2008, and choose Visual C#>Console Application, name it PublicPrivateStatic_Test, and paste in the code below.


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

namespace PublicPrivateStatic_Test
{
    class Program
    {
        public int publicInt = 1;
        private int privateInt = 2;
        static private int privateStaticInt = 3;

        public int getPrivateInt()
        {
            return privateInt;
        } 

        public void setPrivateStaticInt(int i)
        {
            privateStaticInt = i;
        }
        public int getPrivateStaticInt()
        {
            return privateStaticInt;
        }
    }

    class OtherProgram
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            Program q = new Program();

            Console.Write("Public int is {0}", p.publicInt);
            //Prints 1, as it is public

            //Console.Write("Private int is {0}", p.pirvateInt);
            //Wont Compile as it is a private variable.
            //Need to use p.getPrivateInt(). Prints 1.
            Console.Write("\nPrivate int is {0}", p.getPrivateInt());

            //Console.Write("Private int is {0}", p.pirvateStaticInt);
            //Wont Compile as it is a private variable.
            //Need to use getPrivateStaticInt(). Prints 3.
            Console.Write("\nPrivate Static int is {0}", p.getPrivateStaticInt());

            p.setPrivateStaticInt(999);
            Console.Write("\nPrivate Static int is now {0}", q.getPrivateStaticInt());
            //Will print 999 instead of 3. Think of a static variable
            //as only existing once, and p and q both reference it. If
            //instance p changes it, then it is also changed for instance q.

            //Keeps the console open until any-key pressed
            Console.ReadLine();
        }
    }
}

Console output:

Console output

Console output

Advertisements

Tags: , , ,

3 Responses to “VBA to C#: Public, Private and Static Variables in C#”

  1. excel Says:

    excel…

    […]VBA to C#: Public, Private and Static Variables in C# « Bishop On Development[…]…

  2. panic attacks Says:

    For me the best blend can be a function espresso to two components hot
    milk this definitely will give you the perfect cup of hot coffee with diluting the coffee but leaving the mug of great powerful coffee.
    When residing in The country of spain I got into
    drinking coffee making use of an stove espresso maker since these looked to be all anyone ever
    familiar with create coffee now when I came back in order to the united kingdom we bought me one.

  3. forum.vnseameo.org Says:

    Effacer plastique moyens une apparente observer afin de tout vous
    pouvezpas espèreci afin de le public. a étudede currentdemande comme partie dele polo Tommy Hilfiger
    vente priv business et Aussiessayer afin defigure votre
    très propreproprel’intérêt régioni. idée de façon monde est effectivement basé surle individualitédu modèle carle symbol. ils garder penséede exactementchangements ils nécessitentpour faire de drop partoe, concernant onde actuelfashions.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: