Posts Tagged ‘VB.NET’

.NET: Check links (http, https, ftp, news etc …)

Thursday, May 14th, 2009

In this article we show how we can know what kind of address is an address. We will use the URI class, which does not allow URLs to work. The class belongs to the URI name space System.

The URI class has property “Scheme” that indicates the type of URI scheme today. The schemes that detects the URI class are:

* UriSchemeFile: tells us that points the direction in which our class is a file URI.
* UriSchemeFtp: the scheme is FTP (File Transfer Protocol)
* UriSchemeGopher: specifies that we can access through URI using the Gopher protocol.
* UriSchemeHttp: access is a URI using the HTTP protocol.
* UriSchemeHttps: the address is https, http secure.
* UriSchemeMailto: tells us that the address is an e-mail, which can be accessed via SMTP.
* UriSchemeNetPipe: tells us that the identifier URI is NetPipe.
* UriSchemeNetTcp: can access through URI using the protocol NetTCP.
* UriSchemeNews: URI is the address of a newsgroup or News.
* UriSchemeNntp: a newsgroup is accessed via NNTP.

The fields above are just reading.

In the following example, create a constant that contains the address from which we obtain information and identify which type of management is our constant.

In Visual Basic:

Const strURL As String = “http://www.wisecodes.com”
Dim strDir As New Uri(strURL)
Select Case strDir.Scheme
Case Uri.UriSchemeFile
'Code for when the address is a file
Case Uri.UriSchemeFtp
'Code for when the address is ftp
Case Uri.UriSchemeGopher
'Code for when the address is a file Gopher
Case Uri.UriSchemeHttp
'Code for when the address is http
Case Uri.UriSchemeHttps
'Code for when the address is https
Case Uri.UriSchemeMailto
'Code for when the address is an e-mail
Case Uri.UriSchemeNetPipe
'Code for when the address is NetPipe
Case Uri.UriSchemeNetTcp
'Code for when the address is NetTcp
Case Uri.UriSchemeNews
'Code for when the address is a newsgroup
Case Uri.UriSchemeNntp
'Code for when the address is a newsgroup
'accessible via NNTP.
End Select

In C Sharp

const string strURL = “http://www.wisecodes.com”
Uri strDir = new Uri (strURL);
switch (strDir.Scheme)
{
case Uri.SchemeFile:
//Code for when the address is a file
case Uri.UriSchemeFtp:
//Code for when the address is ftp
case Uri.UriSchemeGopher:
//Code for when the address is a file Gopher
case Uri.UriSchemeHttp:
//Code for when the address is http
case Uri.UriSchemeHttps:
//Code for when the address is https
case Uri.UriSchemeMailto:
//Code for when the address is an e-mail
case Uri.UriSchemeNetPipe:
//Code for when the address is NetPipe
case Uri.UriSchemeNetTcp:
//Code for when the address is NetTcp
case Uri.UriSchemeNews:
//Code for when the address is a newsgroup
caseUri.UriSchemeNntp:
//Code for when the address is a newsgroup
//accessible via NNTP.
}

Link: Via Microsoft
Happy Programming!! ;-)

Reading source code of a WebPage

Saturday, April 25th, 2009

This method, get the HTML source code of any WebPage

C#.NET

using System.Net;

WebClient webClient = new WebClient();
string strHTMLSourceCode = webClient.DownloadString("http://www.microsoft.com");

VB.NET

Imports System.Net

Dim webClient As WebClient = New WebClient()
Dim strHTMLSourceCode As String = webClient.DownloadString("http://www.microsoft.com")

Read More about webClient.DownloadString(string)…

Happy Programming!!

ASP.Net Gridview to CSV, Excel

Saturday, April 25th, 2009

This method, export to Excel file from Gridview

VB.NET


Sub wc_gvGridviewToExcel(ByVal gvGridview As Gridview, ByVal strExcelFilePath As String)

'Columns

Dim intColumnsCount As Integer = gvGridview.Columns.Count - 1

'Header

Dim strHeaderNames As String = Nothing

For i = 0 To intColumnsCount

strHeaderNames += gvGridview.Columns(i).HeaderText & ";"

Next

WriteToExcel(strHeaderNames, strExcelFilePath)

'Each Row

Dim strRowText As String = Nothing

For Each grdRow As gvGridviewRow In gvGridview.Rows

