Click here to Skip to main content
15,888,351 members
Home / Discussions / C#
   

C#

 
AnswerRe: USB port Pin
Eddy Vluggen17-Sep-15 22:54
professionalEddy Vluggen17-Sep-15 22:54 
QuestionDynamicly created ErrorProvider and garbage collection Pin
TMattC14-Sep-15 20:37
TMattC14-Sep-15 20:37 
AnswerRe: Dynamicly created ErrorProvider and garbage collection Pin
Eddy Vluggen14-Sep-15 23:36
professionalEddy Vluggen14-Sep-15 23:36 
QuestionRe-Get and Set Property Pin
Member 1116162514-Sep-15 20:02
Member 1116162514-Sep-15 20:02 
AnswerRe: Get and Set Property Pin
Peter Leow14-Sep-15 20:27
professionalPeter Leow14-Sep-15 20:27 
AnswerRe: Get and Set Property Pin
OriginalGriff14-Sep-15 21:40
mveOriginalGriff14-Sep-15 21:40 
AnswerRe: Get and Set Property Pin
V.14-Sep-15 21:52
professionalV.14-Sep-15 21:52 
AnswerRe: Get and Set Property Pin
BillWoodruff15-Sep-15 0:42
professionalBillWoodruff15-Sep-15 0:42 
At the risk (to my immortal soul) of appearing to re-write code by my esteemed mentor and colleague, OriginalGriff, I'd like to add that there is almost a "standard practice" in the use of Properties in .NET, for example:
C#
public class Employee
{
    public Employee()
    {

    }

    public void SetEmployeeName(int authorizationcode, string fname, string lname)
    {
        if(AuthorizeOkay(authorizationcode)
        {
            FirstName = fname;
            FamilyName = lname;
        }
        else
        {
            NotifyUnauthorizedAccess(authorizationcode);
        }
    }

    private string _firstName;
    public string FirstName
    {
        private set { _firstName = value; }
        get { return _firstName; }
    }

    private string _familyName;
    public string FamilyName
    {
        private set { _familyName = value; }
        get { return _familyName; }
    }

    public string FullName
    {
        get { return string.Format("{0}, {1}", FirstName, FamilyName); }
    }
}
Notes:

1. the variables declared 'private which begin with an underscore character "_" are called "backing fields."

2. the fact that all the 'get methods in the properties are public allows access by all.

3. the use of private 'set methods insure that the data can be changed only using the publicly exposed 'SetEmployeeName method.

4. the code shows a simulation in pseudo-code of the use of an "authorization code" to illustrate the idea that some vital information may be protected from "unauthorized" access. I'm not saying you "should" do anything like that, just suggesting the possibility.

You might be asking why there are separate properties for first-name and family-name; consider you have a million records and you want to search for duplicate employee last names: possibly, easier to do with only one "field" to search in each record.

Sure, there are lots more "bells and whistles" you could add here: for example, testing in the 'SetEmployeeName method for null values in either first, or family, name input strings.

There are tools, like ReSharper, that will let you click on an auto-property name and will create the private backing field and modified set/get methods for you.
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.


modified 15-Sep-15 7:42am.

Questionhow to compare two data tables having same column and contain some values according to column name using stored procedure in sql dbx? Pin
Member 1198440814-Sep-15 19:06
Member 1198440814-Sep-15 19:06 
GeneralRe: how to compare two data tables having same column and contain some values according to column name using stored procedure in sql dbx? Pin
Richard MacCutchan14-Sep-15 21:33
mveRichard MacCutchan14-Sep-15 21:33 
AnswerRe: how to compare two data tables having same column and contain some values according to column name using stored procedure in sql dbx? Pin
Mycroft Holmes14-Sep-15 21:37
professionalMycroft Holmes14-Sep-15 21:37 
AnswerRe: how to compare two data tables having same column and contain some values according to column name using stored procedure in sql dbx? Pin
ngoj17-Sep-15 3:13
ngoj17-Sep-15 3:13 
Questionseprating axis swept; Pin
Isawyouoo14-Sep-15 12:58
Isawyouoo14-Sep-15 12:58 
QuestionRe: seprating axis swept; Pin
Matt T Heffron14-Sep-15 13:17
professionalMatt T Heffron14-Sep-15 13:17 
AnswerRe: seprating axis swept; Pin
Isawyouoo14-Sep-15 13:57
Isawyouoo14-Sep-15 13:57 
QuestionUrgently need C# code for pushing files from one server to another server Pin
Naga Nanda Kishore14-Sep-15 6:59
Naga Nanda Kishore14-Sep-15 6:59 
AnswerRe: Urgently need C# code for pushing files from one server to another server Pin
Eddy Vluggen14-Sep-15 7:29
professionalEddy Vluggen14-Sep-15 7:29 
AnswerRe: Urgently need C# code for pushing files from one server to another server Pin
OriginalGriff14-Sep-15 8:09
mveOriginalGriff14-Sep-15 8:09 
AnswerRe: Urgently need C# code for pushing files from one server to another server Pin
Gerry Schmitz14-Sep-15 9:26
mveGerry Schmitz14-Sep-15 9:26 
JokeRe: Urgently need C# code for pushing files from one server to another server Pin
Eddy Vluggen14-Sep-15 9:38
professionalEddy Vluggen14-Sep-15 9:38 
GeneralRe: Urgently need C# code for pushing files from one server to another server Pin
Gerry Schmitz14-Sep-15 9:54
mveGerry Schmitz14-Sep-15 9:54 
GeneralRe: Urgently need C# code for pushing files from one server to another server Pin
Eddy Vluggen14-Sep-15 10:05
professionalEddy Vluggen14-Sep-15 10:05 
GeneralRe: Urgently need C# code for pushing files from one server to another server Pin
Gerry Schmitz14-Sep-15 10:29
mveGerry Schmitz14-Sep-15 10:29 
QuestionSerializing a Dictionary of Items where the Value member is a List of instances of an Interface Pin
BillWoodruff14-Sep-15 5:06
professionalBillWoodruff14-Sep-15 5:06 
QuestionRe: Serializing a Dictionary of Items where the Value member is a List of instances of an Interface Pin
Eddy Vluggen14-Sep-15 7:32
professionalEddy Vluggen14-Sep-15 7:32 

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.