473,763 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Classes, Properties, and structures

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
Nov 21 '05 #1
7 1273

"OpticTygre " <op********@ade lphia.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

Nov 21 '05 #2
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*******@usin ternet.com> wrote in message
news:OD******** *****@TK2MSFTNG P14.phx.gbl...

"OpticTygre " <op********@ade lphia.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

Nov 21 '05 #3

"Larry Serflaten" <se*******@usin ternet.com> wrote in message
news:OD******** *****@TK2MSFTNG P14.phx.gbl...

"OpticTygre " <op********@ade lphia.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
Nov 21 '05 #4
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

Nov 21 '05 #5

"OpticTygre " <op********@ade lphia.net> wrote in message
news:RL******** ************@ad elphia.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
Nov 21 '05 #6
"OpticTygre " <op********@ade lphia.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/>
Nov 21 '05 #7
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******** ******@tk2msftn gp13.phx.gbl...
"OpticTygre " <op********@ade lphia.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/>

Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
741
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 managed point-of-view I've noticed that: 1) for each managed and unmanaged C function (not C++ classes) I get a public managed static method (defined on a 'Global Functions' class) in the generated assembly with an export name of the form...
6
2641
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 classes as opposed to structures. the program basically modelled a simple polygon using point, line and polyogon structures. and had various functions to find the properties of the shape such as area, perimeter, etc. the question reads along the...
14
3800
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??
6
4488
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 systems over the years. I have developed code in more than 30 distinct programming languages for a wide cariety of industries. But this issue of structures in C# is beginning to annoy me. I should also make it clear that I am not a big supporter...
6
1582
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 are returned. For example, username dlozzi, password fun. It returns David Lozzi as full name. If I login as someone else on another computer, say username dsmith and password fun2, the second computer displays the correct fullname. HOWEVER if I...
14
15088
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....
2
2410
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 the change. I know that Structures are value types, sit on the stack, and are generally more efficient to manipulate than reference types (i.e. Classes). Structures cannot use inheritance, the finalize method or default constructors. Can anyone...
3
1552
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 a class that stores the same information. The user selects any number of records from the file when the program loads & can then make changes. The records the user selects are added to an array and changes are made to the records in that array...
18
1929
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 requests at the same time. The first one stops execution as the second continues. If I place either an alert between the two requests or run the second through a setTimeout of only 1 millisecond, they both work. You can see a working example here:...
0
9566
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9389
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9943
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8825
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7370
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5410
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3918
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3529
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.