Results 1 to 10 of 10

Thread: [RESOLVED] VB.net Code to C#

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    265

    Resolved [RESOLVED] VB.net Code to C#

    Hi,
    I'm trying to rewrite my code in c#, the one thing i missed is the equivalent to this
    Code:
    Public sControlPath As New String(" ", 256)
    in c#, could this be causing it to error ?

    Code:
    Public Class vbCode
        Public sControlPath As New String(" ", 256)
        Public bIsAccess As Boolean = False
        Public sControlServer As New String(" ", 256)
        
        Public sSqlName As New String(" ", 31)
        Public sSqlPassword As New String(" ", 31)
        Public sError As New String(" ", 2049)
        Public bRet As Boolean
    
    
        Declare Function GetInfo Lib "ABC.dll" (ByVal sControlPath As String, _
        ByRef bIsAccess As Boolean, ByVal sControlServer As String, ByVal sSqlName As String, _
        ByVal sSqlPassword As String, ByVal sError As String) As Boolean
    
        
        Sub New()
            bRet = GetInfo(sControlPath, bIsAccess, sControlServer, sSqlName, sSqlPassword, sError)
         
        End Sub
    
    End Class
    C#

    Code:
    public class cSharpCode
            {
                [DllImport("ABC.dll")]
                private static extern bool GetInfo(string cControlPath,ref bool bIsAccess,string cControlServer, string sSqlName, string sSqlPassword,string sError);
                     
              
             
                public string sControlPath;
                public bool bIsAccess,bRet;
                public string sControlServer, sSqlName, sSqlPassword, sError;
    
                public cSharpCode()
                {
                           
                   
                    bRet = GetInfo(sControlPath, ref bIsAccess, sControlServer, sSqlName, sSqlPassword, sError);
                    
    
                 
                }
               
        
        }
    Errors on calling GetInfo() in cSharpCode constructor : Attempted to write or read protected memory, this is often an indication that the other memory is corrupt.

  2. #2
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: VB.net Code to C#

    Try using
    http://converter.telerik.com/

    and report if you have any errors?

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: VB.net Code to C#

    yes it would cause an error, as in vb you pass a string of spaces, that getinfo returns as a string of chars, probably null terminated...

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: [RESOLVED] VB.net Code to C#

    At first I thought it was because you didn't define the size of the string buffers, which you like this:
    Code:
    String controlPath;
    controlPath = new String(' ', 256);
    But then I realized the strings are byVal, so it shouldn't matter... shouldn't but doesn't mean it doesn't.
    My guess is that it's looking for strings of a specific size (otherwise why have the fixed-length strings in VB) and it's not getting it.
    Try setting each string to a specific size, then fill it with the data you need (treat it like an array of characters - or use some kind calculation to figure it all in) to pass it into the function and see if that helps.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: VB.net Code to C#

    Quote Originally Posted by .paul. View Post
    yes it would cause an error, as in vb you pass a string of spaces, that getinfo returns as a string of chars, probably null terminated...
    Returns? or accepts? the definition looks like it returns a boolean to me...
    But yeah, I think you're on the right idea there. the strings need to be padded with spaces to the proper length.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    265

    Re: [RESOLVED] VB.net Code to C#

    Thanks guys, this did the trick
    Code:
      public string sControlPath = new string(Convert.ToChar(" "), 256);

  7. #7
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: [RESOLVED] VB.net Code to C#

    Quote Originally Posted by wiss.dev View Post
    Thanks guys, this did the trick
    Code:
      public string sControlPath = new string(Convert.ToChar(" "), 256);
    You can do this more directly:
    Code:
    public string sControlPath = new string(' ', 256);
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: [RESOLVED] VB.net Code to C#

    Quote Originally Posted by David Anton View Post
    You can do this more directly:
    Code:
    public string sControlPath = new string(' ', 256);
    Quite right. The main issue here is that it was bad VB code to start with. That String constructor expects a Char and you were passing a String. That String was being implicitly converted to a Char by taking the first character of the String. You obviously have Option Strict Off or that code wouldn't even compile, which means it's bad code as far as I'm concerned.

    If you're going to convert VB code to C# and you don't already have Option Strict On (for which there's no real excuse) then the first thing to do is turn it On and fix all the errors that get flagged. At least you then have a level playing field, i.e. the VB compiler will play by basically the same rules rules as the C# compiler with regards to strict typing. If you'd been using a Char in VB then an online converter probably would have had no issues.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    265

    Re: [RESOLVED] VB.net Code to C#

    about to start pulling my hair now

    for no reason that i know, my c# code no longer populate the variables, it is fine in VB though

    Code:
    Option Strict On
    Public Class vb
    
        dim sControlPath As New String(Convert.ToChar(" "), 256)
        Public bIsAccess As Boolean = False
        Dim sControlServer As New String(Convert.ToChar(" "), 256)
       
        Dim sSqlName As New String(Convert.ToChar(" "), 31)
        Dim sSqlPassword As New String(Convert.ToChar(" "), 31)
        Dim sError As New String(Convert.ToChar(" "), 2049)
        Dim bRet As Boolean
    
    
    
    
        Declare Function GetInfo Lib "ABC.dll" (ByVal sControlPath As String, _
        ByRef bIsAccess As Boolean, ByVal sControlServer As String, ByVal sSqlName As String, _
        ByVal sSqlPassword As String, ByVal sError As String) As Boolean
       
    
    
        Sub New()
            bRet = GetInfo(sControlPath, bIsAccess, sControlServer, sSqlName, sSqlPassword, sError)
           
        End Sub
    
    End Class

    Here GetInfo returns true, fields are not changed
    Code:
    public class cSharp
            {
                bool bIsAccess, bRet;
                string sControlServer = new string(Convert.ToChar(" "), 256);
                string sSqlName = new string(Convert.ToChar(" "), 31);
                string sSqlPassword = new string(Convert.ToChar(" "), 31);
                string sError = new string(Convert.ToChar(" "), 2049);
                string sControlPath = new string(Convert.ToChar(" "), 256);
    
                [DllImport("ABC.dll")]
                private static extern bool GetInfo(string sControlPath, ref bool bIsAccess, string sControlServer, string sSqlName, string sSqlPassword, string sError);          
               
    
                public cSharp()
                {
                    bRet = GetInfo(sControlPath, ref bIsAccess, sControlServer, sSqlName, sSqlPassword, sError);
                  
                  
                }
        
            }

  10. #10
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: [RESOLVED] VB.net Code to C#

    Well, bIsAccess may (or may not) change... wouldn't expect the others to though... you're passing them (C# equivelent of) by val.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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