Alright, so I'm messing around with some code, and I brought up a good
question to myself.
If creating a class called "Person", and filling that class with variables,
properties like:
Public Class Person
Private mstrName As String
Private mdtBirthDate As Date
Public Property Name() As String
Get
Return mstrName
End Get
Set(ByVal Value As String)
mstrName = Value
End Set
End Property
etc......
What is the benefit of typing out all that code over defining a simple
structure such as:
Structure Person
Public Name As String
Public BirthDate As Date
End Structure
-J 7 1249
"OpticTygre" <op********@adelphia.net> wrote Alright, so I'm messing around with some code, and I brought up a good question to myself.
If creating a class called "Person", and filling that class with variables, properties like:
What is the benefit of typing out all that code over defining a simple structure such as:
The typical answer is because you want to separate the interface from
the implementation such that you can make later adjustments to the
implementation, without affecting the interface.
If, for example, it was later decided that records would be rejected when
the Birthday was entered as being in the 1800's, you could add that as
a requirement in the properties's Let block. If you just had a field member
you could not add it without creating a new interface (and breaking old code
that used the old interface).
LFS
Interesting point. I suppose also, that since it is a class, there is also
the ability to add function, subroutines, events and such that apply
specifically to an object of type "Person" whereas you wouldn't be able to
do something like that with a simple structure. And what's wrong with being
born in the 1800's? It's possible! =) I plan on living forever, and so
far my plan is working perfectly. heheh.
-Jason
"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:OD*************@TK2MSFTNGP14.phx.gbl... "OpticTygre" <op********@adelphia.net> wrote Alright, so I'm messing around with some code, and I brought up a good question to myself.
If creating a class called "Person", and filling that class with variables, properties like:
What is the benefit of typing out all that code over defining a simple structure such as:
The typical answer is because you want to separate the interface from the implementation such that you can make later adjustments to the implementation, without affecting the interface.
If, for example, it was later decided that records would be rejected when the Birthday was entered as being in the 1800's, you could add that as a requirement in the properties's Let block. If you just had a field member you could not add it without creating a new interface (and breaking old code that used the old interface).
LFS
"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:OD*************@TK2MSFTNGP14.phx.gbl... "OpticTygre" <op********@adelphia.net> wrote Alright, so I'm messing around with some code, and I brought up a good question to myself.
If creating a class called "Person", and filling that class with variables, properties like:
What is the benefit of typing out all that code over defining a simple structure such as:
The typical answer is because you want to separate the interface from the implementation such that you can make later adjustments to the implementation, without affecting the interface.
If, for example, it was later decided that records would be rejected when the Birthday was entered as being in the 1800's, you could add that as a requirement in the properties's Let block. If you just had a field member you could not add it without creating a new interface (and breaking old code that used the old interface).
But the OP has a point that those two classes "look" the same to client
code. One of the things I like best about Properties is that if you change
a public field to a property your client code will need to be recompiled,
but it won't need to change.
So you can code the simple Person struct and later add encapsulation with
properties.
David
Using Class properties does allow you to process the user input to the class
and check that it's valid. Also, there are somethings that you can't do with
Structures that you can do with Classes. I'm sure there are many but here
are a couple:
if mystructure is nothing then 'generates a compile error since a
structure is a value type
ArrayLists of structures can't be bound to controls but arraylists of
classes can.
You can get some benefit of structures and classes by using fields in the
class instead of properties, i.e.,
Public Class person
public name string
public birthdate as string
end class
"OpticTygre" wrote: Alright, so I'm messing around with some code, and I brought up a good question to myself.
If creating a class called "Person", and filling that class with variables, properties like:
Public Class Person Private mstrName As String Private mdtBirthDate As Date
Public Property Name() As String Get Return mstrName End Get Set(ByVal Value As String) mstrName = Value End Set End Property
etc......
What is the benefit of typing out all that code over defining a simple structure such as:
Structure Person Public Name As String Public BirthDate As Date End Structure -J
"OpticTygre" <op********@adelphia.net> wrote in message
news:RL********************@adelphia.com... Interesting point. I suppose also, that since it is a class, there is also the ability to add function, subroutines, events and such that apply specifically to an object of type "Person" whereas you wouldn't be able to do something like that with a simple structure.
You can add methods, properties, etc to a structure. The distinction
between a Class and a Structure is that a class is created on the heap, and
a structure is a ValueType created on the stack.
A ValueType which contains only ValueType's for fields will be created
entirely on the stack and won't generate any garbage.
David
"OpticTygre" <op********@adelphia.net> schrieb: Alright, so I'm messing around with some code, and I brought up a good question to myself.
If creating a class called "Person", and filling that class with
variables, properties like:
Public Class Person Private mstrName As String Private mdtBirthDate As Date
Public Property Name() As String Get Return mstrName End Get Set(ByVal Value As String) mstrName = Value End Set End Property
etc......
What is the benefit of typing out all that code over defining a simple structure such as:
Structure Person Public Name As String Public BirthDate As Date End Structure
Using properties instead of public variables has the advantage that you can
add code that checks a property's value when it is set. In addition to
that, properties should be used to model "attributes" of an entity (class).
Structures only make sense when their size is small (they are value types
opposed to classes which are reference types), or when it is important to
make them persistent more easily. In this particular case I would prefer a
class over a structure.
--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Great comments from everyone. I suppose it differs on the application
purpose. Classes with it's internal properties would obviously be better if
planning on using an object repeatedly, whereas a structure might be better
if the object was small, and was only going to be used once or twice during
an application. That's pretty much what I gather from what everyone has
said.
-Jason
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:O8**************@tk2msftngp13.phx.gbl... "OpticTygre" <op********@adelphia.net> schrieb: Alright, so I'm messing around with some code, and I brought up a good question to myself.
If creating a class called "Person", and filling that class with variables, properties like:
Public Class Person Private mstrName As String Private mdtBirthDate As Date
Public Property Name() As String Get Return mstrName End Get Set(ByVal Value As String) mstrName = Value End Set End Property
etc......
What is the benefit of typing out all that code over defining a simple structure such as:
Structure Person Public Name As String Public BirthDate As Date End Structure
Using properties instead of public variables has the advantage that you can add code that checks a property's value when it is set. In addition to that, properties should be used to model "attributes" of an entity (class).
Structures only make sense when their size is small (they are value types opposed to classes which are reference types), or when it is important to make them persistent more easily. In this particular case I would prefer a class over a structure.
-- Herfried K. Wagner [MVP] <URL:http://dotnet.mvps.org/>
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Bob Rock |
last post by:
Hello,
in the last few days I've made my first few attempts at creating mixed C++
managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is
visible in those assemblies from a...
|
by: moi |
last post by:
hi,
im fairly new to c++ and as part of a module at uni im learning it. i know
the basics i suppose, but as our final hand-in we have to alter code we
wrote for an earlier assignment to use...
|
by: Pratts |
last post by:
I am a new one who have joined u plz try to help me bcoz i could not
find ny sutiable answer foer this Question
Qus>>why do we need classes when structures provide similar
functionality??
|
by: Ken Allen |
last post by:
OK, I admit that I have been programming since before C++ was invented,
and I have developed more than my share of assembly language systems,
and even contributed to operating system and compiler...
|
by: David Lozzi |
last post by:
Howdy,
I'm new to classes. Below is my class User. (is this a reserved namespace or
class?) It works great, kind of. If I specify the username and password, the
correct firstname and lastname...
|
by: pmclinn |
last post by:
I've noticed that many programmers use classes to store data about such
things like:
Class Customers
.....Phone
....ID
....Address
End Class....
|
by: thomasfarrow |
last post by:
At work, our development team has a development standards document that
insists Structures should never be used. I'm looking to change this
standard but need a suitable argument in order to make...
|
by: ArmsTom |
last post by:
I was using structures to store information read from a file. That was
working fine for me, but then I read that anything stored in a
structure is added to the stack and not the heap. So, I made...
|
by: Tom Cole |
last post by:
I'm working on a small Ajax request library to simplify some tasks
that I will be taking on shortly. For the most part everything works
fine, however I seem to have some issues when running two...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| | |