How to get the fixed length of a fixed length string

A

active

Private Structure TNavRec

<VBFixedString(100),System.Runtime.InteropServices.MarshalAs(System.Runtime.
InteropServices.UnmanagedType.ByValTStr,SizeConst:=100)> Public strItemName
As String

<VBFixedString(4),System.Runtime.InteropServices.MarshalAs(System.Runtime.In
teropServices.UnmanagedType.ByValTStr,SizeConst:=4)> Public strItemID As
String

<VBFixedString(2),System.Runtime.InteropServices.MarshalAs(System.Runtime.In
teropServices.UnmanagedType.ByValTStr,SizeConst:=2)> Public EndOfLine As
String

End Structure

--snip--
Dim ludtNavRec As TNavRec
'accept more than a record will hold
cboName.Text = VB.Left(Value, Len(ludtNavRec.strItemName))

------
In my program
Len(ludtNavRec.strItemName)
returns 0 (probably because I haven't stored anything into it yet).
Probably returning the length of the variable string not the capacity.

I could, of course, store the max length (100) in a const and use that but
I wonder if there is a better way?

Thanks
 
A

Alexandre Moura

I'd say you have three options - defining a const, only check the len of
structures that have the string fields initialized with strings with the
correct size, or using reflection to pull the attributes and read their
values.
Personally, I'd go either with the constant or with working with correctly
initialized structures only.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top