to VB or Not to VB that is the question..
Hello all....
Well I must say I am a beginner in programming.... I tried to learn C++ (Ver6) but I just kept getting discouraged... But now that the internet has grown and there are more and more places to ask questions.. I have one for you....
after giving up hopes on teaching myself C++ i moved on to VB6... I gained enough knowledge to become dangerous but still not a guru...
Now with the addition of .net I fell like I am relearning everything all over again... So, my question to you is... Is this the time to dive right in and learn C# or should I stick to the basics.... (no pun intended).. What are the pros/cons.... and just how limited am I using VB?
Your input will help decide my future.. Thanks in Advance,
Anjari
you guys got me on the "with" thing
This is really interesting. Not that it would ever be enough motivation to switch back to VB, but the with thing does save quite a few steps in the IL. Here's an example that I tried to write identically in VB / C#. Following that is the disassembly for both. You will notice that VB is about 10 lines shorter...
VB
VB Code:
Sub Main()
Dim MyHash As Hashtable = New Hashtable(10)
MyHash.Add("TestKey", "TestValue")
With CStr(MyHash("TestKey"))
Console.WriteLine(.Length)
Console.WriteLine(.ToString())
Console.WriteLine(.GetHashCode())
End With
End Sub
C#
PHP Code:
static void Main(string[] args)
{
Hashtable MyHash = new Hashtable(10);
MyHash.Add("TestKey","TestValue");
Console.WriteLine( ((string)MyHash["TestKey"]).Length );
Console.WriteLine( ((string)MyHash["TestKey"]).ToString() );
Console.WriteLine( ((string)MyHash["TestKey"]).GetHashCode() );
}
VB IL
Code:
.method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
// Code size 83 (0x53)
.maxstack 3
.locals init ([0] class [mscorlib]System.Collections.Hashtable MyHash,
[1] string _Vb_t_string_0)
IL_0000: nop
IL_0001: ldc.i4.s 10
IL_0003: newobj instance void [mscorlib]System.Collections.Hashtable::.ctor(int32)
IL_0008: stloc.0
IL_0009: ldloc.0
IL_000a: ldstr "TestKey"
IL_000f: ldstr "TestValue"
IL_0014: callvirt instance void [mscorlib]System.Collections.Hashtable::Add(object,
object)
IL_0019: nop
IL_001a: ldloc.0
IL_001b: ldstr "TestKey"
IL_0020: callvirt instance object [mscorlib]System.Collections.Hashtable::get_Item(object)
IL_0025: call string [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StringType::FromObject(object)
IL_002a: stloc.1
IL_002b: ldloc.1
IL_002c: callvirt instance int32 [mscorlib]System.String::get_Length()
IL_0031: call void [mscorlib]System.Console::WriteLine(int32)
IL_0036: nop
IL_0037: ldloc.1
IL_0038: callvirt instance string [mscorlib]System.String::ToString()
IL_003d: call void [mscorlib]System.Console::WriteLine(string)
IL_0042: nop
IL_0043: ldloc.1
IL_0044: callvirt instance int32 [mscorlib]System.String::GetHashCode()
IL_0049: call void [mscorlib]System.Console::WriteLine(int32)
IL_004e: nop
IL_004f: ldnull
IL_0050: stloc.1
IL_0051: nop
IL_0052: ret
} // end of method Module1::Main
C# IL
Code:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
// Code size 103 (0x67)
.maxstack 3
.locals init ([0] class [mscorlib]System.Collections.Hashtable MyHash)
IL_0000: ldc.i4.s 10
IL_0002: newobj instance void [mscorlib]System.Collections.Hashtable::.ctor(int32)
IL_0007: stloc.0
IL_0008: ldloc.0
IL_0009: ldstr "TestKey"
IL_000e: ldstr "TestValue"
IL_0013: callvirt instance void [mscorlib]System.Collections.Hashtable::Add(object,
object)
IL_0018: ldloc.0
IL_0019: ldstr "TestKey"
IL_001e: callvirt instance object [mscorlib]System.Collections.Hashtable::get_Item(object)
IL_0023: castclass [mscorlib]System.String
IL_0028: callvirt instance int32 [mscorlib]System.String::get_Length()
IL_002d: call void [mscorlib]System.Console::WriteLine(int32)
IL_0032: ldloc.0
IL_0033: ldstr "TestKey"
IL_0038: callvirt instance object [mscorlib]System.Collections.Hashtable::get_Item(object)
IL_003d: castclass [mscorlib]System.String
IL_0042: callvirt instance string [mscorlib]System.String::ToString()
IL_0047: call void [mscorlib]System.Console::WriteLine(string)
IL_004c: ldloc.0
IL_004d: ldstr "TestKey"
IL_0052: callvirt instance object [mscorlib]System.Collections.Hashtable::get_Item(object)
IL_0057: castclass [mscorlib]System.String
IL_005c: callvirt instance int32 [mscorlib]System.String::GetHashCode()
IL_0061: call void [mscorlib]System.Console::WriteLine(int32)
IL_0066: ret
} // end of method Class1::Main
...interesting
(sorry for the long post :D )