tags:

views:

118

answers:

3

Possible Duplicates:
IF structure issue
Logic problem while setting conditions

I have the following code:

foreach (DataModelObject dmo in allObjects)
{
    if (string.Compare(dmo.Name, hostName, true) == 0)
    {
        if (dmo is IResourcePolicy)
        {
            IResourcePolicy irp = (IResourcePolicy)dmo;
            irp.ResourcePolicy = rp;
            irp.AgentPolicyVersion.Version = Convert.ToInt64(policyVersion);
            irp.ResourcePolicyEnabled = Convert.ToBoolean(enabled);
            irp.AgentVersion = agentVersion;

            // Distribute the object without saving it.
            SpoServer.Spurt.ServerSendObject(dmo, true, 0);
            break;
        }
    }
}

here i want to assign "irp.AgentVersion = agentVersion;" outside of the main loop and more over neglecting the loop if (dmo is IResourcePolicy) like this way if i am doing this way

foreach (DataModelObject dmo in allObjects)
{
    IResourcePolicy irpa ;
    irpa.AgentVersion = agentVersion;
}

i am getting error Error Use of unassigned local variable 'irpa'

what shall i do here?

A: 

irpa is an instance of a class, meaning it's a reference type. A reference type variable needs to be initialized with the new() operator or it needs to point to an existing variable of the same type using the '=' operator.

IResourcePolicy irpa = new IResourcePolicy() ;
irpa.AgentVersion = agentVersion;

However, the syntax indicates that irpa might be an Interface - which is abstract and thus unusable in this way

Maciek
+1  A: 

I do not know exactly what you want to achieve. I can only highlight your errors:

foreach (DataModelObject dmo in allObjects)
{
    IResourcePolicy irpa ; // <-- declaring but not instantiated or assigned
    irpa.AgentVersion = agentVersion; // <-- hence this error...
}
o.k.w
+2  A: 

I am not sure what exactly you are trying to achieve, but here are some options:

If DataModelObject class contains the AgentVersion property, you can simply do it like this (although this is most likely not the case):

foreach (DataModelObject dmo in allObjects)
{
    dmo.AgentVersion = agentVersion;
}

If AgentVersion is implemented only in some derived classes (not all), then you need to check explicitly it the derived class implements that interface, like you are doing right now:

foreach (DataModelObject dmo in allObjects)
{
    if (dmo is IResourcePolicy)
    {
         IResourcePolicy policy = (IResourcePolicy)dmo;
         policy.AgentVersion = agentVersion;
    }
}

Third case would be that you have several derived classes which don't implement the same interface, but still have the AgentVersion property. The cleanest way to fix that (if you can change the source code) would be to have all these objects implement the same interface, containing properties which are common to them all:

interface IAgentVersion
{
     string AgentVersion { get; }
     // omit other properties which are not common to all objects
}

interface IResourcePolicy : IAgentVersion
{
     string ResourcePolicy { get; }
     // ...
     // add all policy-specific properties
}

Now you are sure that all IAgentVersion and IResourcePolicy objects implement that common property (AgentVersion), and you only need to check for the base interface:

foreach (DataModelObject dmo in allObjects)
{
    if (dmo is IAgentVersion)
    {
         IAgentVersion = (IAgentVersion)dmo;
         policy.AgentVersion = agentVersion;
    }
}
Groo