You are on page 1of 16

Thi s document i s an aut hor i zed der i vat i ve of Fr ank McCown' s " VB.

NET and
C# Compar i son" ( C) 2005 at
ht t p: / / www. har di ng. edu/ USER/ f mccown/ WWW/ vbnet _cshar p_compar i son. ht ml
Thi s wor k i s l i censed under a Cr eat e Common Li cense

Comments
VB.NET
' Si ngl e l i ne onl y
RemSi ngl e l i ne onl y
C#
/ / Si ngl e l i ne
/ * Mul t i pl e
l i ne */
/ / / XML comment s on si ngl e l i ne
/ ** XML comment s on mul t i pl e l i nes */
Program Structure
VB.NET
I mpor t s Syst em
Namespace MyNameSpace
Cl ass Hel l oWor l d
' Ent r y poi nt whi ch del egat es t o C- st yl e mai n
Pr i vat e Funct i on
Publ i c Over l oads Shar ed Sub Mai n( )
Mai n( Syst em. Envi r onment . Get CommandLi neAr gs( ) )
End Sub

Over l oads Shar ed Sub Mai n( ar gs( ) As St r i ng)
Syst em. Consol e. Wr i t eLi ne( " Hel l o Wor l d" )
End Sub ' Mai n
End Cl ass ' Hel l oWor l d End Namespace ' MyNameSpace
C#
usi ng Syst em
Namespace MyNameSpace
{
cl ass Hel l oWor l d
{
st at i c voi d Mai n( st r i ng[ ] ar gs)
{
Syst em. Consol e. Wr i t eLi ne( " Hel l o
Wor l d" )
}
}
}





























Thi s document i s an aut hor i zed der i vat i ve of Fr ank McCown' s " VB. NET and
C# Compar i son" ( C) 2005 at
ht t p: / / www. har di ng. edu/ USER/ f mccown/ WWW/ vbnet _cshar p_compar i son. ht ml
Thi s wor k i s l i censed under a Cr eat e Common Li cense
Data Types
VB.NET
' Val ue Types
Bool ean
Byt e
Char ( exampl e: " A" )
Shor t , I nt eger , Long
Si ngl e, Doubl e
Deci mal
Dat e


' Ref er ence Types
Obj ect
St r i ng



Di mx As I nt eger
Syst em. Consol e. Wr i t eLi ne( x. Get Type( ) )
Syst em. Consol e. Wr i t eLi ne( TypeName( x) )


' Type conver si on
Di md As Si ngl e = 3. 5
Di mi As I nt eger = CType ( d, I nt eger )
i = CI nt ( d)
i = I nt ( d)
C#
/ / Val ue Types
bool
byt e, sbyt e
char ( exampl e: ' A' )
shor t , ushor t , i nt , ui nt , l ong, ul ong
f l oat , doubl e
deci mal
Dat eTi me


/ / Ref er ence Types
obj ect
st r i ng



i nt x;
Consol e. Wr i t eLi ne( x. Get Type( ) )
Consol e. Wr i t eLi ne( t ypeof ( i nt ) )


/ / Type conver si on
f l oat d = 3. 5;
i nt i = ( i nt ) d
Constants
VB.NET
Const MAX_AUTHORS As I nt eger = 25
ReadOnl y MI N_RANK As Si ngl e = 5. 00
C#
const i nt MAX_AUTHORS = 25;
r eadonl y f l oat MI N_RANKI NG = 5. 00;
























Thi s document i s an aut hor i zed der i vat i ve of Fr ank McCown' s " VB. NET and
C# Compar i son" ( C) 2005 at
ht t p: / / www. har di ng. edu/ USER/ f mccown/ WWW/ vbnet _cshar p_compar i son. ht ml
Thi s wor k i s l i censed under a Cr eat e Common Li cense
Enumerations
VB.NET
EnumAct i on
St ar t
' St op i s a r eser ved wor d
[ St op]
Rewi nd
For war d
End Enum

EnumSt at us
Fl unk = 50
Pass = 70
Excel = 90
End Enum

Di ma As Act i on = Act i on. St op
I f a <> Act i on. St ar t Then _
' Pr i nt s " St op i s 1"
Syst em. Consol e. Wr i t eLi ne( a. ToSt r i ng & " i s " &
a)

' Pr i nt s 70
Syst em. Consol e. Wr i t eLi ne( St at us. Pass)
' Pr i nt s Pass
Syst em. Consol e. Wr i t eLi ne( St at us. Pass. ToSt r i ng( ) )


EnumWeekdays
Sat ur day
Sunday
Monday
Tuesday
Wednesday
Thur sday
Fr i day
End Enum' Weekdays
C#
enumAct i on {St ar t , St op, Rewi nd, For war d};
enumSt at us {Fl unk = 50, Pass = 70, Excel =
90};











Act i on a = Act i on. St op;
i f ( a ! = Act i on. St ar t )
/ / Pr i nt s " St op i s 1"
Syst em. Consol e. Wr i t eLi ne( a + " i s " +
( i nt ) a) ;

/ / Pr i nt s 70
Syst em. Consol e. Wr i t eLi ne( ( i nt )
St at us. Pass) ;
/ / Pr i nt s Pass
Syst em. Consol e. Wr i t eLi ne( St at us. Pass) ;



enumWeekdays
{
Sat ur day, Sunday, Monday, Tuesday,
Wednesday, Thur sday, Fr i day
}


























Thi s document i s an aut hor i zed der i vat i ve of Fr ank McCown' s " VB. NET and
C# Compar i son" ( C) 2005 at
ht t p: / / www. har di ng. edu/ USER/ f mccown/ WWW/ vbnet _cshar p_compar i son. ht ml
Thi s wor k i s l i censed under a Cr eat e Common Li cense
Operators
VB.NET
' Compar i son
= < > <= >= <>


' Ar i t hmet i c
+ - * /
Mod
\ ( i nt eger di vi si on)
^ ( r ai se t o a power )


' Assi gnment
= += - = *= / = \ = ^= <<= >>= &=


' Bi t wi se
And AndAl so Or Or El se Not << >>


' Logi cal
And AndAl so Or Or El se Not


' St r i ng Concat enat i on
&
C#
/ / Compar i son
== < > <= >= ! =


/ / Ar i t hmet i c
+ - * /
% ( mod)
/ ( i nt eger di vi si on i f bot h oper ands ar e
i nt s)
Mat h. Pow( x, y)


/ / Assi gnment
= += - = *= / = %= &= | = ^= <<=
>>= ++ - -


/ / Bi t wi se
& | ^ ~ << >>


/ / Logi cal
&& | | !


/ / St r i ng Concat enat i on
+
































Thi s document i s an aut hor i zed der i vat i ve of Fr ank McCown' s " VB. NET and
C# Compar i son" ( C) 2005 at
ht t p: / / www. har di ng. edu/ USER/ f mccown/ WWW/ vbnet _cshar p_compar i son. ht ml
Thi s wor k i s l i censed under a Cr eat e Common Li cense
Choices
VB.NET
gr eet i ng = I I f ( age < 20, " What ' s up?" , " Hel l o" )


' One l i ne doesn' t r equi r e " End I f " , no " El se"
I f l anguage = " VB. NET" Then l angType = " ver bose"


' Use: t o put t wo commands on same l i ne
I f x <> 100 And y < 5 Then x *= 5 : y *= 2


' Pr ef er r ed
I f x <> 100 And y < 5 Then
x *= 5
y *= 2
End I f




' or t o br eak up any l ong si ngl e command use _
I f henYouHaveAReal l y < l ongLi ne And _
i t NeedsToBeBr okenI nt o2 > Li nes Then _
UseTheUnder scor e( char ToBr eakI t Up)


I f x > 5 Then
x *= y
El seI f x = 5 Then
x += y
El seI f x < 10 Then
x - = y
El se
x / = y
End I f


' Must be a pr i mi t i ve dat a t ype
Sel ect Case col or
Case " bl ack" , " r ed"
r += 1
Case " bl ue"
b += 1
Case " gr een"
g += 1
Case El se
ot her += 1
End Sel ect
C#
gr eet i ng = age < 20 ? " What ' s up?" :
" Hel l o" ;











i f ( x ! = 100 && y < 5)
{
/ / Mul t i pl e st at ement s must be encl osed
i n {}
x *= 5;
y *= 2;
}








i f ( x > 5)
x *= y;
el se i f ( x == 5)
x += y;
el se i f ( x < 10)
x - = y;
el se
x / = y;



/ / Must be i nt eger or st r i ng
swi t ch ( col or )
{
case " bl ack" :
case " r ed" : r ++;
br eak;
case " bl ue"
br eak;
case " gr een" : g++;
br eak;
def aul t : ot her ++;
br eak;
}







Thi s document i s an aut hor i zed der i vat i ve of Fr ank McCown' s " VB. NET and
C# Compar i son" ( C) 2005 at
ht t p: / / www. har di ng. edu/ USER/ f mccown/ WWW/ vbnet _cshar p_compar i son. ht ml
Thi s wor k i s l i censed under a Cr eat e Common Li cense
Loops
VB.NET
' Pr e- t est Loops:
Whi l e c < 10
c += 1
End Whi l e Do Unt i l c = 10
c += 1
Loop


' Post - t est Loop:
Do Whi l e c < 10
c += 1
Loop


For c = 2 To 10 St ep 2
Syst em. Consol e. Wr i t eLi ne( c)
Next



' Ar r ay or col l ect i on l oopi ng
Di mnames As St r i ng( ) = {" St even" , " SuOk" , " Sar ah" }
For Each s As St r i ng I n names
Syst em. Consol e. Wr i t eLi ne( s)
Next
C#
/ / Pr e- t est Loops: whi l e ( i < 10)
i ++;
f or ( i = 2; i < = 10; i += 2)
Syst em. Consol e. Wr i t eLi ne( i ) ;






/ / Post - t est Loop:
do
i ++;
whi l e ( i < 10) ;








/ / Ar r ay or col l ect i on l oopi ng
st r i ng[ ] names = {" St even" , " SuOk" ,
" Sar ah" };
f or each ( st r i ng s i n names)
Syst em. Consol e. Wr i t eLi ne( s) ;
































Thi s document i s an aut hor i zed der i vat i ve of Fr ank McCown' s " VB. NET and
C# Compar i son" ( C) 2005 at
ht t p: / / www. har di ng. edu/ USER/ f mccown/ WWW/ vbnet _cshar p_compar i son. ht ml
Thi s wor k i s l i censed under a Cr eat e Common Li cense
Arrays
VB.NET
Di mnums( ) As I nt eger = {1, 2, 3}
For i As I nt eger = 0 To nums. Lengt h - 1
Consol e. Wr i t eLi ne( nums( i ) )
Next

' 4 i s t he i ndex of t he l ast el ement , so i t hol ds 5
el ement s
Di mnames( 4) As St r i ng
names( 0) = " St even"
' Thr ows Syst em. I ndexOut Of RangeExcept i on
names( 5) = " Sar ah"


' Resi ze t he ar r ay, keepi ng t he exi st i ng
' val ues ( Pr eser ve i s opt i onal )
ReDi mPr eser ve names( 6)





Di mt woD( r ows- 1, col s- 1) As Si ngl e
t woD( 2, 0) = 4. 5


Di mj agged( ) ( ) As I nt eger = { _
New I nt eger ( 4) {}, New I nt eger ( 1) {}, New
I nt eger ( 2) {} }
j agged( 0) ( 4) = 5
C#
i nt [ ] nums = {1, 2, 3};
f or ( i nt i = 0; i < nums. Lengt h; i ++)
Consol e. Wr i t eLi ne( nums[ i ] ) ;


/ / 5 i s t he si ze of t he ar r ay
st r i ng[ ] names = new st r i ng[ 5] ;
names[ 0] = " St even" ;
/ / Thr ows Syst em. I ndexOut Of RangeExcept i on
names[ 5] = " Sar ah"


/ / C# can' t dynami cal l y r esi ze an ar r ay.
/ / J ust copy i nt o new ar r ay.
st r i ng[ ] names2 = new st r i ng[ 7] ;
/ / or names. CopyTo( names2, 0) ;
Ar r ay. Copy( names, names2, names. Lengt h) ;



f l oat [ , ] t woD = new f l oat [ r ows, col s] ;
t woD[ 2, 0] = 4. 5;


i nt [ ] [ ] j agged = new i nt [ 3] [ ] {
new i nt [ 5] , new i nt [ 2] , new i nt [ 3] };
j agged[ 0] [ 4] = 5;
































Thi s document i s an aut hor i zed der i vat i ve of Fr ank McCown' s " VB. NET and
C# Compar i son" ( C) 2005 at
ht t p: / / www. har di ng. edu/ USER/ f mccown/ WWW/ vbnet _cshar p_compar i son. ht ml
Thi s wor k i s l i censed under a Cr eat e Common Li cense
Functions
VB.NET
' Pass by val ue ( i n, def aul t ) , r ef er ence
' ( i n/ out ) , and r ef er ence ( out )
Sub Test Func( ByVal x As I nt eger , ByRef y As
I nt eger ,
ByRef z As I nt eger )
x += 1
y += 1
z = 5
End Sub


' c set t o zer o by def aul t

Di ma = 1, b = 1, c As I nt eger
Test Func( a, b, c)
Syst em. Consol e. Wr i t eLi ne( " {0} {1} {2}" , a, b, c) ' 1
2 5


' Accept var i abl e number of ar gument s
Funct i on Sum( ByVal Par amAr r ay nums As I nt eger ( ) ) As
I nt eger
Sum= 0
For Each i As I nt eger I n nums
Sum+= i
Next
End Funct i on ' Or use a Ret ur n st at ement l i ke C#

Di mt ot al As I nt eger = Sum( 4, 3, 2, 1) ' r et ur ns 10


' Opt i onal par amet er s must be l i st ed l ast
' and must have a def aul t val ue
Sub SayHel l o( ByVal name As St r i ng,
Opt i onal ByVal pr ef i x As St r i ng = " " )
Syst em. Consol e. Wr i t eLi ne( " Gr eet i ngs, " & pr ef i x
& " " & name)
End Sub


SayHel l o( " St even" , " Dr . " )
SayHel l o( " SuOk" )

C#
/ / Pass by val ue ( i n, def aul t ) , r ef er ence
/ / ( i n/ out ) , and r ef er ence ( out )
voi d Test Func( i nt x, r ef i nt y, out i nt z)
{
x++;
y++;
z = 5;
}




i nt a = 1, b = 1, c; / / c doesn' t need
i ni t i al i zi ng
Test Func( a, r ef b, out c) ;
Syst em. Consol e. Wr i t eLi ne( " {0} {1} {2}" , a,
b, c) ; / / 1 2 5


/ / Accept var i abl e number of ar gument s
i nt Sum( par ams i nt [ ] nums) {
i nt sum= 0;
f or each ( i nt i i n nums)
sum+= i ;
r et ur n sum;
}


i nt t ot al = Sum( 4, 3, 2, 1) ; / / r et ur ns 10


/ * C# doesn' t suppor t opt i onal
ar gument s/ par amet er s.
J ust cr eat e t wo di f f er ent ver si ons of t he
same f unct i on. */
voi d SayHel l o( st r i ng name, st r i ng pr ef i x) {
Syst em. Consol e. Wr i t eLi ne( " Gr eet i ngs, " +
pr ef i x + " " + name) ;
}

voi d SayHel l o( st r i ng name) {
SayHel l o( name, " " ) ;
}
















Thi s document i s an aut hor i zed der i vat i ve of Fr ank McCown' s " VB. NET and
C# Compar i son" ( C) 2005 at
ht t p: / / www. har di ng. edu/ USER/ f mccown/ WWW/ vbnet _cshar p_compar i son. ht ml
Thi s wor k i s l i censed under a Cr eat e Common Li cense
Exception Handling
VB.NET
Cl ass Wi t hf i nal l y
Publ i c Shar ed Sub Mai n( )
Tr y
Di mx As I nt eger = 5
Di my As I nt eger = 0
Di mz As I nt eger = x / y
Consol e. Wr i t eLi ne( z)
Cat ch e As Di vi deByZer oExcept i on
Syst em. Consol e. Wr i t eLi ne( " Er r or occur r ed" )
Fi nal l y
Syst em. Consol e. Wr i t eLi ne( " Thank you" )
End Tr y
End Sub ' Mai n
End Cl ass ' Wi t hf i nal l y
C#
cl ass Wi t hf i nal l y
{
publ i c st at i c voi d Mai n( )
{
t r y
{
i nt x = 5;
i nt y = 0;
i nt z = x/ y;
Consol e. Wr i t eLi ne( z) ;
}
cat ch( Di vi deByZer oExcept i on e)
{
Syst em. Consol e. Wr i t eLi ne( " Er r or
occur r ed" ) ;
}
f i nal l y
{
Syst em. Consol e. Wr i t eLi ne( " Thank
you" ) ;
}
}
}
Namespaces
VB.NET
Namespace ASPAl l i ance. Dot Net . Communi t y
. . .
End Namespace


' or


Namespace ASPAl l i ance
Namespace Dot Net
Namespace Communi t y
. . .
End Namespace
End Namespace
End Namespace


I mpor t s ASPAl l i ance. Dot Net . Communi t y
C#
namespace ASPAl l i ance. Dot Net . Communi t y {
. . .
}


/ / or


namespace ASPAl l i ance {
namespace Dot Net {
namespace Communi t y {
. . .
}
}
}


usi ng ASPAl l i ance. Dot Net . Communi t y;












Thi s document i s an aut hor i zed der i vat i ve of Fr ank McCown' s " VB. NET and
C# Compar i son" ( C) 2005 at
ht t p: / / www. har di ng. edu/ USER/ f mccown/ WWW/ vbnet _cshar p_compar i son. ht ml
Thi s wor k i s l i censed under a Cr eat e Common Li cense
Classes / Interfaces
VB.NET
' Accessi bi l i t y keywor ds
Publ i c
Pr i vat e
Fr i end
Pr ot ect ed
Pr ot ect ed Fr i end
Shar ed


' I nher i t ance
Cl ass Ar t i cl es
I nher i t s Aut hor s
. . .
End Cl ass


I mpor t s Syst em


I nt er f ace I Ar t i cl e
Sub Show( )
End I nt er f ace ' I Ar t i cl e
_


Cl ass I Aut hor
I mpl ement s I Ar t i cl e

Publ i c Sub Show( )
Syst em. Consol e. Wr i t eLi ne( " Show( ) met hod
I mpl ement ed" )
End Sub ' Show

' Ent r y poi nt whi ch del egat es t o C- st yl e mai n
Pr i vat e Funct i on
Publ i c Over l oads Shar ed Sub Mai n( )
Mai n( Syst em. Envi r onment . Get CommandLi neAr gs( ) )
End Sub


Over l oads Publ i c Shar ed Sub Mai n( ar gs( ) As
St r i ng)
Di maut hor As New I Aut hor ( )
aut hor . Show( )
End Sub ' Mai n
End Cl ass ' I Aut hor
C#
/ / Accessi bi l i t y keywor ds
publ i c
pr i vat e
i nt er nal
pr ot ect ed
pr ot ect ed i nt er nal
st at i c


/ / I nher i t ance
cl ass Ar t i cl es: Aut hor s {
. . .
}


usi ng Syst em;


i nt er f ace I Ar t i cl e
{
voi d Show( ) ;
}


cl ass I Aut hor : I Ar t i cl e
{
publ i c voi d Show( )
{
Syst em. Consol e. Wr i t eLi ne( " Show( ) met hod
I mpl ement ed" ) ;
}


publ i c st at i c voi d Mai n( st r i ng[ ] ar gs)
{
I Aut hor aut hor = new I Aut hor ( ) ;
aut hor . Show( ) ;
}
}




















Thi s document i s an aut hor i zed der i vat i ve of Fr ank McCown' s " VB. NET and
C# Compar i son" ( C) 2005 at
ht t p: / / www. har di ng. edu/ USER/ f mccown/ WWW/ vbnet _cshar p_compar i son. ht ml
Thi s wor k i s l i censed under a Cr eat e Common Li cense
Constructors / Destructors
VB.NET
Cl ass TopAut hor
Pr i vat e _t opAut hor As I nt eger

Publ i c Sub New( )
_t opAut hor = 0
End Sub

Publ i c Sub New( ByVal t opAut hor As I nt eger )
Me. _t opAut hor = t opAut hor
End Sub

Pr ot ect ed Over r i des Sub Fi nal i ze( )
' Desct r uct or code t o f r ee unmanaged r esour ces
MyBase. Fi nal i ze( )
End Sub
End Cl ass
C#
cl ass TopAut hor {
pr i vat e i nt _t opAut hor ;

publ i c TopAut hor ( ) {
_t opAut hor = 0;
}

publ i c TopAut hor ( i nt t opAut hor ) {
t hi s. _t opAut hor = t opAut hor
}

~TopAut hor ( ) {
/ / Dest r uct or code t o f r ee unmanaged
r esour ces.
/ / I mpl i ci t l y cr eat es a Fi nal i ze met hod
}
}
Objects
VB.NET
Di maut hor As TopAut hor = New TopAut hor
Wi t h aut hor
. Name = " St even"
. Aut hor Ranki ng = 3
End Wi t h

aut hor . Rank( " Scot t " )
aut hor . Demot e( ) ' Cal l i ng Shar ed met hod
' or
TopAut hor . Rank( )


Di maut hor 2 As TopAut hor = aut hor ' Bot h r ef er t o
same obj ect
aut hor 2. Name = " J oe"
Syst em. Consol e. Wr i t eLi ne( aut hor 2. Name) ' Pr i nt s J oe


aut hor = Not hi ng ' Fr ee t he obj ect


I f aut hor I s Not hi ng Then _
aut hor = New TopAut hor


Di mobj As Obj ect = New TopAut hor
I f TypeOf obj I s TopAut hor Then _
Syst em. Consol e. Wr i t eLi ne( " I s a TopAut hor
obj ect . " )
C#
TopAut hor aut hor = new TopAut hor ( ) ;

/ / No " Wi t h" const r uct
aut hor . Name = " St even" ;
aut hor . Aut hor Ranki ng = 3;


aut hor . Rank( " Scot t " ) ;
TopAut hor . Demot e( ) / / Cal l i ng st at i c met hod




TopAut hor aut hor 2 = aut hor / / Bot h r ef er t o
same obj ect
aut hor 2. Name = " J oe" ;
Syst em. Consol e. Wr i t eLi ne( aut hor 2. Name)
/ / Pr i nt s J oe


aut hor = nul l / / Fr ee t he obj ect


i f ( aut hor == nul l )
aut hor = new TopAut hor ( ) ;


Obj ect obj = new TopAut hor ( ) ;
i f ( obj i s TopAut hor )
Syst Consol e. Wr i t eLi ne( " I s a TopAut hor
obj ect . " ) ;




Thi s document i s an aut hor i zed der i vat i ve of Fr ank McCown' s " VB. NET and
C# Compar i son" ( C) 2005 at
ht t p: / / www. har di ng. edu/ USER/ f mccown/ WWW/ vbnet _cshar p_compar i son. ht ml
Thi s wor k i s l i censed under a Cr eat e Common Li cense
Structs
VB.NET
St r uct ur e Aut hor Recor d
Publ i c name As St r i ng
Publ i c r ank As Si ngl e

Publ i c Sub New( ByVal name As St r i ng, ByVal r ank
As Si ngl e)
Me. name = name
Me. r ank = r ank
End Sub
End St r uct ur e


Di maut hor As Aut hor Recor d = New
Aut hor Recor d( " St even" , 8. 8)
Di maut hor 2 As Aut hor Recor d = aut hor

aut hor 2. name = " Scot t "
Syst em. Consol e. Wr i t eLi ne( aut hor . name) ' Pr i nt s
St even
Syst em. Consol e. Wr i t eLi ne( aut hor 2. name) ' Pr i nt s
Scot t
C#
st r uct Aut hor Recor d {
publ i c st r i ng name;
publ i c f l oat r ank;

publ i c Aut hor Recor d( st r i ng name, f l oat
r ank) {
t hi s. name = name;
t hi s. r ank = r ank;
}
}



Aut hor Recor d aut hor = new
Aut hor Recor d( " St even" , 8. 8) ;
Aut hor Recor d aut hor 2 = aut hor

aut hor . name = " Scot t " ;
Syst emConsol e. Wr i t eLi ne( aut hor . name) ;
/ / Pr i nt s St even
Syst em. Consol e. Wr i t eLi ne( aut hor 2. name) ;
/ / Pr i nt s Scot t





































Thi s document i s an aut hor i zed der i vat i ve of Fr ank McCown' s " VB. NET and
C# Compar i son" ( C) 2005 at
ht t p: / / www. har di ng. edu/ USER/ f mccown/ WWW/ vbnet _cshar p_compar i son. ht ml
Thi s wor k i s l i censed under a Cr eat e Common Li cense
Properties
VB.NET
Pr i vat e _si ze As I nt eger

Publ i c Pr oper t y Si ze( ) As I nt eger
Get
Ret ur n _si ze
End Get
Set ( ByVal Val ue As I nt eger )
I f Val ue < 0 Then
_si ze = 0
El se
_si ze = Val ue
End I f
End Set
End Pr oper t y


f oo. Si ze += 1


I mpor t s Syst em


Cl ass [ Dat e]

Publ i c Pr oper t y Day( ) As I nt eger
Get
Ret ur n day
End Get
Set
day = val ue
End Set
End Pr oper t y
Pr i vat e day As I nt eger


Publ i c Pr oper t y Mont h( ) As I nt eger
Get
Ret ur n mont h
End Get
Set
mont h = val ue
End Set
End Pr oper t y
Pr i vat e mont h As I nt eger


Publ i c Pr oper t y Year ( ) As I nt eger
Get
Ret ur n year
End Get
Set
year = val ue
End Set
End Pr oper t y
Pr i vat e year As I nt eger


Publ i c Funct i on I sLeapYear ( year As I nt eger ) As
Bool ean
C#
pr i vat e i nt _si ze;

publ i c i nt Si ze {
get {
r et ur n _si ze;
}
set {
i f ( val ue < 0)
_si ze = 0;
el se
_si ze = val ue;
}
}



f oo. Si ze++;


usi ng Syst em;
cl ass Dat e
{
publ i c i nt Day{
get {
r et ur n day;
}
set {
day = val ue;
}
}
i nt day;


publ i c i nt Mont h{
get {
r et ur n mont h;
}
set {
mont h = val ue;
}
}
i nt mont h;


publ i c i nt Year {
get {
r et ur n year ;
}
set {
year = val ue;
}
}
i nt year ;


publ i c bool I sLeapYear ( i nt year )
{
r et ur n year %4== 0 ? t r ue: f al se;
}
Thi s document i s an aut hor i zed der i vat i ve of Fr ank McCown' s " VB. NET and
C# Compar i son" ( C) 2005 at
ht t p: / / www. har di ng. edu/ USER/ f mccown/ WWW/ vbnet _cshar p_compar i son. ht ml
Thi s wor k i s l i censed under a Cr eat e Common Li cense
Ret ur n( I f year Mod 4 = 0 Then Tr ue El se
Fal se)
End Funct i on ' I sLeapYear

Publ i c Sub Set Dat e( day As I nt eger , mont h As
I nt eger ,
year As I nt eger )
Me. day = day
Me. mont h = mont h
Me. year = year
End Sub ' Set Dat e
End Cl ass ' [ Dat e]
publ i c voi d Set Dat e ( i nt day, i nt
mont h, i nt year )
{
t hi s. day = day;
t hi s. mont h = mont h;
t hi s. year = year ;
}
}
Delegates / Events
VB.NET
Del egat e Sub MsgAr r i vedEvent Handl er ( ByVal message
As St r i ng)


Event MsgAr r i vedEvent As MsgAr r i vedEvent Handl er


' or t o def i ne an event whi ch decl ar es a
' del egat e i mpl i ci t l y
Event MsgAr r i vedEvent ( ByVal message As St r i ng)


AddHandl er MsgAr r i vedEvent , Addr essOf
My_MsgAr r i vedCal l back
' Won' t t hr ow an except i on i f obj i s Not hi ng
Rai seEvent MsgAr r i vedEvent ( " Test message" )
RemoveHandl er MsgAr r i vedEvent , Addr essOf
My_MsgAr r i vedCal l back



I mpor t s Syst em. Wi ndows. For ms


' Wi t hEvent s can' t be used on l ocal var i abl e
Di mWi t hEvent s MyBut t on As But t on
MyBut t on = New But t on


Pr i vat e Sub MyBut t on_Cl i ck( ByVal sender As
Syst em. Obj ect , _
ByVal e As Syst em. Event Ar gs) Handl es
MyBut t on. Cl i ck
MessageBox. Show( Me, " But t on was cl i cked" , " I nf o" ,
_
MessageBoxBut t ons. OK,
MessageBoxI con. I nf or mat i on)
End Sub
C#
del egat e voi d MsgAr r i vedEvent Handl er ( st r i ng
message) ;


event MsgAr r i vedEvent Handl er
MsgAr r i vedEvent ;


/ / Del egat es must be used wi t h event s i n C#


MsgAr r i vedEvent += new
MsgAr r i vedEvent Handl er
( My_MsgAr r i vedEvent Cal l back) ;
/ / Thr ows except i on i f obj i s nul l
MsgAr r i vedEvent ( " Test message" ) ;
MsgAr r i vedEvent - = new
MsgAr r i vedEvent Handl er
( My_MsgAr r i vedEvent Cal l back) ;



usi ng Syst em. Wi ndows. For ms;


But t on MyBut t on = new But t on( ) ;
MyBut t on. Cl i ck += new
Syst em. Event Handl er ( MyBut t on_Cl i ck) ;


pr i vat e voi d MyBut t on_Cl i ck( obj ect sender ,
Syst em. Event Ar gs e) {
MessageBox. Show( t hi s, " But t on was
cl i cked" , " I nf o" ,
MessageBoxBut t ons. OK,
MessageBoxI con. I nf or mat i on) ;
}









Thi s document i s an aut hor i zed der i vat i ve of Fr ank McCown' s " VB. NET and
C# Compar i son" ( C) 2005 at
ht t p: / / www. har di ng. edu/ USER/ f mccown/ WWW/ vbnet _cshar p_compar i son. ht ml
Thi s wor k i s l i censed under a Cr eat e Common Li cense

Console I/O
VB.NET
' Speci al char act er const ant s
vbCr Lf , vbCr , vbLf , vbNewLi ne
vbNul l St r i ng
vbTab
vbBack
vbFor mFeed
vbVer t i cal Tab
" "
Chr ( 65) ' Ret ur ns ' A'


Syst em. Consol e. Wr i t e( " What ' s your name? " )
Di mname As St r i ng = Syst em. Consol e. ReadLi ne( )
Syst em. Consol e. Wr i t e( " How ol d ar e you? " )
Di mage As I nt eger = Val ( Syst em. Consol e. ReadLi ne( ) )
Syst em. Consol e. Wr i t eLi ne( " {0} i s {1} year s ol d. " ,
name, age)
' or
Syst em. Consol e. Wr i t eLi ne( name & " i s " & age & "
year s ol d. " )

Di mc As I nt eger
c = Syst em. Consol e. Read( ) ' Read si ngl e char
Syst em. Consol e. Wr i t eLi ne( c) ' Pr i nt s 65 i f user
ent er s " A"
C#
/ / Escape sequences
\ n, \ r
\ t
\ \
\


Conver t . ToChar ( 65) / / Ret ur ns ' A' -
equi val ent t o Chr ( num) i n VB
/ / or
( char ) 65


Syst em. Consol e. Wr i t e( " What ' s your name? " ) ;
st r i ng name = SYst em. Consol e. ReadLi ne( ) ;
Syst em. Consol e. Wr i t e( " How ol d ar e you? " ) ;
i nt age =
Conver t . ToI nt 32( Syst em. Consol e. ReadLi ne( ) ) ;
Syst em. Consol e. Wr i t eLi ne( " {0} i s {1} year s
ol d. " , name, age) ;
/ / or
Syst em. Consol e. Wr i t eLi ne( name + " i s " +
age + " year s ol d. " ) ;


i nt c = Syst em. Consol e. Read( ) ; / / Read
si ngl e char
Syst em. Consol e. Wr i t eLi ne( c) ; / / Pr i nt s 65 i f
user ent er s " A"




























Thi s document i s an aut hor i zed der i vat i ve of Fr ank McCown' s " VB. NET and
C# Compar i son" ( C) 2005 at
ht t p: / / www. har di ng. edu/ USER/ f mccown/ WWW/ vbnet _cshar p_compar i son. ht ml
Thi s wor k i s l i censed under a Cr eat e Common Li cense


File I/O
VB.NET
I mpor t s Syst em. I O


' Wr i t e out t o t ext f i l e
Di mwr i t er As St r eamWr i t er = Fi l e. Cr eat eText
( " c: \ myf i l e. t xt " )
wr i t er . Wr i t eLi ne( " Out t o f i l e. " )
wr i t er . Cl ose( )


' Read al l l i nes f r omt ext f i l e
Di mr eader As St r eamReader = Fi l e. OpenText
( " c: \ myf i l e. t xt " )
Di ml i ne As St r i ng = r eader . ReadLi ne( )
Whi l e Not l i ne I s Not hi ng
Consol e. Wr i t eLi ne( l i ne)
l i ne = r eader . ReadLi ne( )
End Whi l e
r eader . Cl ose( )


' Wr i t e out t o bi nar y f i l e
Di mst r As St r i ng = " Text dat a"
Di mnumAs I nt eger = 123
Di mbi nWr i t er As New Bi nar yWr i t er ( Fi l e. OpenWr i t e
( " c: \ myf i l e. dat " ) )
bi nWr i t er . Wr i t e( st r )
bi nWr i t er . Wr i t e( num)
bi nWr i t er . Cl ose( )


' Read f r ombi nar y f i l e
Di mbi nReader As New Bi nar yReader ( Fi l e. OpenRead
( " c: \ myf i l e. dat " ) )
st r = bi nReader . ReadSt r i ng( )
num= bi nReader . ReadI nt 32( )
bi nReader . Cl ose( )
C#
usi ng Syst em. I O;


/ / Wr i t e out t o t ext f i l e
St r eamWr i t er wr i t er = Fi l e. Cr eat eText
( " c: \ \ myf i l e. t xt " ) ;
wr i t er . Wr i t eLi ne( " Out t o f i l e. " ) ;
wr i t er . Cl ose( ) ;


/ / Read al l l i nes f r omt ext f i l e
St r eamReader r eader = Fi l e. OpenText
( " c: \ \ myf i l e. t xt " ) ;
st r i ng l i ne = r eader . ReadLi ne( ) ;
whi l e ( l i ne ! = nul l ) {
Consol e. Wr i t eLi ne( l i ne) ;
l i ne = r eader . ReadLi ne( ) ;
}
r eader . Cl ose( ) ;


/ / Wr i t e out t o bi nar y f i l e
st r i ng st r = " Text dat a" ;
i nt num= 123;
Bi nar yWr i t er bi nWr i t er = new
Bi nar yWr i t er ( Fi l e. OpenWr i t e
( " c: \ \ myf i l e. dat " ) ) ;
bi nWr i t er . Wr i t e( st r ) ;
bi nWr i t er . Wr i t e( num) ;
bi nWr i t er . Cl ose( ) ;


/ / Read f r ombi nar y f i l e
Bi nar yReader bi nReader = new
Bi nar yReader ( Fi l e. OpenRead
( " c: \ \ myf i l e. dat " ) ) ;
st r = bi nReader . ReadSt r i ng( ) ;
num= bi nReader . ReadI nt 32( ) ;
bi nReader . Cl ose( ) ;

You might also like