473,320 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

.... 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
6 4218
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
"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
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
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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Hector A | last post by:
Hi I'm trying to convert a string that already looks like a date to a date that I can use when I pass it from java to the database. I receive the date in format yyyy-mm-dd and I need it to be a...
1
by: Anonieko Ramos | last post by:
Answer: http://weblogs.asp.net/tims/archive/2004/04/02/106310.aspx by Tim Sneath I've come across the situation on a number of occasions when coding where I've wanted to convert from a string...
3
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
5
by: ywchan | last post by:
I would like to convert a string to a fixed length e.g. if fixed length = 10 'abc' -> ' abc' 'abcdefghijklm' -> 'abcdefghij' Is there any simple function in C# can perform this operation?...
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
3
by: priyanka | last post by:
Hi there, I want to convert a String into integer. I get the string from a file using : string argNum; getline(inputStream,argNum); I now need to convert argNum into integer.
2
by: AMP | last post by:
Hello, I want to convert a string Array permanantly to a int Array. Everywhere in my code I have to use Convert.ToInt32 to use the data. I just want to change it once. Thanks Mike
12
by: aatif | last post by:
I want to convert a string of hex characters (2 hex chars = 1 byte), to ASCII. Hex chars include zeros (0x00) as well, which I want to include in ASCII string. hex string: 5000005355.... ASCII:...
2
by: AndreasL | last post by:
Hi! I have a string passed to a methos via a string&. I would like to convert this to an unsigned long. How do I convert the string iterator to a char*? with all the error checking code...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.