How to call the declare type methods avoiding dynamic binding? - dynamic-binding

Person[] p = new Person[5];
p[0] = new Student();
p[1] = new Student();
...
Arrays.sort(p);
Here Person is the parent class and Student is the child class. There is an overridden compareTo() method both in Person class and Student class. I want to sort Person array based on the method in Person class not in Student class. But JAVA's dynamic binding calling the method in Student class as Student is the actual type. How to solve it?

Related

making an instance of a class in Dart

i'm a beginner in Dart programming and i'm a bit confused when it comes to making an instance of a class,
suppose we have a class named Student
what is the difference between these two:
Student student;
and
Student student = new Student();
In Student student; you are just declaring a field that you can later store a Student object in. You aren't creating an actual object here.
At Student student = new Student(); you are creating an Student object which is stored in student. You now have an instance of the Student class that you can use methods on by calling for example student.study().
In dart (as of Dart 2), unlike Java, you can also omit the "new" keyword.
Which means you can write like this instead: Student student = Student();
Example of using both your provided rows would look like this;
Student student;
student = Student();
Which however can just be writted like this: Student student = Student();

how can i access get father name and store it to

class parent{
var fathername:String="sugumar"
}
class child:parent{
var car:String }
how can I get father name from the base class and store it to the property child in derived class child
Let's assume you have created an instance of the child class. Accessing the fathername variable of the parent class is just as same as accessing the car variable of the child class.
let childInstance = child()
print(childInstance.fathername)
print(childInstance.car)
You might wanna give a value to the car variable before creating the instance.

Linq projection: Get reference of new projected entity

I need to map EF entities to respective DTO. In the example below I have EF entities Parent and Child, and Child entity contains reference to Parent object. I also have ParentDto and ChildDto (DTO), and ChildDto contains reference to ParentDto (not Parent). So, how can I assign ParentDto reference to ChildDto instance in below method:
public Task<List<ParentDto>> Method()
{
return (Context.Set<Parent>()
.Where(someCondition)
.Select(p => new ParentDto
{
// here we map all properties from Parent to ParentDto
... ,
Children = p.Children.Select(c => new ChildDto
{
// here we map all properties from Child to ChildDto
... ,
Parent = ? // reference to newly created ParentDto instance
})
}).ToListAsync();
}
You have to use a variable but you can't do it in a lambda expression. You have to do the mapping in memory after calling ToListAsync():
public Task<List<ParentDto>> Method()
{
var parents = await (Context.Set<Parent>()
.Where(someCondition)
.ToListAsync());
return parents.Select(p =>
{
var parent = new ParentDto();
//map parent properties
parent.Children = p.Children.Select(c => new ChildrenDto
{
//map child properties
});
return parent;
}).ToList();
}
In regular LINQ (not to entities) this isn't possible because of an important feature of object initializers: atomic assignment. As you can read here, an object initialization like...
var c = new Customer() { Name = "Bart", City = "Redmond", Age = 24 };
...is equivalent to...
Customer __t = new Customer();
__t.Name = "Bart";
__t.City = "Redmond";
__t.Age = 24;
Customer c = __t;
So the object is created and fully initialized first and then its reference is exposed. Therefore, if inside the object another object is initialized, the nested object will never be able to grab a reference to its parent during the initialization phase. You can only assign the parent afterwards.
Although in LINQ-to-entities the mechanism of creating objects is entirely different, the initialization logic can be considered identical, and the same restrictions apply.
As you know, in LINQ-to-Entities we can't call instance methods of entities while we're in the query expression. Else you could, for instance, have called some method in Parent that constructs its children (and assigns itself to it as their parent). As it is now, the only thing you can do is construct the Parents with their nested parent.Children first and after that, traverse the parent.Children collections and assign their Parent to them (as in Ufuk's answer).

Entity Framework: Many-to-Many

I have the following scenario: A Doctor can have multiple Companions. A Companion can also have multiple Doctors. Here are my classses (minus the context):
Public Class Doctor
Public Property DoctorId As Integer
Public Property Regeration As Integer
Public Property Name As String
Public Property Actor As String
Public Property Companions As List(Of Companion)
End Class
Public Class Companion
Public Property CompanionId As Integer
Public Property Name As String
Public Property Actor As String
Public Property Doctors As List(Of Doctor)
End Class
Public Class DoctorViewModel
Public Property DoctorId As Integer
Public Property Actor As String
Public Property Companions As List(Of CompanionViewModel)
End Class
Public Class CompanionViewModel
Public Property Actor As String
End Class
I'm trying to fetch a singular Doctor with a list of companions who have travelled with him. This I can do quite simply, but I'm trying to shape the query to get only a few columns and not the entire entity. He is my bungled query - the X's are what I can't figure out.
Dim query = From d In context.Doctors
From c In context.Companions
Select New DoctorViewModel With {
.Actor = d.Actor,
.DoctorId = d.DoctorId,
.Companions = XXXXXXX}
EDIT: If I query:
(From d In Tardis.Doctors
Where d.Actor = "Tom Baker"
Select New DoctorViewModel With {.Actor = d.Actor, .Companions = d.Companions.Select(Function(c) New CompanionViewModel With {.Actor = c.Actor})}).SingleOrDefault
I get:
"Unable to cast the type 'System.Collections.Generic.IEnumerable1' to
type 'System.Collections.Generic.List1'. LINQ to Entities only
supports casting Entity Data Model primitive types."
Would it be considered nasty to ditch the ViewModel classes in the query and just get the stuff as an anonymous type, then pass this to a constuctor in a ViewModel (my models have a whack of functions that are needed) and fill the class like this?
It probably would be not only okay but much more readable (aka maintainable) as well. Especially since the query wouldn't return an anonymous type, it would return an IQueryable
Solved! I've spent days on this! Trick was to change the List inside the Doctor ViewModel to IEnumerable(Of CompanionViewModel)

Cast IEnumerable<T> to ObservableCollection<T> without constructor injection the IEnumerable<T>

How can I do that?
thats a no go:
ObservableCollection obsCol = new ObservableCollection(myIEnumerable);
scenario:
var query = from c in customers
select new Customer()
{
Products = from p in products
where p.Id = c.Id
select p
};
Products is a ObservableCollection so it can not take the IEnumerable result from the select above...
How to cast?
Like this:
ObservableCollection obsCol = new ObservableCollection(myIEnumerable.ToList());
Note that the ObservableCollection instance will not reflect changes to the customers list.
EDIT: The Select extension method returns an instance of a compiler-generated iterator class. Since this class does not inherit ObservableCollection, it is impossible to cast it to one.