Click here to Skip to main content
15,921,454 members
Home / Discussions / C#
   

C#

 
QuestionThat assembly does not allow partially trusted callers Pin
gautam baruah123-Sep-13 22:57
gautam baruah123-Sep-13 22:57 
AnswerRe: That assembly does not allow partially trusted callers Pin
Abhinav S24-Sep-13 17:01
Abhinav S24-Sep-13 17:01 
Questionread and write sector of hard disk in windows 7 and up version Pin
ptvce23-Sep-13 18:40
ptvce23-Sep-13 18:40 
AnswerRe: read and write sector of hard disk in windows 7 and up version Pin
Abhinav S23-Sep-13 19:40
Abhinav S23-Sep-13 19:40 
QuestionUnderstanding changes of the mouse icon Pin
amirpooya23-Sep-13 2:36
amirpooya23-Sep-13 2:36 
AnswerRe: Understanding changes of the mouse icon Pin
Eddy Vluggen23-Sep-13 2:38
professionalEddy Vluggen23-Sep-13 2:38 
AnswerRe: Understanding changes of the mouse icon Pin
V.23-Sep-13 3:01
professionalV.23-Sep-13 3:01 
GeneralRe: Understanding changes of the mouse icon Pin
Eddy Vluggen23-Sep-13 3:32
professionalEddy Vluggen23-Sep-13 3:32 
GeneralRe: Understanding changes of the mouse icon Pin
V.23-Sep-13 3:35
professionalV.23-Sep-13 3:35 
GeneralRe: Understanding changes of the mouse icon Pin
Eddy Vluggen23-Sep-13 5:50
professionalEddy Vluggen23-Sep-13 5:50 
AnswerRe: Understanding changes of the mouse icon Pin
BillWoodruff23-Sep-13 4:55
professionalBillWoodruff23-Sep-13 4:55 
AnswerRe: Understanding changes of the mouse icon Pin
Pete O'Hanlon23-Sep-13 7:07
mvePete O'Hanlon23-Sep-13 7:07 
GeneralRe: Understanding changes of the mouse icon Pin
ExcellentOrg23-Sep-13 8:23
ExcellentOrg23-Sep-13 8:23 
GeneralRe: Understanding changes of the mouse icon Pin
Pete O'Hanlon23-Sep-13 8:41
mvePete O'Hanlon23-Sep-13 8:41 
GeneralRe: Understanding changes of the mouse icon Pin
ExcellentOrg25-Sep-13 20:05
ExcellentOrg25-Sep-13 20:05 
GeneralExample 5-14 - Drill 5-3 Pin
N8tiv23-Sep-13 0:51
N8tiv23-Sep-13 0:51 
GeneralRe: Example 5-14 - Drill 5-3 Pin
Pete O'Hanlon23-Sep-13 1:24
mvePete O'Hanlon23-Sep-13 1:24 
In this example, you've got some stuff wrong. For a start, why do you have a separate method to display the information from each class. You would have been better off overriding a method here. Similarly, I would look at changing the way that you gather information - the Citizen constructor violates way too many good practices. If I were writing this, I would look at something like this
C#
public class Citizen
{
  public string Ssn;
  public int Age; 
  
  public virtual void GetDetails()
  {
     SSN = GetInput("Please enter the citizen's Social Security number: ");
     Age = GetAge();
  }

  public virtual void DisplayInfo()
  {
    Console.WriteLine("Citizen's Information:");
    Console.WriteLine("SSN: {0}", Ssn);
    Console.WriteLine("Age: {0}{1}", Age, Environment.NewLine);
  }

  protected string GetInput(string message)
  {
    Console.WriteLine(message);
    return Console.ReadLine();
  }

  private void GetAge()
  {
     bool success = false;
     int age;
     while (!success)
     {
       string tempAge = GetInput("Please enter the citizen's age: ");
       bool success = int.TryParse(tempAge, age);
       if (!success)
       {
         Console.WriteLine("Unfortunately, you did not enter a valid integer (or whatever message you like here");
       }
     }
  }
}

public class Employee : Citizen
{
  public string Name;
  public string Id;

  public override void GetDetails()
  {
    base.GetDetails();

    Name = GetInput("Please enter the employee's name:");
    Id = GetInput("Please enter the employee's id:");
  }

  public override void DisplayInfo()
  {
    base.DisplayInfo();
    Console.WriteLine("Employee's name: {0}", Name);
    WriteLine("Employee's id: {0}", Id);
  }
}
As you can see, the classes are now largely self contained. We have some common functionality handled very simply for us, and we have a consistent interface that we are coding to. Actually, while I'm demonstrating this, it's a good idea to consider why I'm using fields, rather than properties. There's a commonly held belief that you MUST expose properties rather than fields, but in simple cases like this, that's a load of horse-hooey. If you are only using a property to store a value, and your class isn't doing something like Serialization, then you don't need a property there.

Note that I've just knocked this together in the editor and I've not had much sleep, so there may be the odd minor typo in there. I apologise if that is the case.

GeneralRe: Example 5-14 - Drill 5-3 Pin
N8tiv23-Sep-13 1:45
N8tiv23-Sep-13 1:45 
Questionbug/challenge poll Pin
Per Söderlund22-Sep-13 20:59
Per Söderlund22-Sep-13 20:59 
AnswerRe: bug/challenge poll Pin
Richard MacCutchan22-Sep-13 21:35
mveRichard MacCutchan22-Sep-13 21:35 
GeneralRe: bug/challenge poll Pin
Per Söderlund22-Sep-13 21:40
Per Söderlund22-Sep-13 21:40 
AnswerRe: bug/challenge poll Pin
ExcellentOrg23-Sep-13 8:26
ExcellentOrg23-Sep-13 8:26 
Questionc# on my machine I have installed the virtual webcam and integrated webcam already present. So how we identify that this is virtual webcam Pin
ankur4747@yahoo.com22-Sep-13 20:04
ankur4747@yahoo.com22-Sep-13 20:04 
Questionproblem: webBrowser1.Navigate OR the loaded page? - for a jumping cursor. Pin
_Q12_22-Sep-13 7:39
_Q12_22-Sep-13 7:39 
AnswerRe: problem: webBrowser1.Navigate OR the loaded page? - for a jumping cursor. Pin
Dave Kreskowiak22-Sep-13 8:56
mveDave Kreskowiak22-Sep-13 8:56 

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.