passing objects using ref

R

Robert Lario

For examples sake I have made up a very simple example.

I have an object called foo1 which is of type foo. I want to be able to call
a funtion called myfunc as follows:

myfunc(ref foo1)

here's the function :

public void myfunc(ref object foo)
{
do some stuff
}

I get the following errors:
The best overloaded method match for myfunc(ref object)' has some invalid
arguments
Argument '1' cannot convert from 'ref foo' to 'ref object'
 
R

Robert Lario

Thanks for the replay -- could you be more specific? I tired what I thought
you meant -- this creates a syntax error..

robert
 
N

Nicholas Paldino [.NET/C# MVP]

Robert,

If you are passing a parameter by reference, then you have to make sure
that the type of the variable passed in is the same as the type of the
parameter. That is why you get that error. So, if your type is of type
foo, and you are trying to pass it into a method that needs a "ref object"
it will fail, unless you create a variable of type object first and pass
that.

Hope this helps.
 
R

Robert Lario

Yes. That is what I feared. I was hoping that since foo is of type object
that ploymorphism would take care of the up cast.


Nicholas Paldino said:
Robert,

If you are passing a parameter by reference, then you have to make sure
that the type of the variable passed in is the same as the type of the
parameter. That is why you get that error. So, if your type is of type
foo, and you are trying to pass it into a method that needs a "ref object"
it will fail, unless you create a variable of type object first and pass
that.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Robert Lario said:
Thanks for the replay -- could you be more specific? I tired what I thought
you meant -- this creates a syntax error..

robert
 
N

Nicholas Paldino [.NET/C# MVP]

Robert,

That would be an error if it allowed it. Because of the "ref" keyword,
that means that the method can modify the value of the parameter passed in.
If it allowed you to pass in an instance of type foo, and the method took a
parameter of type object, what if the method assigned a type other than foo
to the parameter? Upon return, the variable passed to the parameter
couldn't be accepted or accessed, since it points to a different type (and
incorrect, on top of that).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Robert Lario said:
Yes. That is what I feared. I was hoping that since foo is of type object
that ploymorphism would take care of the up cast.


in message news:[email protected]...
Robert,

If you are passing a parameter by reference, then you have to make sure
that the type of the variable passed in is the same as the type of the
parameter. That is why you get that error. So, if your type is of type
foo, and you are trying to pass it into a method that needs a "ref object"
it will fail, unless you create a variable of type object first and pass
that.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Robert Lario said:
Thanks for the replay -- could you be more specific? I tired what I thought
you meant -- this creates a syntax error..

robert
Take out the object reference in the function signature.
For examples sake I have made up a very simple example.

I have an object called foo1 which is of type foo. I want to be
able
to
call
a funtion called myfunc as follows:

myfunc(ref foo1)

here's the function :

public void myfunc(ref object foo)
{
do some stuff
}

I get the following errors:
The best overloaded method match for myfunc(ref object)' has some
invalid
arguments
Argument '1' cannot convert from 'ref foo' to 'ref object'
 
R

Robert Lario

Sure that could happen - And that is kind of what I want to do.

Using reflection I examine the class structure of the object passed in.
Then I build a new class in memory that extends the class type of the object
passed in. Then I return an instance of the new extended class wrapped
around the old object. So for example, if foo is passed and is of type Foo,
I build a new class called __Foo that Extends Foo and return an instance of
it.

I was hoping that I could call my methods as such:

myFunc(ref foo);

but know I have to do this:

foo = (Foo) myFunc(foo);

I just want to hide the casting and assignment.

Ideally I could just:

myFunc(foo);

But this does not appear to be supported, like in C++ with the &

any suggestions would be greatly appreciated.

Robert


Nicholas Paldino said:
Robert,

That would be an error if it allowed it. Because of the "ref" keyword,
that means that the method can modify the value of the parameter passed in.
If it allowed you to pass in an instance of type foo, and the method took a
parameter of type object, what if the method assigned a type other than foo
to the parameter? Upon return, the variable passed to the parameter
couldn't be accepted or accessed, since it points to a different type (and
incorrect, on top of that).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Robert Lario said:
Yes. That is what I feared. I was hoping that since foo is of type object
that ploymorphism would take care of the up cast.


in message news:[email protected]...
Robert,

If you are passing a parameter by reference, then you have to make sure
that the type of the variable passed in is the same as the type of the
parameter. That is why you get that error. So, if your type is of type
foo, and you are trying to pass it into a method that needs a "ref object"
it will fail, unless you create a variable of type object first and pass
that.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Thanks for the replay -- could you be more specific? I tired what I
thought
you meant -- this creates a syntax error..

robert
Take out the object reference in the function signature.
For examples sake I have made up a very simple example.

I have an object called foo1 which is of type foo. I want to be able
to
call
a funtion called myfunc as follows:

myfunc(ref foo1)

here's the function :

public void myfunc(ref object foo)
{
do some stuff
}

I get the following errors:
The best overloaded method match for myfunc(ref object)' has some
invalid
arguments
Argument '1' cannot convert from 'ref foo' to 'ref object'
 
J

Jon Skeet

Robert Lario said:
Sure that could happen - And that is kind of what I want to do.

Using reflection I examine the class structure of the object passed in.
Then I build a new class in memory that extends the class type of the object
passed in.

But that's *not* the same as Nick was suggesting. Nick was suggesting
the equivalent of:

string x = "hello";
Foo (ref x);


void Foo (ref object y)
{
y = Encoding.ASCII;
}

then x would no longer hold a reference to a string, it holds a
reference to an Encoding instance (well, an ASCIIEncoding instance to
be precise). Bang - type safety is broken. So, what you wanted to do
wouldn't break type safety (because you'd still be assigning a
compatible reference) but the only way to express it is in a way which
would allow type safety to be broken in other situations - so the
compiler disallows it.
Then I return an instance of the new extended class wrapped
around the old object. So for example, if foo is passed and is of type Foo,
I build a new class called __Foo that Extends Foo and return an instance of
it.

I was hoping that I could call my methods as such:

myFunc(ref foo);

but know I have to do this:

foo = (Foo) myFunc(foo);

I just want to hide the casting and assignment.

You can't, for precisely the reason above. If the method doesn't know
in advance what kind of type it's going to get, it can't assure the
compiler that it will give something compatible back. I'm not sure
whether or not generics would let you do what you want - maybe...
Ideally I could just:

myFunc(foo);

But this does not appear to be supported, like in C++ with the &

any suggestions would be greatly appreciated.

Stick with the assignment and cast.
 

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