Question

I am trying to implement Enum into my code for the first time. I have a simple custom class that looks like this:

public class Application
{
    //Properties
    public string AppID { get; set; }
    public string AppName { get; set; }
    public string AppVer { get; set; }
    public enum AppInstallType { msi, exe }
    public string AppInstallArgs { get; set; }
    public string AppInstallerLocation { get; set; }
}

I have a method within that class called Install() that looks like this:

    public void Install()
    {
        if (AppInstallType.exe)
        {
            ProcessStartInfo procInfo = new ProcessStartInfo("cmd.exe");
            procInfo.Arguments = "/c msiexec.exe /i " + AppInstallerLocation + " " + AppInstallArgs; ;
            procInfo.WindowStyle = ProcessWindowStyle.Normal;

            Process proc = Process.Start(procInfo);
            proc.WaitForExit();
        }
        else
        {
            ProcessStartInfo procInfo = new ProcessStartInfo("cmd.exe");
            procInfo.Arguments = "/c " + AppInstallerLocation + " " + AppInstallArgs;
            procInfo.WindowStyle = ProcessWindowStyle.Normal;

            Process proc = Process.Start(procInfo);
            proc.WaitForExit();
        }
    }

When the AppInstallType was a string, the If statement at the beginning of my Install method worked fine (AppInstallType = "msi"). When I changed AppInstallType to an Enum, I can't seem to work out the syntax for the if statement.

I would like to avoid having to pass in any parameters to the Install() method, if at all possible. It would be nice to be able to install an app just by calling the Install() method on the Application object, like so:

Application app1 = new Application;
app1.AppInstallType = msi;
app1.Install();

How should I be going about this? Thanks in advance.

Was it helpful?

Solution

You have not declared an instance of the Enum, you have simply declared it.

You need

    public enum AppInstallType { msi, exe }

public class Application
{
    //Properties
    public string AppID { get; set; }
    public string AppName { get; set; }
    public string AppVer { get; set; }
    public string AppInstallArgs { get; set; }
    public AppInstallType InstallType;
    public string AppInstallerLocation { get; set; }
}

if(InstallType == AppInstallType.msi)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top