473,397 Members | 1,969 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,397 software developers and data experts.

ref parameter for any object

hi
i want to have a method with a ref object parameter so that i can pass any
object by reference and write output in it like code below. the problem is
it does not work in c# like i coded it. i do NOT want to use overloads!!! is
it possible? if yes, how?
thanx jazper

--- CODE ---
public void foo(ref object o)
{
if( o is int) ((int)o) = 1;
else if( o is double) ((double)o) = 3.5;
else if( o is string) ((string)o) = "test";
else throw new Exception("type not recognized!");
}
public void callfoo()
{
int i = 0;
double d = 0.0;
string s = string.Empty;
foo(ref (object)i);
foo(ref (object)d);
foo(ref (object)s);
}
Nov 16 '05 #1
10 1267
Jazper,

There are a few things going on here. You should be getting compile
errors in the calls to foo, because you must match the signature (the cast
to object violates it).

Also, when you do this:

((int) o) = 1;

That is also a compile error. In order to make this work, you need to
do the following:

public void foo(ref object o)
{
if( o is int)
o = 1;
else if( o is double)
o = 3.5;
else if( o is string)
o = "test";
else
throw new Exception("type not recognized!");
}

public void callfoo()
{
int i = 0;
double d = 0.0;
string s = string.Empty;

// An object that has the assignment.
object o = null;

// Set o, then call.
o = i;
foo(ref o);

// Set o, then call.
o = d;
foo(ref o);

// Set o, then call.
o = s;
foo(ref o);
}

This is the only way it is going to work. Depending on what you are
actually doing in foo, Generics might help in .NET 2.0 (which is in beta
right now).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jazper Manto" <ej*****@hotmail.com> wrote in message
news:um**************@TK2MSFTNGP09.phx.gbl...
hi
i want to have a method with a ref object parameter so that i can pass any
object by reference and write output in it like code below. the problem is
it does not work in c# like i coded it. i do NOT want to use overloads!!!
is
it possible? if yes, how?
thanx jazper

--- CODE ---
public void foo(ref object o)
{
if( o is int) ((int)o) = 1;
else if( o is double) ((double)o) = 3.5;
else if( o is string) ((string)o) = "test";
else throw new Exception("type not recognized!");
}
public void callfoo()
{
int i = 0;
double d = 0.0;
string s = string.Empty;
foo(ref (object)i);
foo(ref (object)d);
foo(ref (object)s);
}

Nov 16 '05 #2
Try this....

public void foo(ref object o)
{
if( o is int) o = 1;
else if( o is double) o = 3.5;
else if( o is string) o = "test";
else throw new Exception("type not recognized!");
}
public void callfoo()
{
int i = 0;
double d = 0.0;
string s = string.Empty;
object o = i;
foo(ref o);
o = d;
foo(ref o);
o = s;
foo(ref o);
}

Willy.

"Jazper Manto" <ej*****@hotmail.com> wrote in message
news:um**************@TK2MSFTNGP09.phx.gbl...
hi
i want to have a method with a ref object parameter so that i can pass any
object by reference and write output in it like code below. the problem is
it does not work in c# like i coded it. i do NOT want to use overloads!!!
is
it possible? if yes, how?
thanx jazper

--- CODE ---
public void foo(ref object o)
{
if( o is int) ((int)o) = 1;
else if( o is double) ((double)o) = 3.5;
else if( o is string) ((string)o) = "test";
else throw new Exception("type not recognized!");
}
public void callfoo()
{
int i = 0;
double d = 0.0;
string s = string.Empty;
foo(ref (object)i);
foo(ref (object)d);
foo(ref (object)s);
}

Nov 16 '05 #3
Hi,

It is not easier to just return it?
public object foo( object o )

Anyway, your code has several things (complex and basics) involved that I
will try to explain.

Boxing and unboxing
--------------------------
when you convert a value type to a reference type ie : int to object you
create a new instance of object in the heap. this is better explained with
an example
double d = 0.0;
object o = d;
d=4;

now you have TWO double one in the heap referenced by "o" and another in the
stack "d" , therefore
d == (double)o is FALSE

now when you say foo(ref (object)i) you are CREATING a new object in the
heap (boxing) the int.

what happen is that with a ref parameter you cannot do this, a ref parameter
NEEDS to be a lvalue, meaning that it should be a expression valid in the
left part of an assignation like:
(object)i=4;
which is not valid ( you are getting the same problem in the if/else
construct you are using.

this is needed cause otherwise the reference used is lost at return , think
about it, you are creating a new instance and you are NOT keeping any
reference to it. !!!
I think that you will get several answer to this, probably one with a
better written techical english than me :)

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Jazper Manto" <ej*****@hotmail.com> wrote in message
news:um**************@TK2MSFTNGP09.phx.gbl...
hi
i want to have a method with a ref object parameter so that i can pass any
object by reference and write output in it like code below. the problem is
it does not work in c# like i coded it. i do NOT want to use overloads!!! is it possible? if yes, how?
thanx jazper

