Structure ?

  • Thread starter Thread starter JerryWEC
  • Start date Start date
J

JerryWEC

I want to be able to create a structure like I did in VB6, that have
variables

Name As String * 10
Age As String * 3

for the size of the strings. Can I do this some way in VB.net?

I'm playing with <StructLayoutArribute()> and <MarshalAs()> attributes, but
I'm not really wanting to expose these structrues to COM. I only want to use
them inside VB.net assemblies.

Help!

JerryM
 
JerryWEC said:
I want to be able to create a structure like I did in VB6, that have
variables

Name As String * 10
Age As String * 3

for the size of the strings. Can I do this some way in VB.net?

This syntax is not supported any more. You can mimick it using the
'VBFixedStringAttribute' attribute. However, if you are converting the
assembly from VB6 to VB.NET, I'd rethink the design. You could implement
'Name' and 'Age' as properties and pad the string to the desired length in
the property's 'Set' accessor.
 
On the 16th Feb. Jerry someone asked the same sort of question in this
newsgroup. It was posted by 'Rick Knospler' & the subject was 'VB6
Conversion. Need help understanding how to implement fixed length string
arrays..'

In this post you will have your answer
 
Herfried, this is a good idea... It is new code and I was hoping there was
a way to simulate the old way of doing strings for fixed size. Seems like
the new Structure object is overkill and should have had this fixed string
capability built in.

I'm still thinking a structure is still faster than a class since it's
stored on the stack?

TIA! JerryM
 
JerryM,

The .Net Framework Developer's Guide gives the following recommendations on
when to use a structure vs a class:

Do not define a structure unless the type has all of the following
characteristics:

It logically represents a single value, similar to primitive types (integer,
double, and so on).

It has an instance size smaller than 16 bytes.

It is immutable.

It will not have to be boxed frequently.

If one or more of these conditions are not met, create a reference type
instead of a structure. Failure to adhere to this guideline can negatively
impact performance.

Kerry Moorman
 
Kerry, thanks for the tip!

Where can I find this .Net Framework Developer's Guide? Is this an online
document or is it in msdn?

Thanks! JerryM
 
JerryM,

When I choose Help | Index from the Visual Basic 2005 Express Edition IDE
and look for "structure" I get a long list of references. One reference is
under "structures [.Net Framework] and is titled "design guidelines". When I
choose "design guidelines" I get a document headed ".NET Framework
Developer's Guide".

So I guess this developer's guide is somehow part of the online
documentation. I don't know if it can be browsed as an independent document.

Kerry Moorman
 
yes; you are correct in your analysis.

UNECESSARY CHANGE IS NOT WANTED
UNECESSARY CHANGE IS NOT BEING ACCEPTED IN THE MARKET
UNECESSARY CHANGE IS NOT SEXY
 
Most of MS documentation is available on the web:

http://msdn2.microsoft.com/en-us/library/ms229017.aspx

Kerry said:
JerryM,

When I choose Help | Index from the Visual Basic 2005 Express Edition IDE
and look for "structure" I get a long list of references. One reference is
under "structures [.Net Framework] and is titled "design guidelines". When I
choose "design guidelines" I get a document headed ".NET Framework
Developer's Guide".

So I guess this developer's guide is somehow part of the online
documentation. I don't know if it can be browsed as an independent document.

Kerry Moorman


JerryWEC said:
Kerry, thanks for the tip!

Where can I find this .Net Framework Developer's Guide? Is this an online
document or is it in msdn?

Thanks! JerryM
 
Göran, thanks for the refer link!

I have created two classes on called WeldData and one called WeldInterval.
I have an array of WeldInterval's in my WeldData class.

I have not been able to test this because I'm in assembly /.dll hell again
right now.

I really just wanted to create a structure to hold my data and beable to
create empty records and build more data related records as needed. I have
490 bytes of data in a record and did not want to create a full blown class
or classes. I'm working in a manufacturing environment and speed is
everything. I'm hoping this will be fast enough for plant floor operation.

I'll provided more feedback if these two classes don't work as I expect them
too.

thanks! JerryM
 
JerryM,

You might also want to consider using a List (Of WeldInterval) instead of an
array in your WeldData class.

Kerry Moorman
 
JerryWEC said:
Göran, thanks for the refer link!

I have created two classes on called WeldData and one called WeldInterval.
I have an array of WeldInterval's in my WeldData class.

I have not been able to test this because I'm in assembly /.dll hell again
right now.

I really just wanted to create a structure to hold my data and beable to
create empty records and build more data related records as needed. I have
490 bytes of data in a record and did not want to create a full blown class
or classes. I'm working in a manufacturing environment and speed is
everything. I'm hoping this will be fast enough for plant floor operation.

I'll provided more feedback if these two classes don't work as I expect them
too.

thanks! JerryM

Don't be afraid to create a class. If your data consists of strings,
then you are using objects anyway, so there is no real gain in using a
structure to handle them.

Objects are a lot faster in .NET than in VB6. I remember seeing a test
that showed that .NET handled objects about 100 times faster. Even if
that test might not be representative for all aspects of object use, you
get an idea of the difference in speed.

As the guidelines states, structures should only be used for data that
represent a single value. Also it says that a reasonable size for a
structure is 16 bytes. If you have 490 bytes in a structure, you will
get a performance hit everytime you use the structure. As it's passed by
value, all 490 bytes will be copied every time.

Also structures are value types, and that may cause you some problems.
If your WeldInterval is a structure and you access one of them from the
array, it will be copied. If you change a property in the structure you
might expect that you are changing the data in your array, but that
doesn't happen, as you are working with a copy of the data.
 
Back
Top