[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 369
  • Last Modified:

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
0
ksd123
Asked:
ksd123
2 Solutions
 
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now