473,386 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

cast a string to an object

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

Nov 16 '05 #1
5 2328
>
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/sahi.../02/40506.aspx
And if you'd rather see a textual comparison -
http://dotnetjunkies.com/WebLog/sahi.../02/40586.aspx

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

"miki" <mh****@hotmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com... 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

Nov 16 '05 #2
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?

Sahil Malik wrote:

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/sahi.../02/40506.aspx And if you'd rather see a textual comparison -
http://dotnetjunkies.com/WebLog/sahi.../02/40586.aspx
- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik

"miki" <mh****@hotmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
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


Nov 16 '05 #3
miki <mh****@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
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

"miki" <mh****@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com... 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?

Sahil Malik wrote:
>
> 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/sahi.../02/40506.aspx
And if you'd rather see a textual comparison -

http://dotnetjunkies.com/WebLog/sahi.../02/40586.aspx

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

"miki" <mh****@hotmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
> 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
>

Nov 16 '05 #5
yeah, you're right. GetType will give back the base object. Thanks

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Richard Lee | last post by:
Hi, I have a question when I do a data type cast. the common way when we do a cast, is we know the type we want to cast to, i.e. we want to cast object to string, object xyz = "question";...
6
by: John Wood | last post by:
If you override ToString(), why can't the default implementation of the string cast use that implementation for the object? It's a question people ask me time and time again.
8
by: GlennDoten | last post by:
I just happened to be looking through the implementation of the System.Version class in the SSCLI and one of the constructors starts like this: public Version(String version) { if ((Object)...
3
by: mra | last post by:
I want to cast an object that I have created from a typename to the corresponding type. Can anycone tell me how to do this? Example: //Here, Create the object of type "MyClass" object...
5
by: Alan Silver | last post by:
Hello, I have an ASP.NET page where I am grabbing an SqlDataReader and using it to populate some controls on the form. Amongst the fields pulled out of the table are two integer fields, which I...
3
by: Mike Cooper | last post by:
Hello All! I am getting teh above error message on the following code: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim dgt As...
5
by: Jimp | last post by:
Why can't I cast List<MyObject> to ICollection<IMyObject>. MyObject implements IMyObject, and of course, List implements ICollection. Thanks
9
by: Jim in Arizona | last post by:
I get this error: Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlInputText' to type 'System.Web.UI.WebControls.TextBox'. Using this code: Dim test3 As TextBox test3 =...
5
by: Eric bouxirot | last post by:
hi, i'd like to do a "cast", who seem to be very complicated in .NET. in C standard it's so easy... i explain : i have made an app based on plugin architecture in VB.NET. all work fine.. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.