For i = 0 To intColumnsCount

strRowText += grdRow.Cells(i).Text & ";"

Next

WriteToExcel(strRowText, strExcelFilePath)

strRowText = Nothing

Next

End Sub

Sub WriteToExcel(ByVal strText As String, ByVal strExcelFileName As String)

Dim myFileWriter As New System.IO.StreamWriter(strExcelFileName, True)

myFileWriter.WriteLine(strText)

myFileWriter.Close()

End Sub

C#.NET

public void wc_gvGridviewToExcel(Gridview gvGridview, string strExcelFilePath)
{

 //Columns

 int intColumnsCount = gvGridview.Columns.Count - 1;

 //Header

 string strHeaderNames = null;

 for (i = 0; i <= intColumnsCount; i++) {

 strHeaderNames += gvGridview.Columns(i).HeaderText + ";";
 }

 WriteToExcel(strHeaderNames, strExcelFilePath);

 //Each Row

 string strRowText = null;

 foreach (gvGridviewRow grdRow in gvGridview.Rows) {

 for (i = 0; i <= intColumnsCount; i++) {

 strRowText += grdRow.Cells(i).Text + ";";
 }

 WriteToExcel(strRowText, strExcelFilePath);

 strRowText = null;

 }
}

public void WriteToExcel(string strText, string strExcelFileName)
{

 System.IO.StreamWriter myFileWriter = new System.IO.StreamWriter(strExcelFileName, true);

 myFileWriter.WriteLine(strText);

 myFileWriter.Close();
}

Happy Programming!!

Compare Files

Friday, April 24th, 2009

This method Compares 2 files and returns true or false.

C#.NET

private bool wc_CompareFiles(string FilePath1, string FilePath2)
{
FileInfo FI1 = new FileInfo(FilePath1);
FileInfo FI2 = new FileInfo(FilePath2);

if (FI1.Length != FI2.Length)
return false;

byte[] Filebytes1 = File.ReadAllBytes(FilePath1);
byte[] Filebytes2 = File.ReadAllBytes(FilePath2);

if (Filebytes1.Length != Filebytes2.Length)
return false;

for (int i = 0; i &lt;= Filebytes2.Length - 1; i++)
{
if (Filebytes1[i] != Filebytes2[i])
return false;
}
return true;
}

VB.NET


Private Function wc_CompareFiles(ByVal FilePath1 As String, ByVal FilePath2 As String) As Boolean
 Dim FI1 As New FileInfo(FilePath1)
 Dim FI2 As New FileInfo(FilePath2)

 If FI1.Length <> FI2.Length Then
 Return False
 End If

 Dim Filebytes1 As Byte() = File.ReadAllBytes(FilePath1)
 Dim Filebytes2 As Byte() = File.ReadAllBytes(FilePath2)

 If Filebytes1.Length <> Filebytes2.Length Then
 Return False
 End If

 For i As Integer = 0 To Filebytes2.Length - 1
 If Filebytes1(i) <> Filebytes2(i) Then
 Return False
 End If
 Next
 Return True
End Function

Happy Programming!!

.NET: Color Balance

Tuesday, April 21st, 2009

Represents a good control horizontal scroll bar of Windows standard, this screen will see how to change the colors to a panel control by controlling HScrollBar.

colorbalance

Public Class Form1_WiseCodes

 Private Sub Red_HScrollBar_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Red_HScrollBar.ValueChanged
 ColorPerview_Panel1.BackColor = ColorTranslator.FromOle(RGB(0 + Red_HScrollBar.Value, Green_HScrollBar.Value + 0, 0 + Blue_HScrollBar.Value))
 lblRedValue.Text = Red_HScrollBar.Value
 End Sub

 Private Sub Green_HScrollBar_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Green_HScrollBar.ValueChanged
 ColorPerview_Panel1.BackColor = ColorTranslator.FromOle(RGB(0 + Red_HScrollBar.Value, Green_HScrollBar.Value + 0, 0 + Blue_HScrollBar.Value))
 lblGreenValue.Text = Green_HScrollBar.Value
 End Sub

 Private Sub Blue_HScrollBar_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Blue_HScrollBar.ValueChanged
 ColorPerview_Panel1.BackColor = ColorTranslator.FromOle(RGB(0 + Red_HScrollBar.Value, Green_HScrollBar.Value + 0, 0 + Blue_HScrollBar.Value))
 lblBlueValue.Text = Blue_HScrollBar.Value
 End Sub

