Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Casting a ref variable..

Posted on 2003-11-05
2
Medium Priority
?
269 Views
Last Modified: 2010-04-16
Hi,

I wanted to cast a reference to another type..
I have a class Port which implements IPortInput ..

My current variable is : IPortInput anInputPort;

And I have a method as follows: public void Test(ref Port){}

So if I want to call this method by passing a reference of anInputPort (IPortInput) as a type Port instead of type IPortInput which requires casting how can I do that?

Since casting directly by using (Port) ref anInputPort or ref (Port) anInputPort doesnt work..

Can someone give me an example please. Thanks..
0
Comment
Question by:heboh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 15

Accepted Solution

by:
SRigney earned 200 total points
ID: 9692039
Why is your current variable
IPortInput anInputPort?

Don't you want to create a main variable of the specific type and the interface is only used by functions that require interface only functionality?

In any case doing something like this should work.
IPortInput anInputPort = new Port();

Port PortToSend = (Port)anInputPort;
Test(ref PortToSend);


If you want to do it within the function call you probably need another layer of parenthesis.  I didn't test this, so I'm not sure if it will work.

Test(ref ((Port) anInputPort));

Good Luck, let me know how it goes.
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9702866
Why are you passing it as a reference?? There is usually no sense to do this. If you need to change it then return the changed value.

public Port Test(IPortInput anInputPort)
{

}
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question