locked
C# Equivalent of "To" statement, "Friend with events" and "Stopwatch" in VB.NET RRS feed

  • Question

  • Hello everyone.

     

    I have three questions.

     

    I would like to now what the equivalent of this code would be in C#

    For FirstChar = 0 To sw.Length - 1

    Secondly I would like to know what the C# version of Friend with events is.

     

    And lastly I would like to know if C# has a Stopwatch object like VB.NET.

     

    Thanks in advance.

     

    Jai Brown.

     

    Monday, August 16, 2010 6:37 AM

Answers

  • 1) You would write...

    for (int FirstChar = 0; FirstChar < sw.Length; FirstChar++)
    {
     // Code here
    }
    

    2) The "Friend" keyword in VB is "internal" in C#. There is no WithEvents in C#, you have to manually add the event handlers (or use Form designer to do it for you)

    3) The Stopwatch object is part of the DotNet framework and thus available to all languages who use the framework such as VB and C#

    Kev

    • Proposed as answer by DK. Da Monday, August 16, 2010 6:54 AM
    • Marked as answer by Shadow_69_91 Monday, August 16, 2010 7:53 AM
    • Edited by mrkevlar Monday, August 16, 2010 8:25 AM Removed the "-1" from the for loop
    Monday, August 16, 2010 6:42 AM
  • C# has an equivalent for()  statement.  C# doesn't  have a direct equivalent for WithEvents.  Neither VB nor C# has a Stopwatch object.  The Stopwatch is a .NET class in the System.Diagnostics namespace.

    Find a VB to C# converter and your help files. If you use VS2008 or earlier, downloading local help is very worthwhile.

    • Marked as answer by Shadow_69_91 Monday, August 16, 2010 7:53 AM
    Monday, August 16, 2010 6:43 AM
  • For FirstChar = 0 To sw.Length - 1

    in C# for(int FirstChar =0; FirstChar <=sw.Length-1;FirstChar ++) see here for details

    C# version of Friend is internal 

    C# also has StopWatch class

    please follow the attached links with keywords to know more details


    Manish Sati
    • Marked as answer by Shadow_69_91 Monday, August 16, 2010 7:53 AM
    Monday, August 16, 2010 6:47 AM

All replies

  • 1) You would write...

    for (int FirstChar = 0; FirstChar < sw.Length; FirstChar++)
    {
     // Code here
    }
    

    2) The "Friend" keyword in VB is "internal" in C#. There is no WithEvents in C#, you have to manually add the event handlers (or use Form designer to do it for you)

    3) The Stopwatch object is part of the DotNet framework and thus available to all languages who use the framework such as VB and C#

    Kev

    • Proposed as answer by DK. Da Monday, August 16, 2010 6:54 AM
    • Marked as answer by Shadow_69_91 Monday, August 16, 2010 7:53 AM
    • Edited by mrkevlar Monday, August 16, 2010 8:25 AM Removed the "-1" from the for loop
    Monday, August 16, 2010 6:42 AM
  • C# has an equivalent for()  statement.  C# doesn't  have a direct equivalent for WithEvents.  Neither VB nor C# has a Stopwatch object.  The Stopwatch is a .NET class in the System.Diagnostics namespace.

    Find a VB to C# converter and your help files. If you use VS2008 or earlier, downloading local help is very worthwhile.

    • Marked as answer by Shadow_69_91 Monday, August 16, 2010 7:53 AM
    Monday, August 16, 2010 6:43 AM
  • For FirstChar = 0 To sw.Length - 1

    in C# for(int FirstChar =0; FirstChar <=sw.Length-1;FirstChar ++) see here for details

    C# version of Friend is internal 

    C# also has StopWatch class

    please follow the attached links with keywords to know more details


    Manish Sati
    • Marked as answer by Shadow_69_91 Monday, August 16, 2010 7:53 AM
    Monday, August 16, 2010 6:47 AM
  • Thank you every one that helped a lot.

     

    One more problem concerning the Stopwatch.


    When I use the following code I get an error.

    if (MyStopwatch.Elapsed.ToString.Contains(".")
    {
      // Code goes here
    }
    

    This is what the error says.

     

    'System.TimeSpan.ToString(string, System.IFormatProvider)' is a 'method', which is not valid in the given context

     

    But I need to do it so I can have a label that counts the time it takes for my application to work.

     

    Thanks.

     

    Monday, August 16, 2010 7:57 AM
  • because ToString() is a method but used here as a property.try the code below
    if (MyStopwatch.Elapsed.ToString().Contains(".")
    {
     // Code goes here
    }
    
    

    Manish Sati
    Monday, August 16, 2010 8:16 AM
  • You can get away with omitting the brackets on methods in VB but not in C#
    Monday, August 16, 2010 8:24 AM