Category: "Example Codes"
Use tryparse if that is possible or have a validation
I faced a number of error for parsing a string into int. In most of cases, there is a validation missing.
Code
If (IsNumeric(str)) | |
{ | |
int i =0; | |
Int32.TryParse(str,out i); | |
} |
You can use regex inside IsNumber or use foreach loop to check whether each character is numberic.
BUT NEVER NEVER DO LIKE THIS:
Code
try | |
{ | |
int i= Int32.Parse(i); | |
} | |
catch(FormatException) | |
{ | |
// Error Hanlding | |
} |
Try-catch is an expensive operation!!!!!!!!
Secondly, I like to use TryParse even in the case, the progam can tolrate invalid value. TryParse can allow the logic to continue execution. I hate some things like stack track on the screen.
String.Format- a comma in each thousand
I have tried to Convert.ToString to convert int into a format a comma in each thousand. I am not successfully. Well, because I went to a wrong way. I shoulduse String.Format. Just like this
Display the int in thousand format(e.g. 1,200)
String.Format("{0:N0}",i);
Network Tester
This afternoon, I was practicing Socket Programming in C#. That is the reason I wrote this program.
http://www.itblogs.info/download/NetworkTester.zip
This program can check whether the port of IP address is opened.
Note that, that is only a toy to practice C#.
How to open a url from winform LinkLabel.
That is easy!
In Click of LinkLabel:
System.Diagnostics.Process.Start("http://www.itblogs.info/");
Then .Net Runtime will call your default browser.
CryptoStream
I have done a google search on crypto topic.
Some examples are saying to CryptoStream(outStream,
TripleDESCryptoServiceProvider.CreateDecryptor(),CryptoStreamMode.Write) and then write chirp text into this stream. They are saying it will be decrypted. But this doesn't work for me. I used CryptoStream(inputStream,
TripleDESCryptoServiceProvider.CreateDecryptor(),CryptoStreamMode.Read) reads the chirp text and then writes into an output stream. That works!