End Class

Set Values of all the HScrollBar as following . The three arguments (red, green & blue)  must each be in the range 0 to 255.

colorbalance_02

Download Source Code

Happy Programming!!

FTP file upload in .NET 2.0

Friday, April 17th, 2009

It is clear that the FileUpload components, which can be found on the Standard toolbar of Visual Studio 2005 allows uploading files to the remote site, but does not cover all the expectations desired or at least not mine.
So it is interesting to know how we can attack a specific server and an FTP to copy the desired file to the remote location to stay.

To do this:

Step 1: In general declarations establish that the class will use.

C#

using System.Net;

VB.NET

Imports System.Net

Step 2: Create a button in the Click event and write the following code in C# or VB.NET:

C#

private static void wc_UploadFTP(string ftpServerPath, string filename, string username, string password)
 {
WebClient ClientFtp = new WebClient() ;
ClientFtp.Credentials = new NetworkCredential(username, password);
ClientFtp.UploadFile(ftpServerPath + "/" + new FileInfo(filename).Name, "STOR", filename);
}

VB.NET


Private Shared Sub wc_UploadFTP(ByVal ftpServerPath As String, ByVal filename As String, ByVal username As String, ByVal password As String)
 Dim ClientFtp As New WebClient()
 ClientFtp.Credentials = New NetworkCredential(username, password)
 ClientFtp.UploadFile((ftpServerPath & "/") + New FileInfo(filename).Name, "STOR", filename)
End Sub

Happy Programming!!

Create Drop Down List Options From Enum

Thursday, April 2nd, 2009

To view the values of an enumeration’s type (enum) to a DropdownList (or other controls with a DataSource property available) is sufficient to bind the following code:

Here’s an example with its own enum (of course also works with system types)

?View Code CSHARP
public enum CountryList
{
Germany = 1,
India = 2,
Japan = 3,
UAE = 4,
USA = 5
}
 
DropdownList1.DataSource = System.Enum.GetValues(typeof(CountryList));

Now, whenever you want to get to The Value of Even enum () method of the enum is useful to get under the VALUE of enum sees below snippet will helpful to
you.

