Click here to Skip to main content
15,888,065 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Public Structure Foo
<FieldOffset(0)>Public NameLength as Integer '4 Byte
<FieldOffset(4), MarshalAs(UnmanagedType.LPStr, SizeConst:=50)>Public Name As String 'variable name in MBCS
<FieldOffset(54)) Public Age As Integer
End Structure


I would expect the FieldOffsets are okay. But Visual Studio 2013 displays an Error, that the Offset of Age is invalid.

What I have tried:

If I change it to 52, the Error is gone. But I don't know why.
Posted
Updated 20-Jul-23 20:24pm

1 solution

I can't check it in VS 2013 - it says my licence is "stale" and it can't retrieve a new one - but if I try it in VS 2022 it works fine converted to C#:
C#
[StructLayout(LayoutKind.Explicit)]
public struct Foo
    {
    [FieldOffset(0)]
    public int NameLength; //4 Byte
    [FieldOffset(4), MarshalAs(UnmanagedType.LPStr, SizeConst = 50)] public string Name; //variable name in MBCS
    [FieldOffset(54)] public int Age;
    }

But in VB, it gives me an error that a ">" is missing:
VB
<FieldOffset(54)) Public Age As Integer
                ^
Which says that the code you are getting the error from isn;t the code you showed us - that won't compile so even if you did change it to "52" you'd still get an error.
Correct it, and I get no errors at all:
VB
Public Structure Foo
    <FieldOffset(0)> Public NameLength As Integer '4 Byte
    <FieldOffset(4), MarshalAs(UnmanagedType.LPStr, SizeConst:=50)> Public Name As String 'variable name in MBCS
    <FieldOffset(54)> Public Age As Integer
End Structure
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900