Coffeehouse Thread

26 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Learning VB.NET

Back to Forum: Coffeehouse
  • User profile image
    Sven Groot

    SlackmasterK wrote:
    I tried for dim b as Integer = 1 to 10, that didn't work

    Almost there. Smiley

    For b As Integer = 1 To 10
       ' Stuff
    Next

    That works. This feature was introduced in VS2003, it won't work with 2002.

    EDIT: As a FYI, you can also do this with For Each: For Each x As String In someCollection

  • User profile image
    Sven Groot

    SlackmasterK wrote:
    Isn't VB's 'my' namespace similar to C#'s 'this'?

    No, Me is VB's equivalent to this. My is a utility namespace and was introduced in VB2005. It contains the stuff that C# puts in the Properties namespace (so, settings and resources) but also a lot of other stuff designed to make common tasks easier.

  • User profile image
    phreaks

    EDIT: something is wrong with the quote engine:

    Encapusalating your methods into components of Single Responsability will ensure that the GC flags them as unreachable and marks them for deallocation from the stack in a timely manner.

    Again, at least that is my understanding of it, if I am undertsanding your question properly.

    Please fill in any missing parts or add corrections.

    EDIT2: WTF, now half my response is gone.
    Is something wrong with C9?

  • User profile image
    Sven Groot

    H4L0PR1CK wrote:
    EDIT: something is wrong with the quote engine:

    No it isn't. Both you and SlackmasterK had a [/quote] too many in your posts.

    H4L0PR1CK wrote:
    Encapusalating your methods into components of Single Responsability will ensure that the GC flags them as unreachable and marks them for deallocation from the stack in a timely manner.

    Again, at least that is my understanding of it, if I am undertsanding your question properly.

    Please fill in any missing parts or add corrections.

    Using the for( int x = 0; x < 10; ++x ) method in C# (or the For x As Integer = 0 To 9 method in VB) doesn't help with variable lifetime. Like in C(++), using this construct means that the variable is out of scope outside the loop body, but .Net does not support C(++)'s destruction semantics for arbitrary scopes, which means the variable's lifetime will end at the end of the method, regardless of the scope.

  • User profile image
    phreaks

    Sven Groot wrote:
    
    H4L0PR1CK wrote:
    EDIT: something is wrong with the quote engine:

    No it isn't. Both you and SlackmasterK had a too many in your posts.

    Ahh, that was the problem.

    H4L0PR1CK wrote:
    Encapusalating your methods into components of Single Responsability will ensure that the GC flags them as unreachable and marks them for deallocation from the stack in a timely manner.

    Again, at least that is my understanding of it, if I am undertsanding your question properly.

    Please fill in any missing parts or add corrections.

    Using the for( int x = 0; x < 10; ++x ) method in C# (or the For x As Integer = 0 To 9 method in VB) doesn't help with variable lifetime. Like in C(++), using this construct means that the variable is out of scope outside the loop body, but .Net does not support C(++)'s destruction semantics for arbitrary scopes, which means the variable's lifetime will end at the end of the method, regardless of the scope.



    No kidding? Thanks for the insight.
    I always thought it was the same.

  • User profile image
    Sampy

    You can see this if you look at the il code. All the locals are declared at the top of the method body even if they exist in loop contexts.

    I believe the C++ did some work to make code they generate more C++-y so these rules may work in that language. That's hearsay though.

  • User profile image
    Sven Groot

    Sampy wrote:
    I believe the C++ did some work to make code they generate more C++-y so these rules may work in that language. That's hearsay though.

    That's partially correct. If you use the deterministic finalization pattern in an arbitrary scope, it generates a try block to make sure the Dispose method is called at the end of the scope (similar to a using statement in C# or VB). However, just like in C# or VB the object can't actually be collected until the method ends.

    For instance:
    int main()
    {
       Console::WriteLine(L
    "Before"
    );
       {
          System::IO::StreamWriter writer(L
    "D:\test.txt"
    );
          writer.WriteLine(L
    "Hello"
    );
       }
       Console::WriteLine(L
    "After"
    );
       return
    0;
    }


    This is compiled to the following IL when using /clr:safe:

    .method assembly static int32 main() cil managed
    {
       .entrypoint
       // Code size 65 (0x41)
       .maxstack 2
       .locals init ([0] class [mscorlib]System.IO.StreamWriter writer,
          [1] int32 V_1,
          [2] class [mscorlib]System.IO.StreamWriter modopt([mscorlib]System.Runtime.CompilerServices.IsConst) V_2)
       IL_0000: ldc.i4.0
       IL_0001: stloc.1
       IL_0002: ldstr "Before"
       IL_0007: call void [mscorlib]System.Console::WriteLine(string)
       IL_000c: ldstr "D:\test.txt"
       IL_0011: newobj instance void [mscorlib]System.IO.StreamWriter::.ctor(string)
       IL_0016: stloc.2
       .try
       {
          IL_0017: ldloc.2
          IL_0018: stloc.0
          IL_0019: ldloc.0
          IL_001a: ldstr "Hello"
          IL_001f: callvirt instance void [mscorlib]System.IO.TextWriter::WriteLine(string)
          IL_0024: leave.s IL_002d
       } // end .try
       fault
       {
          IL_0026: ldloc.0
          IL_0027: callvirt instance void [mscorlib]System.IDisposable::Dispose()
          
    IL_002c: endfinally
       } // end handler
       IL_002d: ldloc.0
       IL_002e: callvirt instance void [mscorlib]System.IDisposable::Dispose()
       IL_0033: ldstr "After"
       IL_0038: call void [mscorlib]System.Console::WriteLine(string)
       IL_003d: ldc.i4.0
       IL_003e: stloc.1
       IL_003f: ldloc.1
       IL_0040: ret
    } // end of method 'Global Functions'::main

    What confuses me though is why they don't use try/finally, but instead put a call to Dispose in a fault clause and again in the normal flow of execution.

  • User profile image
    bhar

    Hi,

    You can also rely on good books. Choosing VB.net or C# is always  debatable.

    TRy this Title:"Database programming using VB.net and Sql server 2000".  This book teaches you the complete database application development including masters, transactions and reports.
    Visit http://www.vkinfotek.com.

    Regards
    bhar

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.