|
"Make the necessary changes to Example 5-14 to make the Employee
class a subclass of the Citizen class. Display some properties from
the Citizen class, such as the SSN and Age. The output should look
something like:
Citizen's Information:
SSN: 555-55-5555
Age: 36
Job Information:
Company Name: Pille Mandla
Company ID: 123-WxYz"
I'm about halfway done with my certification e-book/course… This was one of the little homework assignments…
I realize I could have done it two different ways, I was wondering if there was any other way of accomplishing this…
I realize it is just a simple program, but… As far as performance wise (let's pretend it's a larger program), which way is the most optimal?
Here is what I done:
using System;
public class Citizen
{
private string ssn;
private string age;
public string SSN
{
get { return ssn; }
set { ssn = value; }
}
public string Age
{
get { return age; }
set { age = value; }
}
public void DisplayCitizenInfo()
{
Console.WriteLine("Citizen's Information:");
Console.WriteLine("SSN: {0}", ssn);
Console.WriteLine("Age: {0}\n", age);
}
public Citizen()
{
Console.WriteLine("Please enter the citizens Social Security number: ");
SSN = Console.ReadLine();
Console.WriteLine("Please enter the citizens age: ");
Age = Console.ReadLine();
}
}
public class Employee : Citizen
{
private string name;
private string id;
public string Name
{
get { return name; }
set { name = value; }
}
public string Id
{
get { return id; }
set { id = value; }
}
public void DisplayInfo()
{
Console.WriteLine("Employee's name: {0}", name);
Console.WriteLine("Employee's id: {0}", id);
}
}
class MyClass
{
static void Main(string[] args)
{
Employee emp = new Employee();
Console.Write("Please enter the employee's name: ");
emp.Name = Console.ReadLine();
Console.Write("Please enter the employee's id: ");
emp.Id = Console.ReadLine();
emp.DisplayInfo();
}
}
|
|
|
|
|
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
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.
|
|
|
|
|
Okay, thank you for your response and your feedback/constructive criticism…
I myself haven't had much sleep when I finished this little exercise… I should have set it up a little better…
This exercise came at the end of chapter 5, it was discussing the basics of inheritance, properties, read-only and const keywords…
I'm guessing the book just wanted me to practice writing code for inheritance and properties…
With that said, I do realize I could have just created an object in the main method to ask the user to enter a Social Security number and age…
I just want to practice, and throwing in a constructor…
|
|
|
|
|
Have you guys ever found a bug or misbehaviour that you were unable to correct?
I´ve had a bug haunting me for weeks that makes me wonder if there are unsolveable bugs out there.
I lack experience since ive only been coding proffessionally for 5 years.
If you had one,what was it and what made you give up?
(I´m coding in C#, thats why i posted it on this forum.)
|
|
|
|
|
Söderlund wrote: I´m coding in C#, thats why i posted it on this forum Well, it's the wrong place, this forum is for technical questions; try the Lounge[^] for this type of discussion.
Veni, vidi, abiit domum
|
|
|
|
|
|
What is ur bug.... I think in one of the Dilbert's cartoon, they'd once shown using a real hammer to debug the application and against properly aimed hammer, no bugs (or hard drives carrying the bugs) stand a chance.
|
|
|
|
|
I am working on automatically find which webcam is working properly on my machine. Condition: 1) If you have two webcam one working and another one is not working. And the not working is in 0th position so is comes first so code taking 0th position.
2) If you installed a virtual webcam and integrated webcam already present so how to identify that which is a virtual or working webcam.
|
|
|
|
|
For each (char)character I wrote in textBox1, the webBrowser1.Navigate is fired.
The problem is that the mouse cursor JUMP from my textbox1 into webBrowser1, and I don't figure out WHY is doing that on this PARTICULAR PAGE(see code). On other pages i see is working fine, but here, is going nuts. I think is (part) because of the webBrowser1.Navigate method, and (part) because of the page. I need help on this matter. Its impossible for me to resolve it.
private void textBox1_TextChanged(object sender, EventArgs e)
{
webBrowser1.Navigate("http://translate.google.com/?hl=en&tab=wT#en|ro|" + textBox1.Text);
}
Thanks.
|
|
|
|
|
It's doing that probably because the page is coded to set one of its textboxes as the one that souls have the focus. There's nothing you can do to change that behavior. A better solution would be to not use Google's Translate page but instead use a service to do the translation and your code just displays the result in a textbox. You're relying on things that you have no control over.
|
|
|
|
|
ok, thanks... but is a very big loss,pity,...eh. |
|
|
|
|
|
Can you direct me to a sample for using what you suggesting: "use a service to do the translation"?
It is real? There are dictionaries other than sites on internet? I downloaded one with 30k word + samples, synonyms and explanations for each word, but i never been able to put it in practice. Its a very elaborate PERSONAL project... and i'm single(no help from anybody). I am crazy to even think to such a project. But, please enlighten me -- i do it for fun, and with love for what i like.
Thanks again.
|
|
|
|
|
|
is there any C# developer in Thailand?
|
|
|
|
|
Yes.
There are C# developers in Thailand.
However, this is not a jobs board.
The only instant messaging I do involves my middle finger.
|
|
|
|
|
|
Sawasdee Khrup, Khun Sunsern.
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
|
|
|
|
|
Hi Bill,
How're you?
I'm looking for some good C# developers to join my project in Bangkok. Would you suggest? (Thai developer)
Best regards
|
|
|
|
|
1-How to make a sandbox that sandboxing and preventing Malicous Java script at URL address bar with such permissions like never change the title page , never change the images or flash or any other multimedia from outside in the page and never excute a code from outside ?
2-after detecting and preventing through Sandbox , how to take the piece of code which causes the problem from a big malicious script ? e.g. eval() is the responsible for making the problem so i want the program to catch for the 3rd step
3-if for example eval() is the bad function which causes the trouble then how to REPLACE it -mapping- with a safe function? -lets say only a mapping for eval() ,execScript , SetTimeOut , SetTimeInterval
innerHTML , outerHTML document.cookies, location.replace , location.assign , script.src href , src , Iframe -
I red many articles but i didnt find safe mapping for unsafe functions
What are the safe mapping for those unsafe functions or properties??
I red this it was helpful but not complete:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval[^]
|
|
|
|
|
|
I need to set voucher print c# window form application How i can set that please Help Me.....
|
|
|
|
|
|
Hi guys, I need Basic Basic Silverligt Projects Pls suggest me a link. Thanks in Advance
|
|
|
|
|
|