Late binding in C# - How to pass an object and yet use hardcoded properties?  
Author Message
ForEverLearning





PostPosted: Visual C# General, Late binding in C# - How to pass an object and yet use hardcoded properties? Top

Hi,

I will use an example to describe my problem:

I have two objects - Say Student and Teacher.

The two objects share some properties - Say Name and Birthdate

I have one method that lists person name and birthdate. I need to pass an object and yet use the properties - Something like this:

private void ListPersonInfo(object person)

{

Console.WriteLine(person.Name, person.BirthDate)

}

The above does not compile since at compile time the Name and BirthDate are properties of their respecitve objects and are not properties of the Object class.

I don't want to use two methods one for teachers and one for students.

I also know that it is not a good OO practice to pass plain objects like this and I don't want to use reflection.

Can someone help

Thanks for your help in advance.



Visual C#13  
 
 
ahmedilyas





PostPosted: Visual C# General, Late binding in C# - How to pass an object and yet use hardcoded properties? Top

why dont you just change "object" to Person in the parameter of the method, or the type of object it is this would mean you can only pass in the person object type, which will have the Name and birthdate properties.

Example, if this "Persons" object/class has those properties then with the modification.....

private void ListPersonInfo(Person person)

{

Console.WriteLine(person.Name, person.BirthDate)

}

you could also do this, but not really recommended as if you pass in an incorrect object type, it will throw an exception.

 

private void ListPersonInfo(object person)

{

Console.WriteLine(((Person)person).Name, ((Person)person).BirthDate)

}



 
 
ForEverLearning





PostPosted: Visual C# General, Late binding in C# - How to pass an object and yet use hardcoded properties? Top

Hi,

Thanks for your reply.

person in the example is just a parameter. It could be either a Teacher object or a Student object - Person is not a stand-alone object (no inheritance of any kind - Person and Teacher are independent).

Your approach would work if Person is a separate object that has the properties Name and BirthDate defined but this is not the case.

Thanks anyway.


 
 
ahmedilyas





PostPosted: Visual C# General, Late binding in C# - How to pass an object and yet use hardcoded properties? Top

well you need to look into polymorphism/inheritance/base classes to pretty much achieve what you are asking.

http://www.codeproject.com/csharp/csharpintro01.asp

http://msdn2.microsoft.com/en-us/library/ms173152.aspx

http://msdn2.microsoft.com/en-us/library/9w2ctx1s(VS.71).aspx

http://www.devarticles.com/c/a/C-Sharp/Inheritance-and-Polymorphism/

you can treat one object as another (casting for example) if they have a common "ground" for example.

you could have "Person" as a base class for example, then have Student and Teacher derrive from it (implement the Person class) and create each class (Student and Teacher) to whatever you like for them to have in terms of characteristics or methods for a teacher that does a teaching task, and methods for a student what students would do (sleep and drink maybe..)



 
 
Mark Benningfield





PostPosted: Visual C# General, Late binding in C# - How to pass an object and yet use hardcoded properties? Top

Hello All.

ForEverLearning:

Well, you don't want much, do you What makes any programming language a language is the rule structure of that language.

The properties you speak of reside in their respective class types, but you want to be able to access them from an instance of the base class instance object. C# has ways defined to do this, such as inheritance and polymorphism, but you don't want to use them.

You can use reflection to expose the properties of an unknown type at runtime in C#, but you don't want to use this either.

You could use a different method signature for each class type to access the particular properties, but you don't want to do that either.

Since a re-write of the C# compiler is probably out of the question, if you make some specific, concrete assumptions about your data (not generally considered a wise thing to do), then the following code might be of use:

using System;

using System.Collections.Generic;

using System.Text;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

Teacher tchr = new Teacher();

tchr.Name = "My Teacher Name";

tchr.BirthDate = "02/14/64";

object person = (object)tchr;

ListPersonInfo(person);

Student stdt = new Student();

stdt.Name = "My Student Name";

stdt.BirthDate = "04/28/28";

person = (object)stdt;

ListPersonInfo(person);

Console.ReadLine();

}

static void ListPersonInfo(object person)

{

if (person.ToString().EndsWith("Teacher"))

{

Teacher pers = (Teacher)person;

Console.WriteLine(pers.Name);

Console.WriteLine(pers.BirthDate);

}

if (person.ToString().EndsWith("Student"))

{

Student sPers = (Student)person;

Console.WriteLine(sPers.Name);

Console.WriteLine(sPers.BirthDate);

}

}

}

class Teacher

{

public string Name;

public string BirthDate;

}

class Student

{

public string Name;

public string BirthDate;

}

}



 
 
ForEverLearning





PostPosted: Visual C# General, Late binding in C# - How to pass an object and yet use hardcoded properties? Top

Very helpful - Thanks all.
 
 
James Curran





PostPosted: Visual C# General, Late binding in C# - How to pass an object and yet use hardcoded properties? Top

person in the example is just a parameter. It could be either a Teacher object or a Student object - Person is not a stand-alone object (no inheritance of any kind - Person and Teacher are independent).

But they ARE related. You just haven't bother coding it that way.

However, it you still insist on ignoring the inherient IsA relation is your objects, there is a way to say that two class have the same properties: Interfaces.



interface IHasNameAndBirthDate
{
string Name {get;}
DateTime BirthDate {get;}
}
class Student : IHasNameAndBirthDate
{ ......
}
class Teacher : IHasNameAndBirthDate
{......
}
private void ListPersonInfo(IHasNameAndBirthDate person)
{
Console.WriteLine("{0} {1}", person.Name, person.BirthDate);
}



 
 
James Curran





PostPosted: Visual C# General, Late binding in C# - How to pass an object and yet use hardcoded properties? Top

if(person.ToString().EndsWith("Teacher"))

That does assume that the Student & Teacher classes don't override the ToString defined in System.Object (which is usually one of the first things I do). So, we'd probably be better off with:

if(person.GetType().FullName.EndsWith("Teacher"))

although is there any reason why your avoiding

if (person is Teacher)



 
 
Mark Benningfield





PostPosted: Visual C# General, Late binding in C# - How to pass an object and yet use hardcoded properties? Top

Hello All.

James:

Now, I'm not saying that I would use this code. The whole approach seems to me to be somewhat less than ideal. However, it does manage to work within the boundaries that ForEverLearning set forth, and again, there is the caveat about assumptions. Speaking personally, I would say that any questions concerning a better approach would have to begin at a much earlier point.

Striving to be Diplomatic.