Thursday 28 June 2012

.Net Tips : Validate GUID string and Improvement in GUID in .Net Framework 4

In .NET Framework 4 there is enhancement System.Guid structure, These includes new TryParse and TryParseExact methods. Using these methods you can validate GUID string.
GUID means a globally unique identifier (GUID). GUID is a structure and it's namespace is System.Guid. A GUID is a 128-bit integer this can be used in all computers and networks wherever a unique identifier is required. This identifier has a very less probability of being duplicated.


Here is example for this.
In this example we create new guid object and also take one string variable which has invalid guid. After that we use TryParse method to validate that both variable has valid guid format or not. By running example you can see that string variable has not valid guid format and it gives message of "InValid guid". If string variable has valid guid than this will return true in TryParse method.


C# Example :
        //Generate New GUID
        Guid objGuid = Guid.NewGuid();
        //Take invalid guid format
        string strGUID = "aaa-a-a-a-a";

        Guid newGuid;

        if (Guid.TryParse(objGuid.ToString(), out newGuid) == true)
        {
            Response.Write(string.Format("<br/>{0} is Valid GUID.", objGuid.ToString()));
        }
        else
        {
            Response.Write(string.Format("<br/>{0} is InValid GUID.", objGuid.ToString()));
        }

        
        Guid newTmpGuid;

        if (Guid.TryParse(strGUID, out newTmpGuid) == true)
        {
            Response.Write(string.Format("<br/>{0} is Valid GUID.", strGUID));
        }
        else
        {
            Response.Write(string.Format("<br/>{0} is InValid GUID.", strGUID));
        }

VB.net Example :
        'Generate New GUID
        Dim objGuid As Guid = Guid.NewGuid()
        'Take invalid guid format
        Dim strGUID As String = "aaa-a-a-a-a"

        Dim newGuid As Guid

        If Guid.TryParse(objGuid.ToString(), newGuid) = True Then
            Response.Write(String.Format("<br/>{0} is Valid GUID.", objGuid.ToString()))
        Else
            Response.Write(String.Format("<br/>{0} is InValid GUID.", objGuid.ToString()))
        End If


        Dim newTmpGuid As Guid

        If Guid.TryParse(strGUID, newTmpGuid) = True Then
            Response.Write(String.Format("<br/>{0} is Valid GUID.", strGUID))
        Else
            Response.Write(String.Format("<br/>{0} is InValid GUID.", strGUID))
        End If

Output :

This type of C# Tips is very useful in day to day programming life.

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.




4 comments:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.
    python Course in Pune
    python Course institute in Chennai
    python Training institute in Bangalore

    ReplyDelete
  2. It is extremely nice to see the greatest details presented in an easy and understanding manner.
    data scientist certification malaysia

    ReplyDelete
  3. All things considered I read it yesterday yet I had a few musings about it and today I needed to peruse it again in light of the fact that it is very elegantly composed.

    ReplyDelete
  4. 360DigiTMG, the top-rated organisation among the most prestigious industries around the world, is an educational destination for those looking to pursue their dreams around the globe. The company is changing careers of many people through constant improvement, 360DigiTMG provides an outstanding learning experience and distinguishes itself from the pack. 360DigiTMG is a prominent global presence by offering world-class training. Its main office is in India and subsidiaries across Malaysia, USA, East Asia, Australia, Uk, Netherlands, and the Middle East.

    ReplyDelete