Click here to Skip to main content
15,887,175 members
Home / Discussions / C#
   

C#

 
AnswerRe: mean Pin
v_neil8719-Mar-10 19:43
v_neil8719-Mar-10 19:43 
AnswerRe: mean Pin
Not Active19-Mar-10 19:53
mentorNot Active19-Mar-10 19:53 
QuestionGetting a value from a constructor Pin
av725419-Mar-10 7:22
av725419-Mar-10 7:22 
AnswerRe: Getting a value from a constructor Pin
PIEBALDconsult19-Mar-10 7:27
mvePIEBALDconsult19-Mar-10 7:27 
GeneralRe: Getting a value from a constructor Pin
av725420-Mar-10 2:08
av725420-Mar-10 2:08 
GeneralRe: Getting a value from a constructor Pin
OriginalGriff20-Mar-10 2:55
mveOriginalGriff20-Mar-10 2:55 
GeneralRe: Getting a value from a constructor Pin
av725420-Mar-10 5:32
av725420-Mar-10 5:32 
GeneralRe: Getting a value from a constructor Pin
OriginalGriff20-Mar-10 6:53
mveOriginalGriff20-Mar-10 6:53 
av7254 wrote:
why did you say that public atributes are no ok


This may take a little while to explain:

Assume you have a class: MyClass which contains a string and an int.

public class MyClass
    {
    public string UserName;
    public int UserID;
    ....
    }

You write your class, you test it, you are happy it works.
So happy, you use it to handle all the user identity work in your entire app!
Then the boss comes along, and says he wants the user name to be in two parts in the database: First name, and second name. Oh, and the userID is not an int, it's going to be a GUID.

How many classes do you have to change to implement this? How much code to you have to change, and test, and document?

If instead you had written the class as:

public class MyClass
    {
    private string userName;
    public string UserName
       {
       get { return userName; }
       set { userName = value; }
       }
    private int userID;
    public int UserID
        {
        get { return userID; }
        set { userID = value; }
        }     
    }


When your dumb boss comes along with his changes, how much rework is there to do? Only the one class, because you can change the internals of MyClass without affecting the outside world:

public class MyClass
    {
    private string firstName;
    private string lastName;
    public string UserName
       {
       get { return firstName + " " + lastName; }
       set 
          {
          string[] names = value.Split(' '); 
          if (names.Length != 2)
              {
              throw new ApplicationException("Name must have first and last components"); 
              }
          firstName = names[0];
          lastName = names[1]; 
          }
       }
    private GUID userID;
    public int UserID
        {
        get { return userID.GetHash(); }
        set { userID = GetGUID(value); }
        }     
    }


This is one of the cornerstones of OOP - encapsulation. Never expose your internals more than you have to!

When you are starting off, it seems like a lot of fussing about over nothing! But it very quickly becomes second nature, and does give real benefits - almost from day one. If nothing else, it forces you to think about how your class will be used, and what you want to expose to the outside world. Very often, this affects the internal design in a good way, by the realization that a small change in the original plan can give a more flexible class with wider applications. This leads to more code re-use, and thus better reliability.

I hope that made some sense!

[edit]userID changed to GUID in last example - forgot that when I cut and pasted[/edit]
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace

C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

GeneralRe: Getting a value from a constructor Pin
Phillip Donegan23-Mar-10 6:46
Phillip Donegan23-Mar-10 6:46 
AnswerRe: Getting a value from a constructor Pin
jayantbramhankar19-Mar-10 8:30
jayantbramhankar19-Mar-10 8:30 
GeneralRe: Getting a value from a constructor [modified] Pin
PIEBALDconsult19-Mar-10 8:58
mvePIEBALDconsult19-Mar-10 8:58 
GeneralRe: Getting a value from a constructor Pin
AspDotNetDev19-Mar-10 9:12
protectorAspDotNetDev19-Mar-10 9:12 
GeneralRe: Getting a value from a constructor Pin
PIEBALDconsult19-Mar-10 9:40
mvePIEBALDconsult19-Mar-10 9:40 
GeneralRe: Getting a value from a constructor Pin
av725420-Mar-10 2:12
av725420-Mar-10 2:12 
AnswerRe: Getting a value from a constructor Pin
PIEBALDconsult20-Mar-10 3:29
mvePIEBALDconsult20-Mar-10 3:29 
Questionfrom C++ dll to microsoft C# in .NET Pin
pedefetoll19-Mar-10 4:40
pedefetoll19-Mar-10 4:40 
AnswerRe: from C++ dll to microsoft C# in .NET Pin
Wes Aday19-Mar-10 4:45
professionalWes Aday19-Mar-10 4:45 
GeneralRe: from C++ dll to microsoft C# in .NET Pin
pedefetoll19-Mar-10 4:50
pedefetoll19-Mar-10 4:50 
GeneralRe: from C++ dll to microsoft C# in .NET Pin
Wes Aday19-Mar-10 4:55
professionalWes Aday19-Mar-10 4:55 
Questionproblem in reading .csv files Pin
montu337719-Mar-10 4:19
montu337719-Mar-10 4:19 
AnswerRe: problem in reading .csv files Pin
Wes Aday19-Mar-10 4:41
professionalWes Aday19-Mar-10 4:41 
GeneralRe: problem in reading .csv files Pin
montu337719-Mar-10 4:59
montu337719-Mar-10 4:59 
GeneralRe: problem in reading .csv files Pin
Wes Aday19-Mar-10 5:13
professionalWes Aday19-Mar-10 5:13 
AnswerThe easiest solution Pin
Ennis Ray Lynch, Jr.19-Mar-10 5:14
Ennis Ray Lynch, Jr.19-Mar-10 5:14 
AnswerRe: problem in reading .csv files Pin
PIEBALDconsult19-Mar-10 6:22
mvePIEBALDconsult19-Mar-10 6:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.