Struct (from VB to c#)

P

paulo2012

I have the following in VB6:-
Private Type Struct1
Fb As Integer
Dt(1 To 2000) As Byte
End Type

Private Type Struct2
pr(1 To 20) As Integer
End Type

Private Type IN
IsmprA(1 To 40) As Integer
IsmprB(1 To 2) As Struct2
Fr(1 To 2) As Struct1
Fz(1 To 2) As Struct1
End Type

Private Type OUT
OsmprA(1 To 10) As Integer
OsmprB(1 To 2) As Struct2
fRO(1 To 2) As Struct1
End Type

I need to create an equivalent in C#. I have attempted this but get error:-
Cannot have instance field initializers in structs. Help appreciated
 
P

paulo2012

Patrice thanks for the reply but I can't use 'fixed' keyword as statement:-

public fixed Struct2 IsmprA[2];

caused error:-
Fixed size buffer type must be one of the following: bool, byte, short, int,
long, char, sbyte, ushort, uint, ulong, float or double

So as you can see, I cannot apply 'fixed' to Struct2 as it is not one of
these data types.

If anyone knows how I could succesfully port the VB code to C#, that would
be appreciated.

Paulo
 
P

Peter Duniho

[...]
I need to create an equivalent in C#. I have attempted this but get
error:-
Cannot have instance field initializers in structs. Help appreciated

The error seems pretty self-explanatory to me. And as Patrice says, you
have to post the C# code if you want us to answer questions about the C#
code.

For your general information: a "struct" is a "value type", and has
specific rules (I'm pretty sure these rules apply in VB.NET too, but your
VB.NET code doesn't appear to violate them). In particular, a struct is
required to have an initialization path available where all of the values
in the struct a the default varlues for that type. Since default values
all basically resolve to one or more bytes set to 0, this allows for quick
initialization of arrays of structs.

The implication in that requirement is that you cannot provide alternative
default initialization for struct members. You can't have a field
initializer, and you can't write a parameterless constructor.

Since the VB.NET code you posted doesn't actually have field initializers,
and since you didn't bother to post your C# code, even after being asked
to, there's really nothing anyone can do to help you with your specific
problem. But the above describes the rules that constrain whatever it is
you're trying to do.

If you want better advice, post a better question.

Pete
 
G

Göran Andersson

paulo2012 said:
I have the following in VB6:-
Private Type Struct1
Fb As Integer
Dt(1 To 2000) As Byte
End Type

Private Type Struct2
pr(1 To 20) As Integer
End Type

Private Type IN
IsmprA(1 To 40) As Integer
IsmprB(1 To 2) As Struct2
Fr(1 To 2) As Struct1
Fz(1 To 2) As Struct1
End Type

Private Type OUT
OsmprA(1 To 10) As Integer
OsmprB(1 To 2) As Struct2
fRO(1 To 2) As Struct1
End Type

I need to create an equivalent in C#. I have attempted this but get error:-
Cannot have instance field initializers in structs. Help appreciated

Well, my first reflection has to be that you should use classes instead
of structs.

A struct is a value type that usually is used for performance reasons to
reduce the number of objects allocated on the heap, but as you put
reference types in the structs, that's pretty much out the window.

Another reason to use a struct is to get value type semantics, but as
you have arrays in the structs that doesn't work at all.

A struct should not be larger than 16 bytes to perform well. As your
struct Struct1 is eight bytes, the struct IN will be 24 bytes.

So, stick to classes unless you really have a reason to use structs,
they are easier to implement correctly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top