Link to home
Create AccountLog in
Avatar of gbmcneil
gbmcneil

asked on

What's wrong with "Public Path As String * 256" in VB.NET 2008?

Hello Experts -

Why doesn't this declaration work?  

Public path As String * 256

Avatar of B34RDY
B34RDY

You look like you are trying to multiply a string type by an integer, which is a mismatch and not allowed.
Removing the  *256 will allow it to work.

If you are trying to set the length of the path variable to 256 then I don't think you can. You would have to accept the input for path and then use:

        path = path.Substring(0, 256)

to cut it down.
Avatar of Mike Tomlinson
VB6 style Fixed-length strings aren't really supported in .Net.

The correct approach really depends in HOW the original string was being used.  What is being done in the original app?
Avatar of gbmcneil

ASKER

Hi Idle -

I am trying to get someone else's DLL to work in my application and the documentation that was provided included this in the Declarations Section.

I know this form of declaration used to be valid. I'm not misled into thinking that I'm asking the system to multiply a string times a number. See:

http://www.workflowstudios.com/lance/blog.nsf/d6plinks/LSPN-76LLMJ

The question is, if VB.NET no longer allows it, what can I do in its place?

You can try to substitute a StringBuilder and set its initial length to 256:

    Dim sb As New System.Text.StringBuilder(256)
Hello Experts -

This is what I really started with:

<StructLayout(LayoutKind.Sequential, Pack:=4)> Public Structure gbInfo
        Public vol As gbArray
        Public oi As gbArray
        Public size As Int32
        Public datasize As Int32
        Public reccnt As Int32
        <VBFixedString(256), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)> Public path As String
        <VBFixedString(16), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=16)> Public name As String
        <VBFixedString(48), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=48)> Public description As String
        <VBFixedString(16), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=16)> Public symbol As String
        Public begindate As Double
        Public Function isNull() As Boolean
         isNull = dt.isNull
        End Function
 End Structure

And, the compiler was getting hung up on the strange looking <VBFixedString> " thingies".

So, I pulled the <VBFixedStrings> things and just said, for example:

Public Path As String * 256

But, the compiler doesn't like this declaration either.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
You are a genius Idle. Have you ever thought about going on Jeopardy?

Adding the "Imports System.Runtime.InteropServices" fixed it.

I guess fixed length strings are supported in VB.NET 2008, if you can get past the unusual way of declaring them!

Thanks.
I would not have figured out this one in a million years.