What is the C# equivelant of the Type keyword in VB?

  • Thread starter Thread starter aaa
  • Start date Start date
A

aaa

if I wanted to create the following in C#:



Type Blah
dateTime As DateTimeStruct

BlahCode As String
description As String

systemStatus1Str As String
systemStatus2Str As String
End Type

How would I do this? Via a struct?
 
aaa said:
if I wanted to create the following in C#:



Type Blah
dateTime As DateTimeStruct

BlahCode As String
description As String

systemStatus1Str As String
systemStatus2Str As String
End Type

How would I do this? Via a struct?

Yes:

struct Blah {
....
}
 
Yes... BUT!

Do not use Structure in VB.NET (or struct in C#) unless you _know_ that
you need a _value type_. Otherwise use classes.

What's a value type? If you don't know then you don't need a Structure.
Use a class.

Structures (aka structs) are specialized things in .NET. They are
_absolutely not_ lightweight classes, and trying to use them that way
will bring you no end of grief.
 
Bruce Wood said:
Yes... BUT!

Do not use Structure in VB.NET (or struct in C#) unless you _know_ that
you need a _value type_. Otherwise use classes.

What's a value type? If you don't know then you don't need a Structure.
Use a class.

While I guess I agree with the spirit of what you
mean to say here, I must contest its literal form.

If somebody does not know what a value type is,
they are unlikely to know whether they need one
or not. The right answer, from an engineering
perspective, is to learn the tool, not rely on some
"Rules of Thumb for the Ignorant".
Structures (aka structs) are specialized things in .NET. They are
_absolutely not_ lightweight classes, and trying to use them that way
will bring you no end of grief.

Hmmm. They are very similar to types
that are or can be defined using the 'class'
keyword in C++. I suspect that if I use
them with understanding of their 'value'
semantics, I may hope that the grief they
bring me will be quite limited.
 
Yes, taken literally my advice seems silly, I know.

However, I think that given the history of C-like languages, a sort of
"rule for the unfamiliar" is warranted.

If you would prefer a reworded rule, I would suggest: "Don't use
Structure (in VB.NET) / struct (in C#) unless you've read up on it and
really understand what it's for. Your first impression is likely that
structs are simply 'classes-lite.' If so, you are mistaken."

As far as the grief part goes... trust me... trying to make a Customer
struct, for example, as some sort of "cheap class" and then trying to
do things with it (like put it in a collection) without really
understanding what value types are brings great grief and many hours of
debugging. Been there... tried that... now warning others not to. :)
 
Back
Top