Static methods in c#

Hi experts,

We have a web service that was developed using .net framework 2.0/ VS 2005 and we deploy the service using MSI installer with some custom actions.Now we are upgrading this service installer to vs 2013 using wix  toolset v3.8.Since we have custom actions defined in the Serivice and I am trying to convert the code written in .net  
framework 2.0/ VS 2005 to .net framework 4.5/ VS 2013.

1)I have following method in one of the custom actions
a)
[RunInstaller(true)]
    public partial class Installer1 : System.Configuration.Install.Installer
    {
        
        private string version;
        
        public Installer1()
        {
            InitializeComponent();
        }


        protected override void OnCommitted(IDictionary savedState)
        {
            
            if(Context.Parameters["testversion"]!=null)
            {
                string version = Context.Parameters["testversion"].ToString();
                this.version=version;
            }
            demo();
        }

        public void demo()
        {
            string somevalue = this.version;
        }

    }

Open in new window



In the above OnCommitted method why there is a local variable "version" and later assigned the value using "this.version".We already have global variable "version" and we can simply do like below

b) if(Context.Parameters["testversion"]!=null)
            {
                version = Context.Parameters["testversion"].ToString();
               
            }

1)My question is "Is there any issue with the above code without using "this.version" ?


2) I am converting MSI custom actions to wix custom actions and in this process I have  converted the above code (a) to following code .Basically we have to convert it to static methods

public class CustomActions
 {

      private static string version;

     [CustomAction]
     public static ActionResult OnCommitted(Session session)
      {
         if(session.CustomActionData[]!=null)
      {

	version = session.CustomActionData[].ToString();

      }
      
      demo();   

         return ActionResult.Success;
     }

     public static void demo()
        {
            string somevalue = version;
        }
   
 }

Open in new window



Since we don't have "this" in vs 2013, I have directly assigned golbal variable "version".Is this correct way of converting non-static to static methods or am i breaking anything in the above code.

Thanks in advance
ksd123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Miguel OzSoftware EngineerCommented:
1)  this.version stores the value used by demo method. Thus it will not compile if you remove line 5.
Actually there is no need for local variable "version", your code could be done like:
if(Context.Parameters["testversion"]!=null)
{
                this.version=Context.Parameters["testversion"].ToString();;
}

Open in new window

Note: Line 5 (version) is a private field not a global variable.

2) Yes, if you are only use CustomActions class via its static methods, but if you are only use it in demo method, you could declare version as local and pass version as a parameter for the demo method; thus, you avoid having a static variable in your class that is not adding any real value.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
Since we don't have "this" in vs 2013, I have directly assigned golbal variable "version".
You have some confusion on the difference between static and instance members. this certainly does exist in VS 2013. Your "problem" is that there is no "this" when working with static members. static members are associated with a class, not an instance. In order to refer to static members you reference against the class name, not an instance variable (which is what this would be).

e.g.

CustomActions.version = session.CustomActionData[].ToString();

Open in new window


However, since you are working in the same class where the private field is defined, you are not required to include the class name--the compiler is smart enough to figure out what you meant in that case.
0
ksd123Author Commented:
Thank you guys.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.