Results 1 to 4 of 4

Thread: [RESOLVED] [2.0] Convert VB.Net to C#

  1. #1

    Thread Starter
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,966

    Resolved [RESOLVED] [2.0] Convert VB.Net to C#

    Hello

    I'm afraid I am back again with more questions regarding converting VB.Net to C# in a ASP.Net application I am making.

    Trying to convert the following:

    VB Code:
    1. Dim m As String = Request.QueryString("month")
    2.         Dim y As String = Request.QueryString("year")
    3.  
    4.         If m Is Nothing Then m = Now.Month.ToString()
    5.         If y Is Nothing Then y = Now.Year.ToString()
    6.  
    7.         Dim dt As New Date(y, m, 1)
    8.         litTitle.Text = dt.ToString("MMMM yyy")

    I can convert everything to C# without any problem, however, when trying to use the New Date, the equivalent of which in C# I have found to be new DateTime, it only excepts int as pararmeters to pass to the constructor, not string, and I cannot find a way to convert from a string to an int!

    Any thoughts?

    Here is what I have so far:

    VB Code:
    1. string m = Request.QueryString["month"];
    2.         string y = Request.QueryString["year"];
    3.  
    4.         if (m == null)
    5.             m = DateTime.Now.Month.ToString();
    6.         if (y == null)
    7.             y = DateTime.Now.Year.ToString();
    8.  
    9.         DateTime dt = new DateTime(y,m,1);    <--- This is where I get the error!!!
    10.         litTitle.Text = dt.ToString("MMMM yyy") + " Entries";

    Cheers

    Gary

  2. #2
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: [2.0] Convert VB.Net to C#

    There is no overloading for a DateTime to receive string, string, int. You can convert it to integer, however.

    new DateTime(Convert.ToInt32(y), Convert.ToInt32(m), 1);

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,477

    Re: [2.0] Convert VB.Net to C#

    The DateTime structure is a .NET type. Date is an in-built VB.NET data type that is implemented using the DateTime structure. They are two ways of referring to exactly the same thing, just like Int32 and Integer.

    VB.NET is more lax than C# and will do certain things for you implicitly that C# requires you to do explicitly. The fact that that VB code worked at all shows that you have Option Strict turned Off, which is unfortunately the default. I strongly recommend that every VB.NET developer turn Option Strict On, which will force you to make the majority of conversions explicitly. With Option Strict On that VB code would not compile. This is a good thing as it forces you to have a better understanding of what your code is doing, p;us it makes your code more efficient at run time and less prone to run time errors.

  4. #4

    Thread Starter
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,966

    Re: [2.0] Convert VB.Net to C#

    Hello

    Thanks for the reply!

    I will do what you suggested, and I will make sure I have Option Strict On from now on!

    Thanks

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width