1

I'm trying to retrieve odd values in a List of strings and convert them into a Guid Object. Here is what i came up with

        Guid oGuid = new Guid();
        string objectName = string.Empty;

        for (int i = 0; i < lst_objectName_Guid.Count; i++)
        {
            if (i % 2 != 0) //The GUID values are in the Odd-Numbered Indexses
            {
                oGuid = new Guid(lst_objectName_Guid[i]); //Convert the GUID Values from string to Guid
            }
            else
            {
                objectName = lst_objectName_Guid[i];    //Assign the objectName Values into a string variable
            }

            Console.WriteLine(objectName);
            Console.WriteLine(oGuid);

The problem is now that always it displays a set of (0) Zero's as the first Guid is retrieved and I get a "Object does not exist" when I check if the Object is locked or not.(I get this on every even object that is tested)

Can anybody tell me what's happening and why? and if there is a better way to retrieve odd_numbered indexes and store them as GUID

I'm pretty sure that the odd indexes hold the Guid Values as strings. I printed out the List and made sure of that

Thanks

Reda
  • 470
  • 2
  • 11
  • 19
  • Can you add the code that checks if the object is locked or not? – Nissim Aug 26 '10 at 13:03
  • Please post the full code snippet... nothing is understood from your screenshot – Nissim Aug 26 '10 at 13:08
  • Look at the upvoted answer from @Grzenio. It's because you're printing oGuid before you've assigned it a value. – djdd87 Aug 26 '10 at 13:10
  • this is driving me crazy because it appears to be so obvious as to what's wrong. I've tried to add a more in-depth answer to help you. – djdd87 Aug 26 '10 at 13:26

3 Answers3

1

You print your objects too often, try this approach:

    Guid oGuid = new Guid();
    string objectName = string.Empty;

    for (int i = 0; i +1 < lst_objectName_Guid.Count; i+=2)
    {
        objectName = lst_objectName_Guid[i];
        oGuid = new Guid(lst_objectName_Guid[i+1]); //Convert the GUID Values from string to Guid


        Console.WriteLine(objectName);
        Console.WriteLine(oGuid);
    }
Grzenio
  • 35,875
  • 47
  • 158
  • 240
1

I think Grzenio's answer is correct, but I don't think you're understanding why. To elaborate, take this simple example:

string Part1 = "";
string Part2 = "";

for (int i = 0; i < 2; i++)
{
    if (i == 0)
    {
        Part1 = "Set on first iteration";
    }
    else
    {
        Part2 = "Set on second iteration";
    }
}

Now, this is exactly what you've done. On the first iteration of the loop (i==0), you're only setting the first variable Part1. So the output would be:

Part1: "Set on first iteration"
Part2: ""

On the second iteration (i==1), Part2 will have it's value set and then it would ouput:

Part1: "Set on first iteration"
Part2: "Set on second iteration"

So, taking your example:

Guid oGuid = new Guid(); // Equals all 0s by default
string objectName = string.Empty;

objectName gets set on the first iteration, but oGuid does not. Hence why oGuid remains "all zeros" (Guid.Empty).

So, this should be the code you use:

Guid oGuid = new Guid();
string objectName = string.Empty;

for (int i = 0; i < lst_objectName_Guid.Count; i+=2)
{

    // Notice how BOTH variables are assigned
    oGuid = new Guid(lst_objectName_Guid[i]); 
    objectName = lst_objectName_Guid[i + 1];

    // So when they're written to the console, they both have values
    Console.WriteLine(objectName);
    Console.WriteLine(oGuid);

}
djdd87
  • 67,346
  • 27
  • 156
  • 195
0

If you can use Linq, try this:

lst_objectName_Guid.Select((item, index) => index % 2 != 0 ? new Guid(item) : Guid.Empty);

That uses the overload of .Select() that includes the index. You'll end up with an iEnumerable of Guids, with Guid.Empty for non-Guids.

Also, you may want to use a TryParseGuid method (you'll have to make your own/find one like this one) to make sure they are in fact Guids.

Community
  • 1
  • 1
cofiem
  • 1,384
  • 1
  • 17
  • 29
  • I am using LINQ. Where shall I write this? Won't this only get me the GUID values only? I need the objectName as well – Reda Aug 26 '10 at 13:19
  • Ah, yes I did forget objectName. You could use`. Aggregate` to do that. – cofiem Aug 26 '10 at 13:35