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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
AlanRank #669 · 3.6K reputationPosted Jun 16, 2007, 8:58 AM EDT
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.
Rajeev Ranjan9 reputationPosted Jun 16, 2007, 9:12 AM EDT
But the code you repplied is not working
AlanRank #669 · 3.6K reputationPosted Jun 16, 2007, 9:22 AM EDT
string UName;
string thisName;
thisName = "" + UName + "";
Rajeev Ranjan9 reputationPosted Jun 16, 2007, 10:54 AM EDT
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
AlanRank #669 · 3.6K reputationPosted Jun 16, 2007, 11:34 AM EDT
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 Ranjan9 reputationPosted Jun 16, 2007, 11:57 AM EDT
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
AlanRank #669 · 3.6K reputationPosted Jun 16, 2007, 12:57 PM EDT
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.