I have a collection NameList which contains multiple objects of PersonName class.
I want to insert that NameList using a stored procedure into a table tbl_namelist in a single insert operation. But before the insert, it must delete all records from tbl_namelist.
public class NameList : ObservableCollection<PersonName>
{
public NameList() : base()
{
Add(new PersonName("Willa", "Cather"));
Add(new PersonName("Isak", "Dinesen"));
Add(new PersonName("Victor", "Hugo"));
Add(new PersonName("Jules", "Verne"));
}
}
public class PersonName
{
private string firstName;
private string lastName;
public PersonName(string first, string last)
{
this.firstName = first;
this.lastName = last;
}
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
}
Related
Can anybody please tell me, how to realise my function in main body ?
All works, but i want to do a catalog of employers, so how to write employe to list or massive?
class Catalog : Employe
{
Employe[] employes = new Employe[10];
Employe p1 = new Employe(14, "Mark", "James", 124151, "Coder", 4000);
public Catalog(int _age, string _firstName, string _lastName, int _id, string _job, int _salary) : base(_age, _firstName, _lastName, _id, _job, _salary)
{
employes[1] = p1;
}
public void CatalogLog()
{
for(int i = 0; i < employes.Length; i++)
Console.WriteLine(employes[i]);
}
}
class TestInheritence
{
public static void Main(string[] args)
{
Employe[] employes = new Employe[10];
}
}
I think you didn't set up the inheritance hierarchy with the right logic. The base class Employee is extensible and contains base methods:
public class Employee
{
private int _id;
private string _firstName;
public Employee(int id, string firstName)
{
_id = id;
_firstName = firstName;
}
public int GetID()
{
return _id;
}
public void SetID(int id)
{
if(id > 0)
_id = id;
}
public void Print()
{
Console.WriteLine("ID: {0}\tFirst Name: {1}", this._id, this._firstName);
}
}
The derived class allows the object to expand by adding new methods and properties to the properties of the base class:
public class Manager : Employee
{
private string _city;
public Manager(int id, string firstName, string city) : base(id, firstName)
{
_city = city;
}
public string GetCity()
{
return _city;
}
}
To test how these two classes work, you can review the application code below:
public class Program
{
public static void Main()
{
Employee[] employees = new[]
{
new Employee(1, "Thomas"),
new Employee(2, "John"),
new Employee(3, "Erick"),
new Employee(4, "Ahmet"),
new Employee(5, "Sun")
};
employees[0].Print();
Manager manager = new Manager(6, "Johnson", "London");
manager.Print();
Console.WriteLine("City: {0}", manager.GetCity());
}
}
If you want to add the employees to the catalog you can have an 'add' function that will add the employee to the catalog:
class Catalog : Employe
{
List<Employe> employes = new List<Employe>();
public void AddEmployee(int _age, string _firstName, string _lastName, int _id, string _job, int _salary) : base(_age, _firstName, _lastName, _id, _job, _salary)
{
Employe p1 = new Employe(_age, _firstName, _lastName, _id, _job, _salary);
employes.Add(p1);
}
public void CatalogLog()
{
for(int i = 0; i < employes.Count(); i++)
Console.WriteLine(employes[i]);
}
}
class TestInheritence
{
public static void Main(string[] args)
{
Catalog catalog = new Catalog();
catalog.AddEmployee(14, "Mark", "James", 124151, "Coder", 4000);
// Add more employees.
}
}
But I think that this is the wrong use case for inheritance. Usually you want to inherit when there is a 'Is-a' relationship between the types. But 'Catalog' is not type of 'Employee'
In WPF, I want to create three textboxes for the properties: FullName, FirstName and LastName. The text for these textboxes will be based on the listbox on the left (as shown in the program image below). I'm already done with getting text from the listbox to the textbox but I want to sync the values for the names such that:
changing the value for FullName, changes the listbox, FirstName and LastName box
and changing the value for FirstName and LastName changes the FullName and listbox
Here is what the program looks like:
(Program image)
OK thanks to Jared,I was able to solve the problem by making small changes to his answer but I dont like my solution lol can someone suggest a better revision to this program?
class Student : INotifyPropertyChanged
{
private string name;
public string Name
{
get
{
return name;
}
set
{
FirstName = value.Split(" ".ToCharArray())?[0];
LastName = value.Split(" ".ToCharArray())?[1];
OnPropertyChanged("Name");
OnPropertyChanged("FirstName");
OnPropertyChanged("LastName");
}
}
private string firstName;
public string FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
name = value + " " + LastName;
OnPropertyChanged("FirstName");
OnPropertyChanged("Name");
}
}
private string lastName;
public string LastName
{
get
{
return lastName;
}
set
{
lastName = value;
name = FirstName + " " + value;
OnPropertyChanged("LastName");
OnPropertyChanged("Name");
}
}
private Course _course;
public Course Course
{
get { return _course; }
set
{
_course = value;
OnPropertyChanged("Course");
}
}
private int _age;
public int Age
{
get { return _age; }
set
{
_age = value;
OnPropertyChanged("Age");
}
}
private DateTime _birthday;
public DateTime Birthday
{
get { return _birthday; }
set
{
_birthday = value;
OnPropertyChanged("Birthday");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
I believe what you'll need is a class such as the one below. You'd then bind your listbox to a collection of these, and the form elements to the currently selected item.
I added a space between first and last name. If you want that removed, you'll need to find another way of splitting the first and last names, such as checking capitalization.
Since FullName will be set regardless of which property is modified, I put all the FirePropertyChanged calls in its setter. I haven't tested this, but I assume it will work. :)
EDIT: Changed FullName setter to set the private variable of firstName and lastName instead of the public property.
public class Student : INotifyPropertyChanged
{
public string FullName
{
get
{
return FirstName + " " + LastName;
}
set
{
firstName = value.Split(" ".ToCharArray())?[0];
lastName= value.Split(" ".ToCharArray())?[1];
FirePropertyChanged("FullName");
FirePropertyChanged("FirstName");
FirePropertyChanged("LastName");
}
}
private string firstName;
public string FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
FullName = value + " " + LastName;
}
}
private string lastName;
public string LastName
{
get
{
return firstName;
}
set
{
lastName = value;
FullName = FirstName + " " + value;
}
}
public void FirePropertyChanged (string PropertyName)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(PropertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
I learning C#. I want to see what is the best way to implement inheritance. I have a Employee base class and a PartTime derived class. Employee class only receives First and Last name and has a method to print full name.
I want to know what is the proper way to pass First and last name so that when I just call PartTime class I should be also able to print full name from the calling program. At the moment it is showing blank as full name:
class Program
{
static void Main(string[] args)
{
Employee emp = new Employee("John", "Doe");
// emp.PrintFullName();
PartTime pt = new PartTime();
float pay=pt.CalcPay(10, 8);
pt.PrintFullName();
Console.WriteLine("Pay {0}", pay);
Console.ReadKey();
}
}
public class Employee
{
string _firstName;
string _last_name;
public Employee(string FName, string LName)
{
_firstName = FName;
_last_name = LName;
}
public Employee() { }
public void PrintFullName()
{
Console.WriteLine("Full Name {0} {1} ", _firstName, _last_name);
}
}
public class PartTime : Employee
{
public float CalcPay(int hours, int rate)
{
return hours * rate;
}
}
You can call the base class constructor from you derived class like this:
public class PartTime : Employee
{
public PartTime(string FName, string Lname)
: base(FName, LName)
{ }
}
and then create it,
PartTime pt = new PartTime("Part", "Time");
Try this:
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Employee(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
//method implementations removed for clarity
}
public class PartTime:Employee
{
public PartTime(string firstName, string lastName)
: base(firstName, lastName)
{
}
}
Note that your base constructor will run before any code in your derived constructor, should you need further initialization logic in the PartTime class.
You want to add a constructor to PartTime that will pass along the first and last name to the base constructor
public PartTime(string fName, string lName) : base(fName, lName) {
}
Or you could make first and last name public properties on Employee which would be inherited by PartTime. Then you can initialize them when creating instances of either without having to maintain the PartTime constructor.
class Program
{
static void Main(string[] args)
{
Employee emp = new Employee { FirstName = "John", LastName = "Doe" };
emp.PrintFullName();
PartTime pt = new PartTime { FirstName = "Jane", LastName = "Doe" };
float pay=pt.CalcPay(10, 8);
pt.PrintFullName();
Console.WriteLine("Pay {0}", pay);
Console.ReadKey();
}
}
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public void PrintFullName()
{
Console.WriteLine("Full Name {0} {1} ", FirstName, LastName);
}
}
public class PartTime : Employee
{
public float CalcPay(int hours, int rate)
{
return hours * rate;
}
}
I am trying to create a web service for updating a simple database table. I have an update method which takes a parameter an object of type Employee. I have included a reference of namespace where Employee class belongs. For a reason that I can't understand I receive the following error:
Inconsistent accessibility: parameter type 'EmployeeDBApplication.Employee' is less accessible than method 'EmployeeStoreWS.EmployeeStoreService.update(EmployeeDBApplication.Employee)'
class Employee
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
private double salary;
public double Salary
{
get { return salary; }
set { salary = value; }
}
private string address;
public string Address
{
get { return address; }
set { address = value; }
}
private string firstname;
public string Firstname
{
get { return firstname; }
set { firstname = value; }
}
private string lastname;
public string Lastname
{
get { return lastname; }
set { lastname = value; }
}
public override string ToString() {
string x;
x = "Employee ID:" + this.id + "\tName:" + this.firstname + "\t" + this.lastname + "\n\tSalary:" + this.salary + "\t Address:" + this.address;
return x;
}
}
And the web service:
public class EmployeeStoreService : System.Web.Services.WebService
{
//id int(11) NO PRI 0
//firstname varchar(255) YES
//lastname varchar(255) YES
//address varchar(255) YES
//salary double YES
[WebMethod]
public MySqlConnection getConnection()
{
return new MySqlConnection("Database=sakila;Data Source=localhost;User Id=root;Password=george 01");
}
[WebMethod]
public void update(Employee employee)
{
MySqlConnection connection = null;
try
{
connection = getConnection();
connection.Open();
MySqlCommand myCommand = new MySqlCommand("UPDATE employee SET (?id,?firstname,?lastname,?address,?salary) WHERE employee.id = ?id");
myCommand.Prepare();
myCommand.Parameters.AddWithValue("?id", employee.Id);
myCommand.Parameters.AddWithValue("?firstname", employee.Firstname);
myCommand.Parameters.AddWithValue("?lastname", employee.Lastname);
myCommand.Parameters.AddWithValue("?address", employee.Address);
myCommand.Parameters.AddWithValue("?salary", employee.Salary);
myCommand.ExecuteNonQuery();
connection.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (connection != null)
connection.Close();
}
}
}
You need to make your class public.
public class Employee
It's complaining that some of your class members are public while the class itself is not. classes are Internal by default
Try changing this
class Employee
{
private int id;
//...
To this
public class Employee
{
private int id;
//...
Unless you have a specific reason not to, define Employee as public.
Your update method is public where as your class is internal. hence the message. When you dont specify an access modifier, it is internal.
I have a simple custom class (Person), which I want to bind to a label as a whole (not to separate properties of this class). The label should just present whatever the Person.ToString() returns (in this case FirstName + LastName).
How do I properly bind it using the person as a Source.
How do I make sure that any change in one of the properties of the Person will be reflected in the label?
public class Person : INotifyPropertyChanged {
private string firstName;
public string FirstName {
get {
return firstName;
}
set {
firstName = value;
OnPropertyChanged("FirstName");
}
}
private string lastName;
public string LastName {
get {
return lastName;
}
set {
lastName = value;
OnPropertyChanged("LastName");
}
}
public override string ToString() {
return FirstName + " " + LastName;
}
private void OnPropertyChanged(string name) {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
public Window1() {
myPerson = new Person() {
FirstName = "AAA",
LastName = "BBB"};
InitializeComponent();
}
public Person MyPerson {
get {
return myPerson;
}
set {
myPerson = value;
}
}
Label Content="{Binding Source=MyPerson}"
Create a new property FullName which returns the full name and raise PropertyChanged for FullName in the setters of FirstName and LastName as well. You should never bind to the object itself.