?View Code CSHARP
int intCountry = Convert.ToInt32(System.Enum.Parse(typeof(CountryList), DropdownList1.SelectedItem.ToString());

Read More…

Happy Programming!!

Validate: Username, Email Id, Phone No, Zip Code & URL In 5 Languages(JS, C#.Net, VB.Net, JAVA & PHP)

Monday, March 23rd, 2009

Validate of Username, Email Id, Phone No, Zip/Postal Code & URL In Javascript, C#.Net, VB.Net, JAVA & PHP !

1. Validate username: Validate alpha numeric values
2. Validate email addresses : example@domain.com
3. Validate Phone numbers: Validate US phone number eg: 123-456-7890
4. Validate Zip/Postal Codes: Validate US Zip/Postal Code eg: 12345-6789
5. Validate Domain Name eg: http://live.com

Validate username: Validate alpha numeric values

?View Code JAVASCRIPT
function is_valid_username(str_username)
{
 var filter = /^([a-zA-Z\s0-9]*)$/;
 if (!filter.test(str_username))
 {
 alert(’Wrong username format.’);
 }
 else
 {
 alert('Username format is ok.')
 }
 return false;
}
?View Code CSHARP
 using System.Text.RegularExpressions;
 
 private void is_valid_username(string str_username)
 {
 Regex matchRegex = new Regex(@"^[a-zA-Z0-9_]{3,16}$");
 MatchCollection matches  = matchRegex.Matches(str_username);
 if(matches.Count==0)
 {
 Response.Write("Wrong username format."); // In Webserver
 //MessageBox.Show("Wrong username format."); //In Windows Form
 }
 else
 {
 Response.Write("Username format is ok."); // In Webserver
 //MessageBox.Show("Username format is ok."); //In Windows Form
 }
 }
Imports System.Text.RegularExpressions
 
Private Sub is_valid_username(ByVal str_username As String)
    Dim matchRegex As New Regex("^[a-zA-Z0-9_]{3,16}$")
    Dim matches As MatchCollection = matchRegex.Matches(str_username)
    If matches.Count = 0 Then
        Response.Write("Wrong username format.") ' In Webserver
        'MessageBox.Show("Wrong username format."); 'In Windows Form
    Else
        Response.Write("Username format is ok.")    ' In Webserver
        'MessageBox.Show("Username format is ok."); 'In Windows Form
    End If
End Sub
import java.util.regex.*;
 
public class is_valid_username
{
 public static void main(String str_username)
 {
 Pattern p=Pattern.compile("^[A-Za-z0-9]+$");
 Matcher m=p.matcher(str_username);
 boolean matchFound = m.matches();
 if(matchFound)
 {
 System.out.println("Username format is ok.");
 }
 else
 {
 System.out.println("Wrong username format.");
 }
 }
}
Function is_valid_username($str_username)
{
 if (preg_match('/^[a-z\d_]{5,20}$/i',$str_username))
 {
     echo "Username format is ok.";
 }
 else
 {
     echo "Wrong username format.";
 }
}

Validate email addresses : example@domain.com

?View Code JAVASCRIPT
function is_valid_email(str_email_id)
{
 var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
 if (!filter.test(str_email_id))
 {
 alert(’Wrong email address format.’);
 }
 else
 {
 alert('Email address format is ok.')
 }
 return false;
}
?View Code CSHARP
 using System.Text.RegularExpressions;
 private void is_valid_email(string email_id)
 {
 Regex matchRegex = new Regex(@"[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{2,5}");
 MatchCollection matches  = matchRegex.Matches(email_id);
 if(matches.Count==0)
 {
 Response.Write("Wrong email address format."); // In Webserver
 //MessageBox.Show("Wrong email address format."); //In Windows Form
 }
 else
 {
 Response.Write("Email address format is ok."); // In Webserver
 //MessageBox.Show("Email address format is ok."); //In Windows Form
 }
 }
Imports System.Text.RegularExpressions
 
Private Sub is_valid_email(ByVal email_id As String)
    Dim matchRegex As New Regex("[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{2,5}")
    Dim matches As MatchCollection = matchRegex.Matches(email_id)
    If matches.Count = 0 Then
        Response.Write("Wrong email address format.") ' In Webserver
        'MessageBox.Show("Wrong email address format."); 'In Windows Form
    Else
        Response.Write("Email address format is ok.")    ' In Webserver
        'MessageBox.Show("Email address format is ok."); 'In Windows Form
    End If
End Sub
import java.util.regex.*;
public class is_valid_email
{
 public static void main(String str_email_id)
 {
 Pattern p=Pattern.compile("[a-zA-Z]*[0-9]*@[a-zA-Z]*.[a-zA-Z]*");
 Matcher m=p.matcher(str_email_id);
 boolean matchFound = m.matches();
 if(matchFound)
 {
 System.out.println("Email address format is ok.");
 }
 else
 {
 System.out.println("Wrong email address format.");
 }
 }
}
Function is_valid_email($email_id)
{
 if (preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$email_id))
 {
     echo "Email address format is ok.";
 }
 else
 {
     echo "Wrong email address format.";
 }
}

Validate Phone numbers: Validate US phone number eg: 123-456-7890

?View Code JAVASCRIPT
function is_valid_phone_no(str_phone_no)
{
 var filter =  /^((\+?1-)?\d\d\d-)?\d\d\d-\d\d\d\d$/;
 if (!filter.test(str_phone_no))
 {
 alert(’Wrong phone number format.’);
 }
 else
 {
 alert('Phone number format is ok.')
 }
 return false;
}
?View Code CSHARP
 using System.Text.RegularExpressions;
 
 private void is_valid_phone_no(string str_phone_no)
 {
 Regex matchRegex = new Regex(@"\d{3}-\d{3}-\d{4}$");
 MatchCollection matches  = matchRegex.Matches(str_phone_no);
 if(matches.Count==0)
 {
 Response.Write("Wrong phone number format."); // In Webserver
 //MessageBox.Show("Wrong phone number format."); //In Windows Form
 }
 else
 {
 Response.Write("Phone number format is ok."); // In Webserver
 //MessageBox.Show("Phone number format is ok."); //In Windows Form
 }
 }
Imports System.Text.RegularExpressions
Private Sub is_valid_phone_no(ByVal str_phone_no As String)
    Dim matchRegex As New Regex("\d{3}-\d{3}-\d{4}$")
    Dim matches As MatchCollection = matchRegex.Matches(str_phone_no)
    If matches.Count = 0 Then
        Response.Write("Wrong phone number format.") ' In Webserver
        'MessageBox.Show("Wrong phone number format."); 'In Windows Form
    Else
        Response.Write("Phone number format is ok.")    ' In Webserver
        'MessageBox.Show("Phone number format is ok."); 'In Windows Form
    End If
End Sub
import java.util.regex.*;
public class is_valid_phone_no
{
 public static void main(String str_phone_no)
 {
 Pattern p=Pattern.compile("^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$");
 Matcher m=p.matcher(str_phone_no);
 boolean matchFound = m.matches();
 if(matchFound)
 {
 System.out.println("Phone number format is ok.");
 }
 else
 {
 System.out.println("Wrong phone number format.");
 }
 }
}
Function is_valid_phone_no($str_phone_no)
{
 if (preg_match('/\(?\d{3}\)?[-\s.]?\d{3}[-\s.]\d{4}/x',$str_phone_no))
 {
     echo "Phone number format is ok.";
 }
 else
 {
     echo "Wrong phone number format.";
 }
}

Validate Zip/Postal Codes: Validate US Zip/Postal Code eg: 12345-6789

?View Code JAVASCRIPT
function is_valid_zip_code(str_zip_code)
{
 var filter =  /^\d\d\d\d\d-\d\d\d\d$/;
 if (!filter.test(str_zip_code))
 {
 alert(’Wrong Zip/Postal Code format.’);
 }
 else
 {
 alert('Zip/Postal Code format is ok.')
 }
 return false;
}
?View Code CSHARP
 using System.Text.RegularExpressions;
 
 private void is_valid_zip_code(string str_zip_code)
 {
 Regex matchRegex = new Regex(@"\d{5}-\d{4}$");
 MatchCollection matches  = matchRegex.Matches(str_zip_code);
 if(matches.Count==0)
 {
 Response.Write("Wrong Zip/Postal Code format."); // In Webserver
 //MessageBox.Show("Wrong Zip/Postal Code format."); //In Windows Form
 }
 else
 {
 Response.Write("Zip/Postal Code format is ok."); // In Webserver
 //MessageBox.Show("Zip/Postal Code format is ok."); //In Windows Form
 }
 }
Imports System.Text.RegularExpressions
Private Sub is_valid_zip_code(ByVal str_zip_code As String)
    Dim matchRegex As New Regex("\d{5}-\d{4}$")
    Dim matches As MatchCollection = matchRegex.Matches(str_zip_code)
    If matches.Count = 0 Then
        Response.Write("Wrong Zip/Postal Code format.") ' In Webserver
        'MessageBox.Show("Wrong Zip/Postal Code format."); 'In Windows Form
    Else
        Response.Write("Zip/Postal Code format is ok.")    ' In Webserver
        'MessageBox.Show("Zip/Postal Code format is ok."); 'In Windows Form
    End If
End Sub
import java.util.regex.*;
public class is_valid_zip_code
{
 public static void main(String str_zip_code)
 {
 Pattern p=Pattern.compile("^(\\d{5})[- ]?(\\d{4})$");
 Matcher m=p.matcher(str_zip_code);
 boolean matchFound = m.matches();
if(matchFound)
 {
 System.out.println("Zip/Postal Code format is ok.");
 }
 else
 {
 System.out.println("Wrong Zip/Postal Code format.");
 }
 }
}
Function is_valid_zip_code($str_zip_code)
{
 if (preg_match('/^([0-9]{5})(-[0-9]{4})?$/i',$str_zip_code))
{
     echo "Zip/Postal Code format is ok.";
 }
 else
 {
     echo "Wrong Zip/Postal Code format.";
 }
}

Validate Domain Name eg: http://live.com

?View Code JAVASCRIPT
function is_valid_url(str_url)
{
 var filter =   /^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s&lt;&gt;\#%"\,\{\}\\|\\\^\[\]`]+)?$/
 if (!filter.test(str_url))
 {
 alert(’Wrong URL format.’);
 }
 else
 {
 alert('URL format is ok.')
 }
 return false;
}
?View Code CSHARP
 using System.Text.RegularExpressions;
 private void is_valid_url(string str_url)
 {
Regex matchRegex = new Regex(@"(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)?");
 MatchCollection matches  = matchRegex.Matches(str_url);
 if(matches.Count==0)
 {
 Response.Write("Wrong URL format."); // In Webserver
 //MessageBox.Show("Wrong URL format."); //In Windows Form
 }
 else
 {
 Response.Write("URL format is ok."); // In Webserver
 //MessageBox.Show("URL format is ok."); //In Windows Form
 }
 }
Imports System.Text.RegularExpressions
 
Private Sub is_valid_url(ByVal str_url As String)
    Dim matchRegex As New Regex("(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)?")
    Dim matches As MatchCollection = matchRegex.Matches(str_url)
    If matches.Count = 0 Then
        Response.Write("Wrong URL format.") ' In Webserver
        'MessageBox.Show("Wrong URL format."); 'In Windows Form
    Else
        Response.Write("URL format is ok.")    ' In Webserver
        'MessageBox.Show("URL format is ok."); 'In Windows Form
    End If
End Sub
import java.util.regex.*;
public class is_valid_url
{
 public static void main(String str_url)
 {
 Pattern p=Pattern.compile("(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)?");
 Matcher m=p.matcher(str_url);
 boolean matchFound = m.matches();
 if(matchFound)
 {
 System.out.println("URL format is ok.");
 }
 else
 {
 System.out.println("Wrong URL format.");
 }
 }
}
Function is_valid_url($str_url)
{
 if (preg_match('/^([0-9]{5})(-[0-9]{4})?$/i',$str_url))
 {
     echo "URL format is ok.";
 }
 else
 {
     echo "Wrong URL format.";
 }
}

C# From VB.NET

Saturday, March 7th, 2009

What is dfferent between C#.NET & VB.NET

Basic differences

  • C # will be marked in the case identifiers and method names and class names, VB does not distinguish.
  • VB is switched to late binding and early binding (Option Strict On / Off).
  • VB is whether to switch to force a variable’s type (Option Explicit On / Off).

Literals

Not easily accomplished

?View Code CSHARP
 
string s = "Welcome";
char ch = 'abc';
int i = 123;
long l = 123L;
double d = 123.0;
float f = 123.0F;
bool bl = true;
object o = null;
Dim s As String = "Welcome"
Dim ch As Char = "abc" c
Dim i As Integer = 123
Dim l As Long = 123L
Dim d As Double = 123.0
Dim f As Single = 123.0 F
Dim bl As Boolean = True
Dim o As Object = Nothing

Date Literals

Dim d1 = # 05 / 01 / 2008 #
Dim d2 = # 05 / 01 / 2008 12: 00: 00 AM #

I have another spare.

Array

Creation and access of the array

?View Code CSHARP
int [] array = new int [] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
/ / Skip
int [] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
/ / Type inference
var array = new int [] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
 
Console.WriteLine (array [0]);
Console.WriteLine (array [1]);
Console.WriteLine (array [2]);
Dim array () As Integer = new Integer () {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
'Omitted
Dim array () As Integer () = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
'Type inference
Dim array () = New Integer () {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
 
Console. WriteLine (array (0));
Console. WriteLine (array (1));
Console. WriteLine (array (2));

Operator

The difference is in this together as
Equal Sign:
C# : ==
VB.NET =
Inequality:
C# : !=
VB.NET: <>

Statement

Conditional or repetitive statement

?View Code CSHARP
int int_val = Get_Value ();
/ / Conditional branch
if (int_val == 0) {
/ / True
} Else {
/ / False
}
 
/ / For statement
for (int i = 0; i <10; i + +) {
/ / Processing
}
 
int [] int_Array = Get_Array ();
/ / Statement (Collection)
foreach (int i in int_Array) {
/ / Processing
}
Dim int_val As Integer = Get_Value ()
'Branches
If int_val = 0 Then
'True
Else
'False
End If
 
'Statement
For i As Integer = 0 to 10
'Treatment
End For
 
Dim int_Array As Integer (,) = Get_Array ()
'Statement (Collection)
For Each i As Integer in int_Array
'Treatment
Next

Namespace

Namespace

?View Code CSHARP
namespace TheCodersSpace {
/ / Define
}
 
/ / Import
using TheCodersSpace;
/ / Alias
using TC = TheCodersSpace;
Namespace TheCodersSpace
'Definition
End Namespace
 
'Import
Imports TheCodersSpace
'Alias
Imports TC = TheCodersSpace

Import XML namespace

Imports <xmlns:TheCoders="http://code.venuthomas.net">

Class

Class (VB, the class also included in the module)

?View Code CSHARP
class TheCoders {
}
 
/ / Static class
static class StaticTheCoders {
}
 
/ / Abstract base class
abstract class AbstractTheCoders {
}
 
/ / Inheritance
class ExtendTheCoders: TheCoders {
}
 
/ / No inheritance
sealed class SealedTheCoders {
}
Class TheCoders
End Class
 
'Static class (Module)
Module StaticTheCoders
End Module
 
'Base class
MustInherit Class AbstractTheCoders
End Class
 
'Inherited
Class ExtendTheCoders
Inherits TheCoders
End Class
 
'Not Inherited
NotInheritable Class SealedTheCoders
End Class

Interface

Interface

?View Code CSHARP
interface ITheCoders {
}
 
/ / Implementation
class TheCoders: ITheCoders {
}
Interface ITheCoders
End Interface
 
'Implementation
Class TheCoders
Implements ITheCoders
End Class

Structure

Structure

?View Code CSHARP
struct TheCoders {
public string Name;
}
Structure TheCoders
Public Name As String
End Structure

Methods

Method (professional function)

?View Code CSHARP
public void TheCodersDoSomething () {
}
 
/ / In argument
public void TheCodersPrintMessage (string message) {
}
/ / Two arguments, the overload
public void TheCodersPrintMessage (string message, int count) {
}
 
/ / Return value with
public string TheCodersGetMessage () {
}
 
/ / Overrides
public virtual void TheCodersOverridableMethod () {
}
 
/ / Overrides
public override void TheCodersOverridableMethod () {
}
 
/ / Redefinition
public new public void TheCodersReDeclareMethod () {
}
Public Sub TheCodersDoSomething ()
End Sub
 
'In argument
Public Sub TheCodersPrintMessage (ByVal message As String)
End Sub
'Two Arguments, Overloading
Public Sub TheCodersPrintMessage (ByVal message As String, ByVal count As Integer)
End Sub
 
'In Returns
Public Function TheCodersGetMessage () As String
End Function
 
'Can override
Public Overridable Sub TheCodersOverridableMethod ()
End Sub
 
'Override
Public Overrides Sub TheCodersOverridableMethod ()
End Sub
 
'Redefining
Public Shadows Sub TheCodersReDeclareMethod () {
}

Properties

Properties

?View Code CSHARP
private string name;
/ / Properties
public string Name {
get {return name;}
set {name = value;}
}
 
/ / Read-only property
public string TheCodersReadOnlyName {
get {return name;}
}
Private _name As String
'Properties
Public Property Name () As String
Get
Return _name
End Get
Set (ByVal value As String)
_name = value
End Set
End Property
 
'Read-only property
Public ReadOnly Property TheCodersReadOnlyName () As String
Get
Return _name
End Get
End Property

Generic

Generic

?View Code CSHARP
List<string> strList = new List<string>();
strList.Add ( "Venu");
strList.Add ( "Thomas");
 
/ / Class definition
class GenericTheCoders <t>  {
public T heCodersGetValue () {
}
}
Dim strList As List (Of String) = New List (Of String)
strList. Add ( "Venu")
strList. Add ( "Thomas")
 
/ / Class definition Class GenericTheCoders (Of T)
Public Function heCodersGetValue () As T
End Function
End Class

Nullable Types

Nullable there.

?View Code CSHARP
int i = null;
Dim i As Integer = Nothing

Type inference

Type inference, it is. NET 3.5 (C # 3.0, VB9) function from

?View Code CSHARP
var strVal = "Welcome";
var intVal = 1;
Dim strVal = "Welcome"
Dim intVal = 1

Lambda expression

Lambda expression

?View Code CSHARP
Func <int, int, int> func = (a, b) => a * b;
func (2, 3);
Dim func As Func (Of Integer, Integer, Integer) = Function (a, b) a * b
func (2, 3)

Also write this

Dim func = Function (a As Integer, b As Integer) a * b

VB.NET smarter than the type inference

Directives preprocessor

VB I was able to

?View Code CSHARP
# define TheCoders
 
# if TheCoders
/ / TheCoders
# else
/ / Not TheCoders
# endif
# Const TheCoders = True
 
# If TheCoders
'TheCoders
# Else
'Not TheCoders
# End If