IT Community - Software Programming, Web Development and Technical Support

What is the difference between structures and enumeration?

This is a discussion on What is the difference between structures and enumeration? within the VB.NET Programming forums, part of the Software Development category; What is the difference between structures and enumeration?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > VB.NET Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-26-2007, 12:10 AM
anbuchezhians anbuchezhians is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 77
anbuchezhians is on a distinguished road
Question What is the difference between structures and enumeration?

What is the difference between structures and enumeration?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-28-2007, 12:50 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Thumbs up Re: What is the difference between structures and enumeration?

Hi anbuchezhian

Structures and Enumerations are Value-Types. This means, the data that they contain is stored as a stack on the memory. Classes are Reference-Types, means they are stored as a heap on the memory.
Structures are implicitly derived from a class called System.ValueType. The purpose of System.ValueType is to override the virtual methods defined by System.Object. So when the runtime encounters a type derived from System.ValueType, then stack allocation is achieved. When we allocate a structure type, we may also use the new keyword. We may even make a constructor of a structure, but, remember, A No-argument constructor for a structure is not possible. The structure's constructor should always have a parameter.

So if we define the following structure

Code:
struct MyStruct
{
  public int y,z;
}
and we create a structure type
MyStruct st = new MyStruct();

In case of a class, no-argument constructors are possible. Class is defined using the class keyword.

A struct cannot have an instance field, whereas a class can.


Code:
class A
{
int x = 5; //No error 
...
}

struct
{
int x = 5; //Syntax Error 
}

A class can inherit from one class (Multiple inheritance not possible). A Structure cannot inherit from a structure.

Enum is the keyword used to define an enumeration. An enumeration is a distinct type consisting of a set of named constants called the enumerator list. Every enumeration has an underlying type. The default type is "int". Note: char cant be the underlying data type for enum. First value in enum has value 0, each consequent item is increased by 1.

enum colors {red, green, blue, yellow};

Here, red is 0, green is 1, blue is 2 and so on.
An explicit casting is required to convert an enum value to its underlying type

Code:
int x = (int)colors.yellow;
Hope usefull

Oxygen
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
looping structures in Ruby? vadivelanvaidyanathan Ruby 1 08-18-2007 12:53 AM
difference between an enumeration and a set of preprocessor #defines? prasath C and C++ Programming 1 08-17-2007 11:36 PM
Difference between enumeration and iterator in java? kingmaker Java Programming 3 08-08-2007 02:53 AM
How can do test the Pointers & Structures? sundarraja Software Testing 1 07-30-2007 09:54 PM
Difference of Structures and Classes vigneshgets C and C++ Programming 1 05-24-2007 10:53 AM


All times are GMT -7. The time now is 12:52 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0