Object.Parse()


Parse

assumes the data is valid and does its best to fit into the type
Can be used to convert a variable to a different type

System.Boolean.Parse("True") 
System.Byte.Parse()
System.Char.Parse("A")
System.DateTime.Parse - link to specific page
System.Decimal.Parse
System.Double.Parse
System.Enum.Parse()
System.Int32.Parse("23")
System.Int64.Parse
System.Single.Parse
System.String.Parse

ParseExact

Only available with System.DateTime
Dates & Times > System.DateTimeParse
only allows the exact format and will throw an exception otherwise


TryCast

TryCast attempts to cast an object to a specific type and if it fails it just returns Nothing instead of an InvalidCastException error which needs to be handled
With TryCast you can test to be sure that the object is not Nothing before performing additional operations on it.


Dim objMailItem As Outlook.MailItem 
   objMailItem = TryCast(objItem, Outlook.MailItem)

TryParse

Introduced in .NET 2.0
It attempts to parse the value and if it is not successful it returns False
Instead of this

int mynumber; 
try
{ mynumber = int.Parse("50"); }
catch (FormatException ex)
{ mynumber = 0; }
catch (OverflowException ex)
{ mynumber = 0; }

we can have

int mynumber; 
if (int.TryParse("50",out mynumber) == false)
{ mynumber = 0; }

The our parameter automatically gets a default value but this is more explicit


TryParseExact

Only available with System.DateTime
Dates & Times > System.DateTimeParse


VB.Net



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext