|
-
Apr 18th, 2006, 03:55 PM
#1
[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:
Dim m As String = Request.QueryString("month")
Dim y As String = Request.QueryString("year")
If m Is Nothing Then m = Now.Month.ToString()
If y Is Nothing Then y = Now.Year.ToString()
Dim dt As New Date(y, m, 1)
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:
string m = Request.QueryString["month"];
string y = Request.QueryString["year"];
if (m == null)
m = DateTime.Now.Month.ToString();
if (y == null)
y = DateTime.Now.Year.ToString();
DateTime dt = new DateTime(y,m,1); <--- This is where I get the error!!!
litTitle.Text = dt.ToString("MMMM yyy") + " Entries";
Cheers
Gary
-
Apr 18th, 2006, 06:59 PM
#2
Fanatic Member
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);
-
Apr 18th, 2006, 07:24 PM
#3
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.
-
Apr 19th, 2006, 02:08 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|