cast a string to an object

M

miki

Hi all,

How can I cast from a string to an object? For example, suppose I have
classes as employee, manager, supervisor, director,...I have a user
interface that takes a person name, then performs query database and
returns a string which tells that person is either employee,
manager,...If it's student, a student object will be return. I can use
switch case to do that, but I think .net probaly has a simpler way to
solve that. Thanks in advance
 
S

Sahil Malik

How can I cast from a string to an object?

object o = "like this" ;
For example, suppose I have
classes as employee, manager, supervisor, director,...I have a user
interface that takes a person name, then performs query database and
returns a string which tells that person is either employee,
manager,...If it's student, a student object will be return. I can use
switch case to do that, but I think .net probaly has a simpler way to
solve that. Thanks in advance

But I think you might be trying to acheive something different than a simple
cast (maybe). The query result tells you if the person is an employee,
manager, supervisor, director.
Maybe you need to have a person base class, and employee, manager,
supervisor, directory classes as inherited classes. You could acheive the
same by specifying an IPerson interface too.
And then you could of course use a switch case statement to differentiate
what kind of object to create, but you could return a Person or IPerson.

If you'd rather see it in code -
http://dotnetjunkies.com/WebLog/sahilmalik/archive/2005/01/02/40506.aspx
And if you'd rather see a textual comparison -
http://dotnetjunkies.com/WebLog/sahilmalik/archive/2005/01/02/40586.aspx

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
 
M

miki

Thanks, Interface would be nice to use

for my solution, I think about creating a Member base class, and
employee, student, professor,... are inheritted from that class

string membertype = GetMemberType(personname);
//GetMemberType query database based on person name and return a
string
Member member = new Member(personname);
Type type = Type.GetType(membertype)
member = (type) member;

haven't run it yet. What do you think?
 
J

Jon Skeet [C# MVP]

miki said:
How can I cast from a string to an object? For example, suppose I have
classes as employee, manager, supervisor, director,...I have a user
interface that takes a person name, then performs query database and
returns a string which tells that person is either employee,
manager,...If it's student, a student object will be return. I can use
switch case to do that, but I think .net probaly has a simpler way to
solve that. Thanks in advance

It's not entirely clear what you mean - what actually builds the
employee, manager etc object itself? A string *isn't* an employee
object, even if it has information which can be used to build one.
 
S

Sahil Malik

Okay so in your suggested code below - member is the equivalent of person as
I was talking?

Check this out --

If employee: person, and
manager : person and
directory : person

Then you could write code like this --

person p = GetManager(...) ;
or
Person p = Getemployee(...) ;

Also this ---
Type type = Type.GetType(membertype)
member = (type) member;

Would then be unecessary.

If you had code like this --

person p = GetManager(...) ;
p.GetType() <-- gives you back MANAGER not PERSON.
and ...
((Manager)p).ManagerSpecificMethod() <-- this will work, but involves an
explicit cast.

Check up "Implicit cast" and "Explicit cast" - you'll know what I am
referring to :)


- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top