Results 1 to 9 of 9

Thread: Convert Code to C# ?

  1. #1

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Question Convert Code to C# ?

    I know how to convert code in C# to VB.NET. But in this one small instance, i want to convert code from VB.NET into C#.

    Does anyone know of a utility/webpage that does this? It's not a lot of code. Only maybe 10 lines or so.
    ~Peter


  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Post the code here, we can all chip in
    Dont gain the world and lose your soul

  3. #3

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Post

    I can figure out the declaration of the string variables, the message box, the changes to the try-catch, and even the trailing semi colan.

    But it's the XML objects and System variables i don't know how to change:
    VB Code:
    1. Dim sAppPath As String = System.Environment.CurrentDirectory
    2.         Dim sFilename As String = sAppPath & "SampleFile.XML"
    3.         Dim oXML As System.Xml.XmlDataDocument = New XmlDataDocument()
    4.         oXML.Load(sFilename)
    5.         Dim sTmp As String = ""
    6.         Try
    7.             sTmp = oXML.SelectSingleNode("//AppUpdateURL").InnerText
    8.         Catch
    9.             'An error here might indicate that there is no node with that given name
    10.         End Try
    11.         MsgBox("the URL is: " & sTmp)
    12.         oXML = Nothing
    That's why i was looking for a way to convert it; so i can see what the different syntax is.
    ~Peter


  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    example
    Dim oXML As System.Xml.XmlDataDocument = New XmlDataDocument()



    =

    System.Xml.XmlDataDocument oXML = new XmlDataDocument();
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    This is the converted version . It should works fine .
    Code:
    public void C_Code(){
    		string sAppPath =System.Environment.CurrentDirectory;
    		string sFilename =sAppPath + "SampleFile.XML";
    		System.Xml.XmlDataDocument oXML = new System.Xml.XmlDataDocument();
    		oXML.Load(sFilename);
    		string sTmp =null;
    
    					try {
    						sTmp = oXML.SelectSingleNode("//AppUpdateURL").InnerText;
    					}
    					catch (System.Exception x) {
    						MessageBox.Show (x.Message);
    					}
    		MessageBox.Show ("the URL is: " + sTmp);
    		oXML = null;
    
    }

  6. #6

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277
    I had to add a \\ before the name of the file because C# doesn't have the trailing backslash like VB.NET does. Other than that, all that code helps. It works.

    I have found a major flaw in my code. - The program crashes hard if the XML is NOT there.

    In VB.NET i'd just use the Dir function to verify that the file/path was valid. I'll have to see if there is a translation for the Dir function in C#.
    ~Peter


  7. #7

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up

    Actually,... if i just re-arrange the order of the items to have the .Load inside the TryCatch, i can avoid the error.

    So that works fine then. Thanks.
    ~Peter


  8. #8
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Here is a trick you can use to eliminate the \\'s
    You way now:
    ("\\AppUpdateURL")
    Another way you can do it:
    (@"\AppUpdateURL")

    The @ sign in front of a string makes C# count it as a literal string, and tells it that there is no backslash constants in the string.

  9. #9

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up

    I was wondering what that was for. I'd seen it in a few examples, and i couldn't understand what it did.

    Thanks!
    ~Peter


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