tags:

views:

164

answers:

5

Here is a bit of code

foreach (DataModelObject dmo in allObjects)
{
    if (dmo is IResourcePolicy)
    {
         if (string.Compare(dmo.Name, hostName, true) == 0)
         {
             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;
         }
    }
}

i want to get executed this statement "irp.AgentVersion = agentVersion;" without executing these three loops " foreach (DataModelObject dmo in allObjects),if (dmo is IResourcePolicy), if (string.Compare(dmo.Name, hostName, true) == 0)",, if these loops are executed then i want to execute the entire four assignment inside the loop including the previous assignment(irp.AgentVersion = agentVersion;) also. Previously its showing none in UI without executing loop,,once executed showing all values,, that we need to change

Can anyone give the code to execute this logic,,IS there "Goto" loop condition checking we can do here

+4  A: 

I believe you are looking for continue.

if (dmo is IResourcePolicy)
{
    etc...
}
else
{
    continue;
}

EDIT:

Based on the comments, here is what I understand you want to do:

Also to note, there is only one loop here, and you're breaking out of it once your inner conditional is met. I think this may be what is confusing you. The way it is now, you'll always be processing only one of the objects in your collection.

The following has the break statement removed so it will process every object in your collection.

foreach (DataModelObject dmo in allObjects)
{
    if (dmo is IResourcePolicy)
    {
         // if these loops are not executed i want to show agentversion instead of showing None in UI layer
         IResourcePolicy irp = (IResourcePolicy)dmo;
         irp.AgentVersion = agentVersion;

         //(else) i want to show the entire four things including agent version
         if (string.Compare(dmo.Name, hostName, true) == 0)
         {             
             irp.ResourcePolicy = rp;
             irp.AgentPolicyVersion.Version = Convert.ToInt64(policyVersion);
             irp.ResourcePolicyEnabled = Convert.ToBoolean(enabled);
         }

         // Distribute the object without saving it.
         SpoServer.Spurt.ServerSendObject(dmo, true, 0);
    }
}
Aaron Daniels
what actually i need is,,if these loops are not executed i want to show agentversion instead of showing None in UI layer..More over if loops are executed i want to show the entire four things including agent version,i dont want to put any aditional else part in the existing three loops
peter
how can i use continue here though there are three loops
peter
continue is only functional on loop operations (while, for, foreach.... am i forgetting one...) the scope (or the '{ / * code here /* }' ) defined for the if conditional is not affected by the loop operator. Therefore, the continue statement will cause any code after the continue statement to be disregarded and the loop to continue with the next value.
fauxtrot
I thought that's what he wanted to do. With the comments now, I think his break statement is really what's throwing him off.
Aaron Daniels
@fauxtrot: You're forgetting 'do'.
Erik Forbes
@Erik I so rarely use do/while, I lumped it with while... Thanks for the info.
fauxtrot
A: 

I am not clear on what you are trying to do. Is this close?

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

         if (string.Compare(dmo.Name, hostName, true) == 0) 
         {
            irp.ResourcePolicy = rp; 
            irp.AgentPolicyVersion.Version = Convert.ToInt64(policyVersion); 
            irp.ResourcePolicyEnabled = Convert.ToBoolean(enabled); 
         }

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

         break; 
    }
}
auujay
no this dont work,,i should need to display agent version before the loops executing and aslo if loops executed i need to display all the values including agent version
peter
+2  A: 

It's a little difficult to decipher what you're looking for, but I'll take a stab at it:

bool objectsFound = false;
foreach (DataModelObject dmo in allObjects)
{
    if (dmo is IResourcePolicy && string.Compare(dmo.Name, hostName, true) == 0)
    {
        // ...
        objectsFound = true;
    }
}

if(objectsFound)
{
    // "show the entire four things including agent version"
}
else
{
    // " show agentversion instead of showing None in UI layer"
}
SnOrfus
Let me try this
peter
but i am getting error ,,Error 3 Use of unassigned local variable 'objectsFound'
peter
@peter replace "bool objectsFound;" with "bool objectFound = false;"
bniwredyc
@bniewredyc - thanks. Updated.
SnOrfus
+1  A: 

You can eliminate your loop and nested if statements by using some LINQ. Here's the general idea:

var objects = new List<Object>();
objects.Add(1);
objects.Add("string");
objects.Add("magic");
objects.Add(2.5);

var magic = (from o in objects
             where o is string
                && ((string)o) == "magic"
             select o as string).SingleOrDefault();

if(magic != null) {
    Console.Write("magic found: {0}", magic);
}
else {
    // Do your other logic if nothing was found (loop, etc)
}
Lance McNearney
i am using 2.o framework not 3.5
peter
That's unfortunate. There are ways to use vanilla LINQ in 2.0 though if you're interested: http://stackoverflow.com/questions/2138/linq-on-the-net-2-0-runtime
Lance McNearney
LINQBridge would be recommended: http://www.albahari.com/nutshell/linqbridge.aspx. The accepted answer to that question is a "hack" as its author states. LINQBride is mentioned but later down the page.
Ahmad Mageed
+1  A: 

Here's how I would rewrite that code, if I had the luxury of .NET 3.5 or better. No loops, one if statement.

var irp = allObjects.OfType<IResourcePolicy>()
    .FirstOrDefault(item => String.Equals(item.Name, hostName));

if (irp != null)
{
     irp.ResourcePolicy = rp;
     irp.AgentPolicyVersion.Version = Convert.ToInt64(policyVersion);
     irp.ResourcePolicyEnabled = Convert.ToBoolean(enabled);
     irp.AgentVersion = agentVersion;

     // I don't know the signature of ServerSendObject, 
     // you might need a cast here:
     SpoServer.Spurt.ServerSendObject(irp, true, 0);
}
Joel Mueller