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

cast to ref object

Hi,

how can i cast to ref object, in C#:

internal void Foo( ref object o ) {
// . . .
}

// . . .

MyClass1 mc1 = new MyClass1();
MyClass2 mc2 = new MyClass2();

Foo( ( ??? )mc1 );
Foo( ( ??? )mc2 );
thx

Kimmo Laine
Nov 16 '05 #1
4 6098
Hi Kimmo,

Why do you want to cast it to object?

Foo(ref mc1);

should work just fine as it derives from object

Foo(ref (object)mc1);

should work too, but is unecessary.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Kimmo Laine <reply.to@newsgroup> wrote:
how can i cast to ref object, in C#:

internal void Foo( ref object o ) {
// . . .
}

// . . .

MyClass1 mc1 = new MyClass1();
MyClass2 mc2 = new MyClass2();

Foo( ( ??? )mc1 );
Foo( ( ??? )mc2 );


You can't. Imagine if the implementation of Foo did:

void Foo (ref object o)
{
o = new object();
}

What would you expect to happen then?

Pass-by-reference parameters must match type exactly. The workaround is
to do:

object o = mc1;
Foo (ref o);
mc1 = (MyClass1) o;

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Morten Wennevik <Mo************@hotmail.com> wrote:
Why do you want to cast it to object?

Foo(ref mc1);

should work just fine as it derives from object

Foo(ref (object)mc1);

should work too, but is unecessary.


No, neither work.

From the spec:

<quote>
When a formal parameter is a reference parameter, the corresponding
argument in a method invocation must consist of the keyword ref
followed by a variable-reference (§12.3.3) of the same type as the
formal parameter
</quote>

Compare that with:

<quote>
When a formal parameter is a value parameter, the corresponding
argument in a method invocation must be an expression of a type that is
implicitly convertible (§13.1) to the formal parameter type.
</quote>

--
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
Ah, [seriously embarrassed] I knew I should have tested it before posting :(

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #5

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";...
2
by: Dave Leach | last post by:
I am writing a Windows MDI application in C#. I would like to cast an object to a specific type. I know that the object is of a compatible type. The code below shows what I would like to do, but...
5
by: miki | last post by:
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...
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)...
17
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
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...
3
by: John Howard | last post by:
Making the following call to a local MSAccess database works fine: Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) Dim intRows As Integer Dim strSQL As String Dim ds As New...
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.. ...
11
by: Dinsdale | last post by:
I am trying to determine what the current cast of an object is. Essentially, if I use GetType, it always returns the original type of the object created but I want to know what the current cast of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.