Saturday, October 27, 2007

How to use if statement in VB.Net & C# ?

if statement


In vb.net & c# language if statement are differ.Code if statement of vb.net & c# language.

___________________________________________________________________

VB.NET

greeting = IIf(age <>

' One line doesn't require "End If"
If age <>Then greeting = "What`s up?"
If age <>Then greeting = "What`s up?" Else greeting = "Hello"

' Use : to put two commands on same line
If x <> 100 And y <>Then x *= 5 : y *= 2

' Preferred
If x <> 100 And y <>Then
x *= 5
y *= 2
End If

' To break up any long single line use _
If whenYouHaveAReally <> Lines Then _
UseTheUnderscore(charToBreakItUp)

'If x > 5 Then
x *= y
ElseIf x = 5 Then
x += y
ElseIf x <>Then
x -= y
Else
x /= y
End If

Select Case color ' Must be a primitive data type
Case "pink", "red"
r += 1
Case "blue"
b += 1
Case "green"
g += 1
Case Else
other += 1
End Select

-------------------------------------------------------------------------------------------------

C#

greeting = age <>? "What's up?" : "Hello";

if (age < greeting = "What's up?">else
greeting = "Hello";

// Multiple statements must be enclosed in {}
if (x != 100 && y <>

No need for _ or : since ; is used to terminate each statement.





if
(x > 5)
x *= y;
else if (x == 5)
x += y;
else if (x <>else
x /= y;



// Every case must end with break or goto case
switch (color) { // Must be integer or string
case "pink":
case "red": r++; break;
case "blue": b++; break;
case "green": g++; break;
default: other++; break; // break necessary on default
}

-------------------------------------------------------------------------------------------------

Note: This code applicable for both desktop and web application.

-------------------------------------------------------------------------------------------------

No comments: