tags:

views:

45

answers:

3

Hello,

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

+1  A: 

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
+1 Nice spot! I don't think you need the `i + 1` though as you're doing `i+=2`.
GenericTypeTea
No Gzenio it didn't work
Reda
@Reda, what result did you get?
Grzenio
I got an error while trying to load the file back to the DB
Reda
Yeah just remove the +1 from the for loop condition and it will workThanks Grzenio
Reda
A: 

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.

cofiem
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
Ah, yes I did forget objectName. You could use`. Aggregate` to do that.
cofiem
+1  A: 

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);

}
GenericTypeTea
Yes that's it :D I really appreciate your help.
Reda