Re: Classes, Properties, and structures

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Herfried K. Wagner [MVP] (hirf-spam-me-here_at_gmx.at)
Date: 10/24/04


Date: Sun, 24 Oct 2004 23:10:05 +0200


"OpticTygre" <optictygre@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/>


Relevant Pages

  • Classes, Properties, and structures
    ... Alright, so I'm messing around with some code, and I brought up a good ... If creating a class called "Person", and filling that class with variables, ... Private mstrName As String ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Classes, Properties, and structures
    ... >> Public Class Person ... >> Private mstrName As String ... >> Public Property NameAs String ... > opposed to classes which are reference types), or when it is important to ...
    (microsoft.public.dotnet.languages.vb)
  • RE: Passing reference type as method parameter
    ... Hi Maxim, ... reference type object but it has value type semantics, meaning a string is ... Classes are usually reference types, so in the example below the Person ... public static void ChangePersonName ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Passing reference type as method parameter
    ... String is immutable sequence of characters ... > Classes are usually reference types, so in the example below the Person ... > public static void ChangePersonName ...
    (microsoft.public.dotnet.languages.csharp)