locked
what is the equivalent string * RRS feed

  • Question

  • User2027942032 posted

    hi there,

    if I have Dim sString as String * 5 in VB then how do I create the same data type in .NET?

     

    CHeers

    Monday, December 1, 2008 6:44 PM

All replies

  • User455213731 posted

    You can use stringbuilder to achieve it

    In C#StringBuilder sString = new StringBuilder(5);

    In VB.Net Dim sString As New StringBuilder(5)

    Monday, December 1, 2008 7:19 PM
  • User2027942032 posted

    if I have a struct definition as below:

    Structure Meminfo
            Dim sString As StringBuilder
    End Structure

    public function test()

    end function

    then how do I implement this in the test() function?

     

    Monday, December 1, 2008 7:32 PM
  • User455213731 posted

    Just always take 1st 5 characters of the string inside the test fuction. You can use substring to achieve it.

    Monday, December 1, 2008 7:41 PM
  • User2027942032 posted

    mate see my comments below:

    Structure Meminfo
            Dim sString As StringBuilder <-- I cannot specify the length in here (e.g. stringbuilder (5)) , error: "array bounds cannot apprear in the type specifier"
    End Structure

    public function test()

    dim someString as Meminfo

    someString.sstring = "hello" <-- how do I specify the length?

    end function

     

    Thanks

    Monday, December 1, 2008 7:57 PM
  • User455213731 posted

    you will say "hello".Substring(0, 5)

    Monday, December 1, 2008 8:25 PM
  • User2027942032 posted

    hey mate.. not so clear.. so still need further helps.. below are my VBA codes:

    ---------------------------------------------------

    Option Compare Database

    Declare Sub Process Lib "Process.dll" (ByRef sInt As Integer, ByRef sString As MemInfo)

    Type MemInfo
        sString As String * 100
    End Type

    Public Sub test()
        Dim sSize As Integer
        Dim sWord As MemInfo
       
        sSize = 100
        sWord.sString = "Hello: "
        Call Process (sSize , sWord )
       
        MsgBox sWord.sString
        
    End Sub
    ------------------------------------------------------------------------

    how do I implement the above code in .NET?

    Thanks

    Monday, December 1, 2008 11:32 PM
  • User-1630302068 posted

     Hey I dont remeber how to do this in VB.Net (or even C# for that matter) but what you need to do is look up P/Invoke and marshaling fixed length strings.  It looks like your process function is accepting a pre-allocated fixed length string and modifying it.  IIRC, there are several ways to do this I think your best bet is to read up on marshaling and P/Invoke. 

    Tuesday, December 2, 2008 8:05 AM
  • User455213731 posted

    Try this code

    Structure Meminfo
            Dim sString As StringBuilder
    End Structure

    public function test()

    dim sWord as Meminfo
    dim sSize as long

    sSize=100
    sWord.sstring = "hello".substring(0,sSize)

    Process(sSize,sWord)

    MessageBox sWord.sString

    end function

    Tuesday, December 2, 2008 12:54 PM
  • User-1630302068 posted

    @tdselvam: I really dont think a stringbuilder will work.  It looks like the code is attempting to do a P/Invoke call to unmanaged code which is going to have no idea what to do with a StringBuilder. 

     

    @ronald_yoh  Do you happen to have the C header file for the Process.dll that makes writing these things much more straight forward.

    Tuesday, December 2, 2008 1:18 PM
  • User2027942032 posted

    yes it is something to do with marshaling.. i just don't have any idea what the heck marshal is...!!

    tdselvam: when I tried your code I got this error "the same affect can usually be achieved by using string field and preinitializing...."

    My dll file was written in cobol...  don't have the header file...

    please help.. 

     

    Tuesday, December 2, 2008 5:40 PM
  • User-1630302068 posted

    All computer languages define how they store their basic types.  For example in C, a string is an array of bytes with a '\0' character at the end where as .Net stores a string as a length-prefixed array of variable width 'characters'.  Marshaling is the process of converting data from the format that one language uses into the format another uses and making a call across lanugages.

     

    I think this is the right way to do it:

     

    Private Declare Function Process Lib "Process.dll" (ByRef sInt As Integer, ByRef sString as String)

    Public Shared Sub test()
        Dim sSize As Integer
        Dim sWord As String = New String(" "c, 100)
      
        sSize = 100
        sWord.sString = "Hello: "
        Call Process (sSize , sWord )
      
        MsgBox sWord
    End Sub

    Tuesday, December 2, 2008 6:14 PM
  • User2027942032 posted
     
    Private Declare Function Process Lib "Process.dll" (ByRef sInt As Integer, ByRef sString as String) <-- is this .NET syntax? haven't tried this anyway
     
    Public Shared Sub test()
    Dim sSize As Integer
    Dim sWord As String = New String(" "c, 100)
      
    sSize = 100
    sWord.sString = "Hello: " <-- WHAT is sstring?
    Call Process (sSize , sWord )

    MsgBox sWord
    End Sub

    I'm not sure whether you know much about cobol or not.. but I have some programs in cobol with the following data type:

    1) 01 Test.

           05 Test-X   PIC X(10).

    2) 01 Temp         PIC X(20).

    any further thoughts?

    Tuesday, December 2, 2008 6:26 PM
  • User2027942032 posted

    Hi Jeff,

    I tried your code and changed the function to:

    <webmethod()> _
        Public Function wsCobol2() As String
            Dim sSize As Integer
            Dim sWord As String = New String(" "c, 20)
    
            Try
                sSize = 100
                sWord = "Hello: " <--- only this line I changed
                Call TRIAL(sSize, sWord)
            Catch ex As Exception
                Return ex.Message
            End Try
            
            Return sWord
        End Function</webmethod()>

     

    The sWord is not being modified (even though passed by ref) but the sSize is being modified once returned. any clue?

    Tuesday, December 2, 2008 6:51 PM
  • User-1630302068 posted

     Honestly I know nothing about COBOL.  Could you describe the parameters to the function and how they're being used?  I suspect that this function takes a fixed length buffer of bytes (sWord) and it's size (sSize) and then somehow populates that buffer with information.  Is this correct?  In C terms, is the function signature something like "void trial(int* len, char* msg);"?

     

    Now i'm at the point of guessing but try:

     

     

    Imports System.Runtime.InteropServices

    <DllImport("Process.dll")> _
    Private Shared Function Process (ByRef sInt As Integer, <MarshalAs(UnmanagedType.LPArray)> sString as Byte())

    Public Function wsCobol2() As String
        Dim sSize As Integer
        Dim buff as Byte() = new Byte(100)
        Dim sWord As String

        Try
            sSize = 100
            "Hello: ".ToCharArray().CopyTo(buff, 0);
            Call TRIAL(sSize, buff)
           
            sWord = Encoding.ASCII.GetString(buff);
        Catch ex As Exception
            Return ex.Message
        End Try
       
        Return sWord
    End Function

    Wednesday, December 3, 2008 7:48 AM
  • User2027942032 posted

    yes.. this c (void trial(int* len, char* msg)) function is the equivalent.

    In cobol program, you have to declare the length of the variable. (e.g. PIC X(20)) --> the 'X' means character; the 20 is the number of bytes will be stored. So if you assign a string "Hello" to this variable, you will end up with "Hello                 " (15 spaces at the end).

    The VBA code that I supplied earlier is working perfectly.. Programmatically saying, I just convert that VBA code to .NET syntax. but unfortunately that's not the case.

    Further to your code, I've commented out as bellow:

     <DllImport("Process.dll")> _
    Private Shared Function Process (ByRef sInt As Integer, <MarshalAs(UnmanagedType.LPArray)> sString as Byte()) <-- sString is passed byval or byref?

    Public Function wsCobol2() As String
        Dim sSize As Integer
        Dim buff as Byte() = new Byte(100) <-- i got an error saying byte type does not have constructor
        Dim sWord As String

        Try
            sSize = 100
            "Hello: ".ToCharArray().CopyTo(buff, 0);
            Call TRIAL(sSize, buff)
           
            sWord = Encoding.ASCII.GetString(buff);
        Catch ex As Exception
            Return ex.Message
        End Try
       
        Return sWord
    End Function

     

    I've been flirting around for more than 3 months just to get this working.. please help..

    Wednesday, December 3, 2008 6:01 PM