Unboxing to get Original Class

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Execuse me.

I do boxing, for example Employee class, as object:

public object ManipulateObject(object objInput)
{
...
Object obj;
obj = objInput;
...
//Manipulate obj here..
...

return obj;
}

Assume, then the client procedure/function exactly not know what the
original class is
and call this function by passing, may be Employee, Customer, or Supplier
class or whatever class.

The problem is, how to get an object as original class, so we can perform
unboxing and get back the original class?

Best regards.
Joufri
 
The problem is, how to get an object as original class, so we can
perform unboxing and get back the original class?

type-casting:

Employee e2 = (Employee)ManipulateObject(e1);

I have a vague feeling this is not what you ask about, but perhaps an
example of what you want to do would clarify it.

--
 
Boxing is not happenning in this case. Boxing only happens when you are
dealing with value types being converted to reference types. I would
recommend reading http://msdn.microsoft.com/msdnmag/issues/1200/dotnet/

To handle the case you are describing you would simply need to have an
explicit cast

Employee e = (Employee) objInput;

The problem is that objInput may not always be an Employee in which case you
would get an invalid cast exception.

An alternative is to use

Employee e = objInput as Employee;

Which will set e to null if objInput is not an employee.

Cheers,

Greg Young
MVP - C#
http://geekswithblogs.net/gyoung
 
Execuse me.

I do boxing, for example Employee class, as object:

That's not boxing. Boxing involves incorporating a value type (i.e.
struct) value within a reference type box. What you appear to have here
is just normal reference conversions.

Having said that, it's still unclear what you're trying to do. What
manipulations of objInput are you after? How do you want to be able to
call the method?

Also, are you using C# 2.0? Whether or not generics are available may
well affect the answer.

Jon
 
<[email protected]> a écrit dans le message de (e-mail address removed)...

| I do boxing, for example Employee class, as object:

| The problem is, how to get an object as original class, so we can perform
| unboxing and get back the original class?

What you describe is *not* boxing, it is a simple casting of Employee, which
is a reference type and derives from Object, to Object and back again.

Boxing only applies to value types and implies placing a value type into an
object reference.

e.g.

{
int i = 2;

object o = i; // this is boxing

int x = (int) o; // this is unboxing
}

| public object ManipulateObject(object objInput)
| {
| ...
| Object obj;
| obj = objInput;
| ...
| //Manipulate obj here..
| ...
|
| return obj;
| }

Why would you pass an object in, assign its address to another reference,
edit the same object and then return the second reference ???

All reference types are passed to methods by value unless you explicitly
state "ref" on the parameter. This means that the *address" of non-ref
objects cannot be changed, but their contents can.

e.g.

public void ManipulateObject(object obj)
{
//Manipulate obj here..
}

.... should achieve the same as your version.

| Assume, then the client procedure/function exactly not know what the
| original class is
| and call this function by passing, may be Employee, Customer, or Supplier
| class or whatever class.

Unless you have good reason for treating all types as object, it is better
to create overloaded methods, one for each type, so that they can be treated
as the individual types that they really are.

e.g.

public void ManipulateObject(Employee obj)
{
//Manipulate obj here..
}

public void ManipulateObject(Customer obj)
{
//Manipulate obj here..
}

public void ManipulateObject(Supplier obj)
{
//Manipulate obj here..
}

Joanna
 
object obj = new object();
object obj1 = new object();
Employee Emp = new Employee();
obj = ManipulateObject(obj1);
Emp = (Employee) obj; //unboxing

chanmm
 
"chanmm" <[email protected]> a écrit dans le message de (e-mail address removed)...

| object obj = new object();
| object obj1 = new object();
| Employee Emp = new Employee();
| obj = ManipulateObject(obj1);
| Emp = (Employee) obj; //unboxing

Sorry but, no, that last line is *not* unboxing, it is a simple cast. Boxing
and unboxing is only applicable to value types.

And why would you create a new instance of Employee into a variable and then
overwrite it without having done anything to it ??

Joanna
 
Execuse me.

I do boxing, for example Employee class, as object:

public object ManipulateObject(object objInput)
{
...
Object obj;
obj = objInput;
...
//Manipulate obj here..
...

return obj;
}

I am not 100% sure what you are asking. As everyone pointed out, the
above is not boxing.
Assume, then the client procedure/function exactly not know what the
original class is
and call this function by passing, may be Employee, Customer, or Supplier
class or whatever class.

Do you mean you have a function that accepts an object of an unknown
type and you want that function to be able to find out the type and do a
type-specific action with it?

Here is a pseudocode example of what I think you might be asking:


function ProcessBusinessOjbect(object o)
{
if (o.type == "Customer")
o.DoCustomerJob();
else if (o.type == "Company")
o.DoCompanyJob();
else if (o.type == "Government")
o.DoGovernentJob();
}
ProcessOjbect(new Customer());
ProcessOjbect(new Company());
ProcessOjbect(new Governement());


Is the above pseudocode what you are talking about?

If not, than as everyone pointed out, all you have to do is a cast.
Best regards.
Joufri

Jim
--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
You roll an 18 in Dex and see if you
don't end up with a girlfriend
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
JimD
Central FL, USA, Earth, Sol
 
<snip>

Here is a little better example that you can compile. I am not sure if
this is what you are looking for or not.

public class Customer
{
string account = "012";
}

public class Employee
{
string account = "345";
}

public class Supplier
{
string account = "678";
}

class Program
{
static void Main(string[] args)
{
ProcessBusinessObject(new Customer());
ProcessBusinessObject(new Employee());
ProcessBusinessObject(new Supplier());
}

static void ProcessBusinessObject(Object o)
{
if (o.GetType().Name.Equals("Customer"))
Console.WriteLine("We have a Customer object");

if (o.GetType().Name.Equals("Employee"))
Console.WriteLine("We have a Employee object");

if (o.GetType().Name.Equals("Supplier"))
Console.WriteLine("We have a Supplier object");
}
}


Jim
--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
You roll an 18 in Dex and see if you
don't end up with a girlfriend
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
JimD
Central FL, USA, Earth, Sol
 
Thank for all.
Your suggession inspire me to learn more, especially in generic topic.

FYI, I use NHibernate and want to get a universal function (just one
fucntion) to read any persistent data that map on business object. I think
this mechanism will reduce a lot of code.

Thank a lot.
Ahmad Jufri
 
Back
Top