Accessing data members of an inherited class - c#

So I have 4 classes: Employee (base class), PartTime : Employee, FullTime : Employee, Manager : Employee. I'm trying to access unique but can't figure out exactly how. I tried casting but that didn't work. Here's what I have so far.
Employee emp1 = new Manager();
emp1.FirstName = txtFirst.Text;
emp1.LastName = txtLast.Text;
emp1.Ssn = Convert.ToInt32(txtSSN.Text);
emp1.HireDate = Convert.ToInt32(txtHire.Text);
emp1.TaxRate = Convert.ToDecimal(txtTax.Text);
emp1.Email = txtEmail.Text;
emp1.PhoneNum = Convert.ToInt32(txtPhone);
if (emp1 is PartTime)
{
emp1.HourlyRate = txtRate.Text;
emp1.HoursWorked = txtHrs.Text;
}
if (emp1 is FullTime)
{
emp1.Salary = Convert.ToDecimal(txtSalary.Text);
emp1.VacationDays = Convert.ToDouble(txtVacation.Text);
emp1.SickDays = Convert.ToDouble(txtSick.Text);
emp1.IsTaxExempt = comboTax.SelectedIndex == 0 ? true : false;
emp1.HasInsurance = comboInsurance.SelectedIndex == 0 ? true : false;
}
if (emp1 is Manager)
{
(Manager)emp1.BonusEarned = Convert.ToDecimal(txtBonus.Text);
(Manager)emp1.Department = comboDepartment.SelectedText;
(Manager)emp1.OfficeLocation = txtOffice.Text;
}
In this example, Manager has the properties BonusEarned, Department, and OfficeLocation but Employee, FullTime, and PartTime don't.

Try this (pay attention to the parenthesis):
((Manager)emp1).BonusEarned = Convert.ToDecimal(txtBonus.Text);
((Manager)emp1).Department = comboDepartment.SelectedText;
((Manager)emp1).OfficeLocation = txtOffice.Text;

Ugh, I think that is just invalid syntax. You're doing cast in the LHS of an assignment statement... It doesn't work like that. Cast has to be on the RHS so the result can be assigned. Instead try something like this;
if (emp1 is Manager)
{
var man = (Manager)emp1
man.BonusEarned = Convert.ToDecimal(txtBonus.Text);
man.Department = comboDepartment.SelectedText;
man.OfficeLocation = txtOffice.Text;
}

Related

Check if column exists when iterating datatable?

I'm taking a datatable and serializing it as geojson. I'm using linq for this:
var envelope = new
{
type = "FeatureCollection",
features = dataTable.AsEnumerable().Select(record => new {
type = "Feature",
properties = new
{
Name = Convert.ToString(record["Name"]),
Date = Convert.ToString(record["Date"]),
Icon = Convert.ToString(record["imageUrl"]),
//ReportMonth = Convert.ToString(record["Month"]),
ReportMonth = (!string.IsNullOrEmpty(record["Month"])) ? Convert.ToString(record["ReportMonth"]) : string.Empty
},
geometry = new
{
type = "Point",
coordinates = new[] {
Convert.ToDecimal(record["Lon"]),
Convert.ToDecimal(record["Lat"])
}
}
}).ToArray()
};
This works when the datatable has all the columns. When the column doesn't exist in the datatable (ie. column Month) then the iteration fails.
Is there a way to check if the column exists? I tried using a ternary operator to check the value, but it obviously won't work since I'm still checking if the value exists.
You can use:
ReportMonth = record.Table.Columns.Contains("Month")
? Convert.ToString(record["Month"])
: string.Empty;
Convert.ToString(object) returns string.Empty if the object is null so we don't need to check it.
Here is a speed performance optimization:
bool hasName = dataTable.Columns.Contains("Name");
bool hasDate = dataTable.Columns.Contains("Date");
bool hasimageUrl = dataTable.Columns.Contains("imageUrl");
bool hasMonth = dataTable.Columns.Contains("Month");
bool hasLon = dataTable.Columns.Contains("Lon");
bool hasLat = dataTable.Columns.Contains("Lat");
var envelope = new
{
// use: ReportMonth = hasMonth ? ... : ... ;
}
You could try record.Table.Columns.Contains(...).

F#: Why those two collections are not equal?

Was writing some units using XUnit until that at some points I bumped into something surprising:
let id = Guid.Empty
let contact = {
Name = {
FirstName = "Marcel"
MiddleInitial = None
LastName = "Patulacci"
}
DateOfBith = new DateTime(1850, 12, 25)
Address = {
Address1 = "41 av 8 Mai 1945"
Address2 = None
City = "Sarcelles"
State = None
Zip = "95200"
}
PhoneNumber = {
DialOutCode = 33
LocalNumber = "766030703"
}
Email = "marcel.patulacci#outlook.com"
}
[<Fact>]
let ``Open an account...``() =
let event = Event.AccountOpened({
AccountId = id
Contact = contact
})
let a = [event]
let b = seq { yield event }
Assert.Equal(a, b)
System.NullReferenceException : Object reference not set to an instance of an object.
It seems surprising especially since considering that the overload used by Assert is:
public static void Equal<T>(IEnumerable<T> expected, IEnumerable<T> actual)
Which states that:
Verifies that two sequences are equivalent, using a default comparer.
Why are they considered different, and why does Assert.Equal raise a System.NullReferenceException?
[EDIT]
System.NullReferenceException : Object reference not set to an instance of an object.
at Domain.Events.AccountOpenedEvent.Equals(Object obj, IEqualityComparer comp)
at Domain.Events.Event.Equals(Object obj, IEqualityComparer comp)
Seems
type PersonalName = {
FirstName: string;
MiddleInitial: string option;
LastName: string;
}
type Address = {
Address1: string;
Address2: string option ;
City: string;
State: string option;
Zip: string;
}
type PhoneNumber = {
DialOutCode : int;
LocalNumber: string
}
type Contact = {
Name: PersonalName;
DateOfBith: DateTime
Email: string;
Address: Address;
PhoneNumber: PhoneNumber
}
type AccountOpenedEvent = {
AccountId: Guid
Contact: Contact
}
type Event =
| AccountOpened of AccountOpenedEvent
It turns out one of the fields of event was null, but not event itself.
The problem resided in the id and contact that were defined right above the test / [<Fact>]:
let id = Guid.Empty
let contact = {
Name = {
FirstName = "Marcel"
MiddleInitial = None
LastName = "Patulacci"
}
DateOfBith = new DateTime(1850, 12, 25)
Address = {
Address1 = "41 av 8 Mai 1945"
Address2 = None
City = "Sarcelles"
State = None
Zip = "95200"
}
PhoneNumber = {
DialOutCode = 33
LocalNumber = "766030703"
}
Email = "marcel.patulacci#outlook.com"
}
[<Fact>]
let ``Open an account...``() =
let event = Event.AccountOpened({
AccountId = id
Contact = contact
})
let a = [event]
let b = seq { yield event }
Assert.Equal(a, b)
The thing is when running the test independently the id and contact are not initialized, hence even though event was not null, contact was null (id being a Guid aka a struct it has a value anyway).
Since F# works with structural equality, if one of the field is not initialized it was enough to have a field null to make the Assert failed at some point in its implementation.
There are a few solutions / workarounds:
Defining those variables directly in the unit test body.
Defining methods which produce those values out of the unit test body
let getId() = Guid.Empty
let getContact() = {
Name = {
FirstName = "Marcel"
MiddleInitial = None
LastName = "Patulacci"
}
DateOfBith = new DateTime(1850, 12, 25)
Address = {
Address1 = "41 av 8 Mai 1945"
Address2 = None
City = "Sarcelles"
State = None
Zip = "95200"
}
PhoneNumber = {
DialOutCode = 33
LocalNumber = "766030703"
}
Email = "marcel.patulacci#outlook.com"
}
[<Fact>]
let ``Open an account...``() =
let id = getId()
let contact = getContact()
let event = Event.AccountOpened({
AccountId = id
Contact = contact
})
let a = [event]
let b = seq { yield event }
Assert.Equal(a, b)
While those workarounds work, I am surprised that the variables declared right above the unit test function are not considered when the test is running / and are uninitialized.
It might worth to shoot another question about why this is the case.
This is surprising in the sense that if a function can be defined and returning pretty much the same thing as those variables it means that let is also properly compiled, so why this is not the case with the variables?

Listing in WCF Entity

I have a problem with LINQ query (see comment) there is a First method and it only shows me the first element.
When I write in the console "Sales Representative" it shows me only the first element of it as in
I would like to get all of data about Sales Representative. How can I do it?
public PracownikDane GetPracownik(string imie)
{
PracownikDane pracownikDane = null;
using (NORTHWNDEntities database = new NORTHWNDEntities())
{
//Employee matchingProduct = database.Employees.First(p => p.Title == imie);
var query = from pros in database.Employees
where pros.Title == imie
select pros;
// Here
Employee pp = query.First();
pracownikDane = new PracownikDane();
pracownikDane.Tytul = pp.Title;
pracownikDane.Imie = pp.FirstName;
pracownikDane.Nazwisko = pp.LastName;
pracownikDane.Kraj = pp.Country;
pracownikDane.Miasto = pp.City;
pracownikDane.Adres = pp.Address;
pracownikDane.Telefon = pp.HomePhone;
pracownikDane.WWW = pp.PhotoPath;
}
return pracownikDane;
}
Right now you are just getting the .First() result from the Query collection:
Employee pp = query.First();
If you want to list all employees you need to iterate through the entire collection.
Now, if you want to return all the employee's you should then store each new "pracownikDane" you create in some sort of IEnumerable
public IEnumerable<PracownikDane> GetPracownik(string imie) {
using (NORTHWNDEntities database = new NORTHWNDEntities())
{
var query = from pros in database.Employees
where pros.Title == imie
select pros;
var EmployeeList = new IEnumerable<PracownikDane>();
foreach(var pp in query)
{
EmployeeList.Add(new PracownikDane()
{
Tytul = pp.Title,
Imie = pp.FirstName,
Nazwisko = pp.LastName,
Kraj = pp.Country,
Miasto = pp.City,
Adres = pp.Address,
Telefon = pp.HomePhone,
WWW = pp.PhotoPath
});
}
return EmployeeList;
}
Then, with this returned List you can then do what ever you wanted with them.

Get ID value from combo box with entity framework

With ADO.net, if I want to retrieve the ID from combobox I just do such like this :
int idToGet = int.parse(tbCategory.SelectedValue.ToString());
Then done,
But when I bind the combobox with EF, it will show error Input string was not in a correct format. So the current value show { id = 7, category = TESTING } and not as usual.
Here a my code snippet :
public void loadCategory()
{
tbCategory.DataSource = null;
var listCategoryObj = new malsic_inventoryEntities();
var query = from cat in listCategoryObj.Category
//join cat in listItmObj.Category on n.id_category equals cat.id
select new { cat.id,cat.category };
if (query.Count() > 0)
{
tbCategory.DataSource = query.ToList();
tbCategory.DisplayMember = "category";
tbCategory.ValueMember = "id";
tbCategory.Invalidate();
}
else
{
tbSubCategory.Enabled = false;
}
}
public void loadSubcategory()
{
tbSubCategory.DataSource = null;
int id_category_current = int.Parse(tbCategory.SelectedItem.Value.ToString());
var listSubCategoryObj = new malsic_inventoryEntities();
var query = from SubCat in listSubCategoryObj.SubCategories
where SubCat.id_category == id_category_current
select new { SubCat.id, SubCat.subcategory };
if (query.Count() > 0)
{
groupBox1.Enabled = true;
tbSubCategory.DataSource = query.ToList();
tbSubCategory.DisplayMember = "subcategory";
tbSubCategory.ValueMember = "id";
tbSubCategory.Invalidate();
}
else
{
groupBox1.Enabled = false;
}
}
I do something wrong?
I don't think your problem is anything to with ADO.NET or Entity Framework. I think your problem is on the line with int.Parse. Try setting id_category_current this way instead of how you do it now:
int id_category_current;
if(!int.TryParse(tbCategory.SelectedItem.Value.ToString(), out id_category_current))
{
groupBox1.Enabled = false;
return;
}

LINQ to SQL does not support casting?

If I do this:
User userOOM = new User();
userOOM.Nickname = "markzzz";
UserFacebook userFacebook = new UserFacebook();
userFacebook.first_name = "marco";
db.User.InsertOnSubmit(userOOM);
userOOM.UserFacebook.Add(userFacebook);
db.SubmitChanges();
the insert is perfect, and it works. But, if I do this:
User userOOM = new User();
userOOM.Nickname = "markzzz";
UserFacebookCatcher userCatched = new UserFacebookCatcher();
UserFacebook userFacebook = (UserFacebook)userCatched;
userFacebook.first_name = "marco";
db.User.InsertOnSubmit(userOOM);
userOOM.UserFacebook.Add(userFacebook);
db.SubmitChanges();
(so, I cast the userFacebook class) I got a Object reference not set to an instance of an object. exception. It works if I remove:
userOOM.UserFacebook.Add(userFacebook);
so the casting is the problem. UserFacebookCatcher is nothing special:
public class UserFacebookCatcher : UserFacebook
{
public UserFacebookCatcher()
{
this.first_name = "marco"
}
}
LINQ to SQL does not work if I inherit a class?

Categories