| ■No65033 (コンバート後に悩む人 さん) に返信 > #前述の話題ですが、Midだから+1ですよね。
そのとおりです。 Len(a3) ではなく、Len(a5) + 1 ですね。失礼しました。(^^;
> substringでしたら0スタートなので+1の心配はしなくてよいということをすっかり失念してました。 Substring だと、データ長を超える位置を指定したときに例外を発するため、 意図的に Strings.Mid を使っていました。
> 以下のような感じでしょうか。
さらに言えば、元の String→Structure のコードは、
Public Function StructureFromString(Of T As Structure)(ByVal s As String) As T Dim newValue As ValueType = StructureFromString s &= StrDup(Len(newValue), " "c) Dim pos As Integer = 1 For Each f As FieldInfo In newValue.GetType().GetFields() Dim attributes() As Object = f.GetCustomAttributes(GetType(VBFixedStringAttribute), False) If attributes.Length <> 0 Then Dim attr As VBFixedStringAttribute = DirectCast(attributes(0), VBFixedStringAttribute) Dim value As String = Strings.Mid(s, pos, attr.Length) f.SetValue(newValue, value) pos += attr.Length End If Next Return DirectCast(newValue, T) End Function
などとしておいた方が良さそうです。
元のコードだと、 'StructureFromString(Of BBB)("z").xxx.Length StructureFromText(Of BBB)("z").xxx.Length が 0 になってしまいますが、この修正版であれば 2 が返されます。 |