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
I think Grzenio’s answer is correct, but I don’t think you’re understanding why. To elaborate, take this simple example:
Now, this is exactly what you’ve done. On the first iteration of the loop (
i==0), you’re only setting the first variablePart1. So the output would be:On the second iteration (
i==1),Part2will have it’s value set and then it would ouput:So, taking your example:
objectNamegets set on the first iteration, butoGuiddoes not. Hence whyoGuidremains “all zeros” (Guid.Empty).So, this should be the code you use: