By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,742 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,742 IT Pros & Developers. It's quick & easy.

Convert this fixed string VB6 code in VB.NET and win ...

P: n/a
.... my eternal gratitude!!! :p

Here is the problem. A sample of my original VB6 code :

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''
Public Type INFO
name(1 To 3) As String * 41
address As String * 41
End Typ
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''

I used to be able to get the length of any element in my type with the
following code:

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''
dim INFO as INFO
dim temp as string = Len(INFO.address)
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''

The value of temp would then be 41
With my new VB.NET code, I try to use the following code, but whatever I
try, the value of temp is always 0
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''
Public Structure INFO
<VBFixedString(41)> public name(1 To 28) As String
<VBFixedString(41)> public address As String
End Structure
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''
dim INFO as INFO
dim temp as integer = Microsoft.VisualBasic.Len(INFO.address)
or
dim temp as integer =
System.Runtime.InteropServices.Marshal.SizeOf(INFO .address)
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''

In the latter case, the return value is a null and my application crashes.
Any input on this would be extremely appreciated.

Thank you.

Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi,

From the note about the vbfixedlength string. Keep in mind that
this attribute does not change the actual length of the string itself.

http://msdn.microsoft.com/library/de...gattribute.asp

Ken
-----------------------
"Eric" <Er**@discussions.microsoft.com> wrote in message
news:3D**********************************@microsof t.com...
.... my eternal gratitude!!! :p

Here is the problem. A sample of my original VB6 code :

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''
Public Type INFO
name(1 To 3) As String * 41
address As String * 41
End Type
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''

I used to be able to get the length of any element in my type with the
following code:

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''
dim INFO as INFO
dim temp as string = Len(INFO.address)
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''

The value of temp would then be 41
With my new VB.NET code, I try to use the following code, but whatever I
try, the value of temp is always 0.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''
Public Structure INFO
<VBFixedString(41)> public name(1 To 28) As String
<VBFixedString(41)> public address As String
End Structure
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''
dim INFO as INFO
dim temp as integer = Microsoft.VisualBasic.Len(INFO.address)
or
dim temp as integer =
System.Runtime.InteropServices.Marshal.SizeOf(INFO .address)
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''

In the latter case, the return value is a null and my application crashes.
Any input on this would be extremely appreciated.

Thank you.
Nov 21 '05 #2

P: n/a
"Eric" <Er**@discussions.microsoft.com> schrieb
... my eternal gratitude!!! :p

Here is the problem. A sample of my original VB6 code :

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''
Public Type INFO
name(1 To 3) As String * 41
address As String * 41
End Type
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''

I used to be able to get the length of any element in my type with
the following code:

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''
dim INFO as INFO
dim temp as string = Len(INFO.address)
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''

The value of temp would then be 41
With my new VB.NET code, I try to use the following code, but
whatever I try, the value of temp is always 0.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''
Public Structure INFO
<VBFixedString(41)> public name(1 To 28) As String
<VBFixedString(41)> public address As String
End Structure
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''
dim INFO as INFO
dim temp as integer = Microsoft.VisualBasic.Len(INFO.address)
or
dim temp as integer =
System.Runtime.InteropServices.Marshal.SizeOf(INFO .address)
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''

In the latter case, the return value is a null and my application
crashes.
Any input on this would be extremely appreciated.

Thank you.


It might be possible to get the attribute using Reflection, but why do you
need this? The structure/string would be written to a file correctly as you
apply the vbfixedstring attribute. Where else do you need the length? If you
want to pass it to API functions, you'd have to add the MarshalAs-Attribute
on the 'address' element anyway:

<MarshalAs(UnmanagedType.ByValTStr, SizeConst := 41)> _
address As String * 41

For the first Element 'name', it's currently not supported to marshal an
array of fixed length strings. You'd have to, for example, replace it by an
array of chars (sizeconst = 123). Using VB file functions and referring to
the first VB6 declaration, this declaration should work:
Private Structure INFO
<VBFixedArray(2), VBFixedString(41)> _
Public name() As String
<VBFixedString(41)> _
Public address As String
End Structure
The Len function returns 164 as expected.
Armin

Nov 21 '05 #3

P: n/a
Thank you for your reply Armin, it is a lot clearer now.

However i still experience one ultimate problem. What if I were to want the
size of a structure containing another structure.

ie:

Public Structure TEST
PUblic Integ as Integer
End Structure

Public Structure INFO
Public Test as TEST
Public Str as Integer
End Structure

When I try Len(INFO) it give me the size of an Integer, ignoring the Test
element. Is there a special secret attribute for this situation?

Thnk you
"Armin Zingler" wrote:
"Eric" <Er**@discussions.microsoft.com> schrieb
... my eternal gratitude!!! :p

Here is the problem. A sample of my original VB6 code :

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''
Public Type INFO
name(1 To 3) As String * 41
address As String * 41
End Type
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''

I used to be able to get the length of any element in my type with
the following code:

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''
dim INFO as INFO
dim temp as string = Len(INFO.address)
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''

The value of temp would then be 41
With my new VB.NET code, I try to use the following code, but
whatever I try, the value of temp is always 0.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''
Public Structure INFO
<VBFixedString(41)> public name(1 To 28) As String
<VBFixedString(41)> public address As String
End Structure
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''
dim INFO as INFO
dim temp as integer = Microsoft.VisualBasic.Len(INFO.address)
or
dim temp as integer =
System.Runtime.InteropServices.Marshal.SizeOf(INFO .address)
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''

In the latter case, the return value is a null and my application
crashes.
Any input on this would be extremely appreciated.

Thank you.


It might be possible to get the attribute using Reflection, but why do you
need this? The structure/string would be written to a file correctly as you
apply the vbfixedstring attribute. Where else do you need the length? If you
want to pass it to API functions, you'd have to add the MarshalAs-Attribute
on the 'address' element anyway:

<MarshalAs(UnmanagedType.ByValTStr, SizeConst := 41)> _
address As String * 41

For the first Element 'name', it's currently not supported to marshal an
array of fixed length strings. You'd have to, for example, replace it by an
array of chars (sizeconst = 123). Using VB file functions and referring to
the first VB6 declaration, this declaration should work:
Private Structure INFO
<VBFixedArray(2), VBFixedString(41)> _
Public name() As String
<VBFixedString(41)> _
Public address As String
End Structure
The Len function returns 164 as expected.
Armin

Nov 21 '05 #4

P: n/a
I made a mistake on my last post, the problem is that the structure is
ignored if it is a fixed array.

ie:

Public Structure TEST
Public Integ As Integer
End Structure

Public Structure INFO
<VBFixedArray(4)> Public Test() As Test
Public Str As Integer
End Structure

The result of Len(INFO) is 4, instead of 24.

Thank you

"Eric" wrote:
Thank you for your reply Armin, it is a lot clearer now.

However i still experience one ultimate problem. What if I were to want the
size of a structure containing another structure.

ie:

Public Structure TEST
PUblic Integ as Integer
End Structure

Public Structure INFO
Public Test as TEST
Public Str as Integer
End Structure

When I try Len(INFO) it give me the size of an Integer, ignoring the Test
element. Is there a special secret attribute for this situation?

Thnk you
"Armin Zingler" wrote:
"Eric" <Er**@discussions.microsoft.com> schrieb
... my eternal gratitude!!! :p

Here is the problem. A sample of my original VB6 code :

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''
Public Type INFO
name(1 To 3) As String * 41
address As String * 41
End Type
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''

I used to be able to get the length of any element in my type with
the following code:

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''
dim INFO as INFO
dim temp as string = Len(INFO.address)
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''

The value of temp would then be 41
With my new VB.NET code, I try to use the following code, but
whatever I try, the value of temp is always 0.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''
Public Structure INFO
<VBFixedString(41)> public name(1 To 28) As String
<VBFixedString(41)> public address As String
End Structure
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''
dim INFO as INFO
dim temp as integer = Microsoft.VisualBasic.Len(INFO.address)
or
dim temp as integer =
System.Runtime.InteropServices.Marshal.SizeOf(INFO .address)
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''

In the latter case, the return value is a null and my application
crashes.
Any input on this would be extremely appreciated.

Thank you.


It might be possible to get the attribute using Reflection, but why do you
need this? The structure/string would be written to a file correctly as you
apply the vbfixedstring attribute. Where else do you need the length? If you
want to pass it to API functions, you'd have to add the MarshalAs-Attribute
on the 'address' element anyway:

<MarshalAs(UnmanagedType.ByValTStr, SizeConst := 41)> _
address As String * 41

For the first Element 'name', it's currently not supported to marshal an
array of fixed length strings. You'd have to, for example, replace it by an
array of chars (sizeconst = 123). Using VB file functions and referring to
the first VB6 declaration, this declaration should work:
Private Structure INFO
<VBFixedArray(2), VBFixedString(41)> _
Public name() As String
<VBFixedString(41)> _
Public address As String
End Structure
The Len function returns 164 as expected.
Armin

Nov 21 '05 #5

P: n/a
"Eric" <Er**@discussions.microsoft.com> schrieb
Thank you for your reply Armin, it is a lot clearer now.

However i still experience one ultimate problem. What if I were to
want the size of a structure containing another structure.

ie:

Public Structure TEST
PUblic Integ as Integer
End Structure

Public Structure INFO
Public Test as TEST
Public Str as Integer
End Structure

When I try Len(INFO) it give me the size of an Integer, ignoring the
Test element. Is there a special secret attribute for this
situation?


Using your declarations above:

dim i as info
msgbox len(i)

I get "8" which is what I expect.

Armin
Nov 21 '05 #6

P: n/a
"Eric" <Er**@discussions.microsoft.com> schrieb
I made a mistake on my last post, the problem is that the structure is

ignored if it is a fixed array.

ie:

Public Structure TEST
Public Integ As Integer
End Structure

Public Structure INFO
<VBFixedArray(4)> Public Test() As Test
Public Str As Integer
End Structure

The result of Len(INFO) is 4, instead of 24.

Right, now I also get 4. Writing the structure to a file creates a 22 byte
sized file, so it's only the len function not returning the correct value.
But as you say it must be 24 bytes. This is probably because it's not
possible(?) to write a fixed array contained in a structure. The problem is
that you can specify "ArrayIsDynamic = False" if you write an array using
FilePut, but you can not specify this flag if you write a structure
containing an array. That's probably the reason why Len also returns the
"wrong" value - my guess only. There's a long explanation in the docs on the
Fileput function.
Armin

Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.