473,769 Members | 5,072 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.addres s)
''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''

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.Visua lBasic.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 4257
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**@discussio ns.microsoft.co m> wrote in message
news:3D******** *************** ***********@mic rosoft.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.addres s)
''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''

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.Visua lBasic.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**@discussio ns.microsoft.co m> 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.addres s)
''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''

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.Visua lBasic.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(Unma nagedType.ByVal TStr, 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(4 1)> _
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**@discussio ns.microsoft.co m> 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.addres s)
''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''

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.Visua lBasic.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(Unma nagedType.ByVal TStr, 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(4 1)> _
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**@discussio ns.microsoft.co m> 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.addres s)
''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''

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.Visua lBasic.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(Unma nagedType.ByVal TStr, 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(4 1)> _
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**@discussio ns.microsoft.co m> 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**@discussio ns.microsoft.co m> 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 "ArrayIsDyn amic = 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
39807
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 date variable in 'mm/dd/yyyy' or 'm/dd/yyyy' format. My code is shown below. Any suggestions? code: String sailYear = (String)sail_date.toString().substring(0,4);
1
375
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 to an enum. In the Media Catalog sample, I resorted to one giant switch statement that has a case block for each string that returns an enum from it. One of my colleagues came up with the answer yesterday; it's one of
3
10292
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 and there you have it. So I enquired and found the Convert class with it's promising ToInt32 method, great... but it doesn't work. The thing keeps throwing Format Exceptions all over the place. What is the "C#" way to do this??? code int wmin,...
5
23676
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? thanks!
15
10834
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
9201
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
1330
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
4975
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: P<null><null>SU... I can do it and the string length also includes nulls but when I concatenate other string, it doesn't show as its part. string HexValue = "500000535500";
2
7730
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 removed, it's basically this: MyFunction(string &s)
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10214
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10048
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.