I have this code for VB.NET
Dim UName As String
Dim thisName As String
thisName = "" & UName & ""
But I want it in C#. Plz solve this problem. Its urgent
Thanks
I have this code for VB.NET
Dim UName As String
Dim thisName As String
thisName = "" & UName & ""
But I want it in C#. Plz solve this problem. Its urgent
Thanks
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
AlanPosted Jun 16, 2007, 4:57 PM
This is a literal translation which I've done by hand as I don't have any translation software. Notice that the VB.Net function doesn't have a return type and is therefore Object by default even though a String is actually returned here:
public object getLastPosterByTopic(int topicId)
{
SqlConnection myConnection = new SqlConnection(ConfigurationManager.AppSettings["forumDSN"]);
SqlCommand myCommand = new SqlCommand("SELECT TOP 1 psId, psRelTopId, psName FROM forumThreads WHERE psRelBcId = " + topicId.ToString() + " ORDER BY psDate DESC", myConnection);
myConnection.Open();
SqlDataReader myReader;
myReader = myCommand.ExecuteReader();
int thisId = 0;
int thisTopId = 0;
string thisName = "";
while (myReader.Read())
{
thisId = (int)myReader["psId"];
thisTopId = (int)myReader["psRelTopId"];
thisName = (string)myReader["psName"];
}
myReader.Close();
myConnection.Close();
if (thisName == "")
{
thisName = "New!";
}
else
{
if (thisTopId != 0)
{
thisName = "" + thisName.ToString() + "";
}
else
{
thisName = "" + thisName.ToString() + "";
}
}
return thisName;
}
Judging by this, it looks to me as though the error previously was that UserID was an int, not a string, which was not apparent from the code snippet.
Rajeev RanjanPosted Jun 16, 2007, 3:57 PM
But here not working.
This is the function in VB.NET, I want it to be converted into C#.
Public Function getLastPosterByTopic(ByVal topicId As Integer)
Dim myConnection As New SqlConnection(ConfigurationManager.AppSettings("forumDSN"))
Dim myCommand As New SqlCommand("SELECT TOP 1 psId, psRelTopId, psName FROM forumThreads WHERE psRelBcId = " & topicId & " ORDER BY psDate DESC", myConnection)
myConnection.Open()
Dim myReader As SqlDataReader
myReader = myCommand.ExecuteReader()
Dim thisId As Integer
Dim thisTopId As Integer
Dim thisName As String
While myReader.Read()
thisId = myReader("psId")
thisTopId = myReader("psRelTopId")
thisName = myReader("psName")
End While
myReader.Close()
myConnection.Close()
If thisName = "" Then
thisName = "New!"
Else
If thisTopId <> 0 Then
thisName = "" & thisName & ""
Else
thisName = "" & thisName & ""
End If
End If
Return thisName
End Function
AlanPosted Jun 16, 2007, 3:34 PM
Well, there's nothing nothing wrong with the translation to C#.
When I ran this program in VB.Net:
Imports System
Imports System.Windows.Forms
Class Test
Shared Sub Main()
Dim UName As String = "Alan"
Dim thisName As String
Dim UserID As String = "1"
thisName = "" & UName & ""
MessageBox.Show(thisName)
End Sub
End Class
and then this program in C#:
using System;
using System.Windows.Forms;
class Test
{
static void Main()
{
string UName = "Alan";
string thisName;
string UserId = "1";
thisName = thisName = "" + UName + "";
MessageBox.Show(thisName);
}
}
The result was exactly the same, namely:
Alan
Rajeev RanjanPosted Jun 16, 2007, 2:54 PM
I have this code for VB.NET
Dim UName As String
Dim thisName As String
thisName = "" & UName & ""
This works in vb.net perfectly.
But I want it in C#. Plz solve this problem. Its urgent
Thanks
AlanPosted Jun 16, 2007, 1:22 PM
string UName;
string thisName;
thisName = "" + UName + "";
Rajeev RanjanPosted Jun 16, 2007, 1:12 PM
But the code you repplied is not working
AlanPosted Jun 16, 2007, 12:58 PM
string UName;
string thisName;
thisName = "" + UName + "";
However, UName is going to have to be assigned something (it's null by default in C# or Nothing in VB.Net) for this to work.