Kashif
Kashif

Reputation: 875

"Use of unassigned local variable" error with an Interface

I'm having trouble with some syntax. I'm not really familiar with interfaces so please excuse my ignorance.

VS2010 is giving me an error at... application.Name = System.AppDomain.CurrentDomain.FriendlyName;

public static void AddApplication(string applicationName = null, string processImageFileName = null)
{
    INetFwAuthorizedApplications applications;
    INetFwAuthorizedApplication application;

    if(applicationName == null)
    {
        application.Name = System.AppDomain.CurrentDomain.FriendlyName;/*set the name of the application */
    }
    else
    {
        application.Name = applicationName;/*set the name of the application */
    }

    if (processImageFileName == null)
    {
        application.ProcessImageFileName = System.Reflection.Assembly.GetExecutingAssembly().Location; /* set this property to the location of the executable file of the application*/
    }
    else
    {
        application.ProcessImageFileName = processImageFileName; /* set this property to the location of the executable file of the application*/
    }

    application.Enabled =  true; //enable it

    /*now add this application to AuthorizedApplications collection */
    Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); 
    INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType); 
    applications = (INetFwAuthorizedApplications)mgr.LocalPolicy.CurrentProfile.AuthorizedApplications;
    applications.Add(application);
}

I can make that error go away by setting application to null but that causes a run-time null reference error.

Edit:

Here's where I'm adapting the code from. I hope it gives more context http://blogs.msdn.com/b/securitytools/archive/2009/08/21/automating-windows-firewall-settings-with-c.aspx

Upvotes: 3

Views: 4143

Answers (2)

Bryan B
Bryan B

Reputation: 4535

Interfaces are used to describe what an object does, not what it is specifically. To put into "real world" terms, an interface might be like:

ISmallerThanABreadbox with a FitIntoBreadbox() method. I can't ask you to give me "the smaller than a breadbox" ... as that doesn't make any sense. I can only ask you to give me something that "IS smaller than a breadbox". You have to come up with your own object that makes sense to have the interface on it. An apple is smaller than a breadbox, so if you have a breadbox that only holds items smaller than it, an apple is a good candidate for the ISmallerThanABreadbox interface.

Another example is IGraspable with a Hold() method and FitsInPocket bool property. You can ask to be given something that IS graspable that may or may not fit in your pocket, but you can't ask for "the graspable".

Hope that helps...

Upvotes: 1

Eric J.
Eric J.

Reputation: 150228

You never initialize

application

before using it here:

application.Name = System.AppDomain.CurrentDomain.FriendlyName;

The variable application is defined as:

INetFwAuthorizedApplication application

You need to assign an instance of a class that implements the interface INetFwAuthorizedApplication.

Somewhere there must be one (or probably more) classes in your project that look something like this:

public class SomeClass : INetFwAuthorizedApplication
{
    // ...
}

public class AnotherClass : INetFwAuthorizedApplication
{
    // ...
}

You need to determine what class you should use (SomeClass, AnotherClass) then assign an appropriate object, e.g. like this:

INetFwAuthorizedApplication application = new SomeClass();

Upvotes: 10

Related Questions