Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / March 2007

Tip: Looking for answers? Try searching our database.

Structure vs Class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tim - 28 Mar 2007 17:47 GMT
Hi Gurus,
I am newbie and little confused with Classes(Ref type) and Structures(Value
type).
I know how to use both classes and Structures and also know the difference
between value type and ref type, but I don't see much use of structures.
Please help me by answering the question below.

1. Is there anything a Structure can do and Class cannot.
2. What is the advantage of Structure over Classes
3. When should we use Structure.

Please clear my doubt. I'll appreciate any help you can provide
Thanks, Tim.
james - 28 Mar 2007 18:12 GMT
> Hi Gurus,
> I am newbie and little confused with Classes(Ref type) and Structures(Value
[quoted text clipped - 9 lines]
> Please clear my doubt. I'll appreciate any help you can provide
> Thanks, Tim.

There isn't much value to structures except to improve performance.
Most of the value types in .Net are about the size of a pointer so it
makes sense for Int32 and DateTime to go on the stack instead of the
managed heap.  I don't like structs because you cant set them to null
(so int i = null; won't compile).  There are some gotchas with
valuetypes as well so I would just make everything a class.
Ciaran O''Donnell - 29 Mar 2007 14:34 GMT
I personally find a lot of value in value types (excuse the pun)
Imagine if there where no value types and you did some control manipulation
like this

textbox1.Size = textbox2.Size;

then later on did

textbox1.Size.Height *= 2;

This would double the height of textbox1 and (probably unintentially)double
the height of textbox2. The handy thing about Value types is that every
reference is to a different copy so assigning one from the other copies it.
This is handy when you want this and is also more performant when passing
them as parameters to functions as long as they are lightweight as
recommended. They should represent a complete piece of information in the
same way as the Size, Point, Rectangle structures do.
Structures all help with encapsulation as with the textbox example above,
giving out a reference to a Size class would enable some body to change the
height of the textbox without using the height property. With structs, this
isnt possible as when you get the return value from the Size property, it is
a copy of the textboxes internal data, not a direct reference to it.

I'm not a great technical authore but here is a link to a good document on
the diferences in practice:

http://www.awprofessional.com/content/images/0321245660/items/wagner_item6.pdf

HTH

Signature

Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com

> Hi Gurus,
> I am newbie and little confused with Classes(Ref type) and Structures(Value
[quoted text clipped - 9 lines]
> Please clear my doubt. I'll appreciate any help you can provide
> Thanks, Tim.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.