In this video we will use with new methods and TryParse TryParseExact () in the two versions of Visual Studio 2010 have been extended to the Guid class. For without these methods of testing the process of validation of a guide assumes the use of code we say "handmade". For example we could think to catch the exception that occurs in an attempt to convert a string into a numeric identifier. So for example, create a function such as ad hoc and then create an ad hoc function such CheckGuid that this function is called passing the string Guid be converted, and just try to make the conversion, creating a new guid. If successful returns true, return false on failure. Catch the exception that occurs after conversion error so if we do a quick test CheckGuid we can call this function, passing an invalid string and throwing this code.
We can check, maybe put a breakpoint to check what is the flow of the code. The method has been called up, we go step by step this is an attempt to convert the string to a new guid. The error occurs, false is returned so they can let go. Now the code is returned false. But this assumes the fact to throw an exception in another way ever to check the validity of a guide may be the use of regular expressions. so we can replace our function with another CheckGuid checkguid that actually uses regular expressions.
We can try again to launch this code and know step by step flow.
Come on, now occurs in the attempt to launch our string with the regular expression, go ahead, here is that even in this case the result is a nice false. Fortunately, with Visual Studio 20010 was introduced TryParse (). As we said at the beginning of this video the TryParse () Visual Studio 2010 allows you to make the attempt to convert from string to guid and then come back and see a true value if successful and false if an error occurs.
So there will be no need for me to generate any exception, and then Trappist error in Visual Studio 2010. For example we will create a method that will be called ConvertiGuid that, given a string passed as parameter will attempt to convert a guid just using this new method TryParse (). The method is exactly that here. The method ConvertiGuid as we have said before is passed as a string parameter called string_in_guid input string and the string that is passed to the method will be used to generate a true right guid. auto_guid exactly is the guid from the conversion using the TryParse (). This line of code does is print the video entry guide. This is the true heart of this method because it is the TryParse: it takes as a parameter the string of the GUID to be converted, and as an output parameter the resulting guid.
If successful, that is if Guid.TryParse with these parameters is equal to true, then the program goes into this line of code and we will see the resulting guid. Otherwise it will be displayed instead of a conversion failure.
In the Main () method we are doing is creating a new guid and declare randomly generated on the fly and then convert it to various formats such as the B format, the first attempt to convert the format guide that is raised here this guide uses the format parameter B guid species including braces and using dashes. This second conversion attempt but the drive formatted in parentheses instead of braces and also uses this hyphens.
This conversion however does not use any parentheses and dashes will be neither a block of subsequent characters. The format consists instead of driving without using hyphens, parentheses, but instead will use the X hexadecimal. All these can be seen as a valid driver's are expressed, however, with the formatting. We can also pass a test and see for example invalid guid the guid correctly set up here but formed only by the top 10 Disney character in a substring, and 1 to 10 and finally a last a GUID that is not declared invalid string " hello ". Try running this code and see the result of the first attempt of conversion: the B that exactly this format with dashes and with the brace, was able to convert the string to guid. If you press a button the second conversion is done with the "conversion" in the formatting with parentheses and dashes. The second conversion was successful. We press a button and move on to the third conversion in the format consists of a single block. Again the conversion went well. The fourth is conversion without parentheses but with hyphens, and even here we did well. Hexadecimal format, so that with the X. Again the conversion failed. Let's find out the conversion of the string of guid invalid. Indeed we can verify that the conversion failed, the more so that the final conversion of the string "hello", this also fails. So we did all this with the method that converts TryParse guid guid regardless of the format of which is passed as parameter. Unlike the case instead of the method TryPrarseExact instead try to convert a string to a second format guid precise. In fact, in an example below we will try to convert the drive from the previous of which each represents a different form of driving, specific format. So we replace this block of code with the new and verify that indeed
throughout this first part is equal. The method is identical convertiGuid. The only change is that we have changed is the method TryParseExact, which in addition to the input string and the GUID of output has an additional parameter that defines the format of which is guid verificarndo. That is, the conversion will take effect only if it is passed a string that represents a GUID in the format denoted by the letter.
We check as we did before the conversion of these strings in the resulting guid. We launch the code and then enter the result of the first conversion of the first conversion attempt. It uses a format B, braces and hyphens, but as we see the format B. is different from the format N. remember size N. here that this is a guid formatted without dashes and without brackets: it was a block of characters all stick together. So do not reflect the desired format conversion fails. Let's try the second format p, parentheses and dashes. Again the format does not reflect the correct format for which conversion fails the third attempt. Instead what should be successful because it passes a guid formatted as a parameter N. let 'and actually the conversion succeeded. Let's go on the format D but without parentheses with dashes failed the X format, in hexadecimal still does. The guid invalid surely fail, because the conversion in that case it is absolutely impossible that we have seen that while the method and TryParse () allows regardless of format conversion method instead TryParseExact () is close to the format that is passed and maintains conversion only if the format you want to convert the string matches the format specified method parameter that is exactly the second parameter.