-4

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

Lance McNearney
  • 9,410
  • 4
  • 49
  • 55
peter
  • 8,158
  • 21
  • 66
  • 119

5 Answers5

4

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
  • 9,563
  • 6
  • 45
  • 58
  • 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 Jan 12 '10 at 19:00
  • how can i use continue here though there are three loops – peter Jan 12 '10 at 19:09
  • 2
    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. – Todd Richardson Jan 12 '10 at 19:16
  • 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 Jan 12 '10 at 19:38
  • 1
    @fauxtrot: You're forgetting 'do'. – Erik Forbes Jan 12 '10 at 19:49
  • @Erik I so rarely use do/while, I lumped it with while... Thanks for the info. – Todd Richardson Jan 12 '10 at 20:22
2

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"
}
Steven Evers
  • 16,649
  • 19
  • 79
  • 126
1

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
  • 9,410
  • 4
  • 49
  • 55
  • 1
    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 Jan 12 '10 at 19:46
  • 1
    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 Jan 12 '10 at 19:57
1

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
  • 28,324
  • 9
  • 63
  • 88
-1

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
  • 578
  • 5
  • 19
  • 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 Jan 12 '10 at 19:21