--- CODE ---
public void foo(ref object o)
{
if( o is int) ((int)o) = 1;
else if( o is double) ((double)o) = 3.5;
else if( o is string) ((string)o) = "test";
else throw new Exception("type not recognized!");
}
public void callfoo()
{
int i = 0;
double d = 0.0;
string s = string.Empty;
foo(ref (object)i);
foo(ref (object)d);
foo(ref (object)s);
}

Nov 16 '05 #4
hi nicholas

thanx for your answer.
with your code i have to cast in in a object and convert back because foo()
changed the object o and not the assigned type. is this not just a bit
circuitous?

public void callfoo()
{
int i = 0;
double d = 0.0;
string s = string.Empty;

// An object that has the assignment.
object o = null;

// Set o, then call.
o = i;
foo(ref o);
i = Convert.ToInt32(o);

// Set o, then call.
o = d;
foo(ref o);
d = Convert.ToDouble(o);

// Set o, then call.
o = s;
foo(ref o);
s = o.ToString();
}
Nov 16 '05 #5
hi Willy

thanx for your answer.
see my answer to nicholas, who suggested the same thing.
Nov 16 '05 #6
Jazper,

You are right, it is, but this is the only way, since you want to pass
it by reference. Imagine you were able to pass the string instance in for
ref object, like so:

// Declare an instance of string, "s".
string s = "hello there";

// Call foo.
foo(ref s);

Then, in foo, you do this:

// Set o to an integer.
o = 1;

This is bad, because you are trying to stuff an integer into a string.
This is why this rule is in effect for parameters passed by reference.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jazper Manto" <ej*****@hotmail.com> wrote in message
news:uF**************@TK2MSFTNGP15.phx.gbl...
hi nicholas

thanx for your answer.
with your code i have to cast in in a object and convert back because
foo()
changed the object o and not the assigned type. is this not just a bit
circuitous?

public void callfoo()
{
int i = 0;
double d = 0.0;
string s = string.Empty;

// An object that has the assignment.
object o = null;

// Set o, then call.
o = i;
foo(ref o);
i = Convert.ToInt32(o);

// Set o, then call.
o = d;
foo(ref o);
d = Convert.ToDouble(o);

// Set o, then call.
o = s;
foo(ref o);
s = o.ToString();
}

Nov 16 '05 #7
hy ignacio

thanx for your explanations.
It is not easier to just return it?
public object foo( object o ) yes, would be but my method will have a bool as return like
Double.TryParse().

(object)i=4;
which is not valid ( you are getting the same problem in the if/else
construct you are using.
this is needed cause otherwise the reference used is lost at return , think about it, you are creating a new instance and you are NOT keeping any
reference to it. !!!

that sounds logic but i thought that "foo(ref (object)i)" will create a
object in the heap refering to the object in the stack, which will be
assigned to the methods parameter "ref object o". i thought changing o will
change i now. when the method returns, the GC will collect the created
object in the heap. however obviously dotnet is not able to do what i meant.
never mind :-)

jazper
Nov 16 '05 #8
Hi,

thanx for your explanations.
It is not easier to just return it?
public object foo( object o )

yes, would be but my method will have a bool as return like
Double.TryParse().


No really, if you cast it already you don't have to.

if ( o is double)
return 3.5;
then of course you will have to cast it back to double: /* no tested */

d = (double) foo( (object) d);
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Nov 16 '05 #9
> This is bad, because you are trying to stuff an integer into a string.
This is why this rule is in effect for parameters passed by reference.


indeed it is. the programmer is responsible that the types must be correct
and no error is not going to happen...!
i mean the following code is possible too and also there is the programmer
responsible not to forget to instance the object. i mean it is allowed, it
will throw an exception but it is allowed...

object x = null;
x.ToString();
Nov 16 '05 #10
Hi,
object x = null;
x.ToString();

Of course, that synctactically and semantically is correct , and the
variable x is initialize ( to what is another thing !!! ) so it's a valid
construction.

The compiler can not keep track of how the variable is used, just think that
between those two lines can be a lot of lines where x can be used and
possible assigned ( even to null ) so it's not possible at compiler time to
know if x will be null in the second line.
cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Nov 16 '05 #11

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

Similar topics

8
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int,...
2
by: Siegfried Weiss | last post by:
Hi guys, i give up finding a solution by reading or by trial & error. Hope, YOU can help me! (Sorry for my rather long posting.) Stroustrup says, that templates could be declared with - type...
20
by: Brien King | last post by:
If I have a parameter that has an Object type (as opposed to something like a string), can I make that parameter a CONST? Right now, if you pass an object into a sub/function, that sub/function...
5
by: Ram | last post by:
Hi Friends I want to develope a custom control in .net which can be used with any project. I am writing a function in that class which I want to take any object as parameter. For that I have...
1
by: ahmed mahmoud | last post by:
I want to save a composite Object to mysql database. I mean if i have Object A which has attributes of primitive data types and array of Object B. So how can i save the array of B
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.