开发者

retrieving Odd values from a List<string> collection and saving them as GUID c#

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 val开发者_如何学编程ues 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:

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

}


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