5

I'm almost done converting a module from VB6 to VB.NET, but I'm having trouble with the following 2 quotes and am wondering if there's any way to go about this:

Structure AUDINPUTARRAY
    bytes(5000) As Byte
End Structure

I'm trying to change that bytes line to: Dim bytes(5000) as Byte but it's not letting me define the size in a structure.


Here's the second one:

Private i As Integer, j As Integer, msg As String * 200, hWaveIn As integer

I haven't a clue on how to convert: msg As String * 200

MarkJ
  • 30,070
  • 5
  • 68
  • 111
user1060582
  • 185
  • 1
  • 1
  • 6

3 Answers3

4

you cannot declare an initial size in VB.Net , you can set its size later using Redim statement in constructor or wherever needed

Structure AUDINPUTARRAY
    Public bytes() As Byte
    Public Sub New(ByVal size As Integer)
        ReDim bytes(size) ' set size=5000

    End Sub


End Structure

In Visual Basic .NET, you cannot declare a string to have a fixed length unless you use the VBFixedStringAttribute Class attribute in the declaration. The code in the preceding example causes an error.

You declare a string without a length. When your code assigns a value to the string, the length of the value determines the length of the string see http://msdn.microsoft.com/en-us/library/f47b0zy4%28v=vs.71%29.aspx . so your declarration will become

    Private i As Integer, j As Integer, hWaveIn As Integer
    <VBFixedString(200)> Private msg As String
Akshita
  • 849
  • 8
  • 15
1

You can do this via attributes

Public Structure <StructLayout(LayoutKind.Sequential)> AUDINPUTARRAY
   Public <MarshalAs(UnmanagedType.ByValArray, SizeConst := 5000)> 
     Bytes() As Byte
End Structure
jmoreno
  • 12,752
  • 4
  • 60
  • 91
0

I would suggest that, while refactoring your code from VB6 to .net, that you take another look at whether you even want to emulate the fixed-length msg As String * 200. If you were counting on the fixed-length string so that you could chop characters off of the end, and still have a 200-character record, that's messy code that depends on a function's side effects.

When we converted from VB6 (a still-ongoing process), it made the intent of the code clearer if we explicitly set the string to a 200-byte block of spaces. Perhaps by declaring:

String msg = String(' ', 200)

(if that's valid in VB.net as well as C#).

RobertB
  • 946
  • 1
  • 9
  • 22