Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Type" Equivalent in VB.net 3

Status
Not open for further replies.

RoguePoet01

Programmer
Oct 1, 2003
302
US
Hi all,

In VB6 I found the "type" variable very useful. It was essentially a class, as far as I could tell.

Is there an equivalent in VB.net?

Would I just declare a class instead of a type?

It used to be:

Type CarType
Year as Integer
Color as String
Doors as Integer
Miles as Long
End Type
Dim Car as CarType

What's the equivalent now?

Thanks.
 
Public Structure CarStruct
Public Year As Integer
Public Color As String
Public Doors As Integer
Public Miles As Long
End Structure
Dim Car As CarStruct

Scott
Programmer Analyst
<{{><
 
You should be aware that structures are value types (lower-case &quot;t&quot;) in .NET, and not reference types (think object variables). The difference is: If you want to add a bunch of structure instances to a collection, the .NET framework has to do what is called &quot;boxing&quot;, which is a small object which encapsulates your non-object structures. It can add quite a bit of memory overhead, as well as time-consuming boxing and un-boxing operations when .NET needs to access elements in the collection (very slow!)

If you think you'll need to store it in a collection, it's best to start with a class, and not a structure.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
I'm still getting the hang of this &quot;class&quot; business. Lot's to learn.

I liked &quot;types&quot; in VB6; it was a real neat little way to store stuff.

As it happens, usually when I ask a quesion here, I'll go on to find the answer myself - but I can never seem to find the answer unless I ask the question.

Tek-Tips Zen, I guess.
 
A class is a structure, but with it's own private code added.

Ideally, your class should contain data that is related to the purpose of the class (and no other un-related data), plus the operations that can be performed on that data (and no unrelated operations). So if you have a class named &quot;Dog&quot;, it would have data for name, breed, furcolor, collarcolor, etc. It would also have methods that only dogs do -- bark(), catchflyingdisc(), chasecat(), etc.

If you have data that is unrelated to the class in there, it's called being &quot;unfocused&quot; -- there's no sense having a Dog that has a BankBalance property. If there are methods that are unrelated to the class, it turns the class into what is called a &quot;kitchen sink&quot; class. There's no reason to have a method in a Dog class named JumpOverMoon() (cows have that method, not dogs).

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top