Passing a class by reference

G

Guest

I need to change the Url property in a web service proxy class in a generic way. The proxy class looks like this

public class Sender : System.Web.Services.Protocols.SoapHttpClientProtocol

...

If I instantiate the proxy class, I can set its Url property

localhost.Sender oSender = new localhost.Sender(); // proxy clas
oSender.Url = "http://server/webservice/s.asmx"

However, in order to set Url I need to look up a server name in appSettings and replace the name of server in the existing Url from 'localhost' to a name from appSettings
Therefore, I would like to make a generic method that would accept any web service proxy class and do all work

So, this method

public static void AdjustWebServiceProxyUrl(System.Web.Services.Protocols.WebClientProtocol proxyClass

allows me to call i

localhost.Sender oSender = new localhost.Sender(); // proxy clas
AdjustWebServiceProxyUrl (oSender); // works fin

However, AdjustWebServiceProxyUrl method should change the Url property and return it
In order to do that I need to pass it by reference and this is where I got stuck.

This code won't complie

public static void AdjustWebServiceProxyUrl(ref System.Web.Services.Protocols.WebClientProtocol proxyClass

localhost.Sender oSender = new localhost.Sender();
AdjustWebServiceProxyUrl (ref oSender);

Why does it compile without ref

Thanks,

-Sta
 
J

Jon Skeet [C# MVP]

Stan said:
However, AdjustWebServiceProxyUrl method should change the Url
property and return it.
In order to do that I need to pass it by reference and this is where
I got stuck.

No, in order to do that you *don't* need to pass by reference.

See http://www.pobox.com/~skeet/csharp/parameters.html

As for why it won't compile the way you're trying to (unnecessarily)
pass it by reference, see

http://blogs.msdn.com/csharpfaq/archive/2004/04/08/110241.aspx

(ignore the title of the entry - it really is about this).
 
D

Daniel Pratt

Hi Stan,

A couple things. First, the "ref" parameter modifier allows you to
create a method that can change the value of a variable that is passed to
the method. The value of a reference type variable is the object the
variable references. To put it another way, the "ref" parameter modifier
does not affect what happens when a method calls a method or sets a property
of an object passed via a parameter. Unless the AdjustWebServiceProxyUrl
method needs to point the oSender variable to another object entirely, the
proxyClass parameter does not need to be "ref".

Second, for "ref" parameters, the type of the variable passed must be
exactly the type of the parameter. Considering your example, it would be
legal for AdjustWebServiceProxyUrl to assign the proxyClass parameter to any
object that derives from the WebClientProtocol class. But the assignment is
*actually* happening to the oSender variable you passed in. If
AdjustWebServiceProxyUrl, for some reason, assigned proxyClass to something
other than an instance of class Sender, it would be an invalid assignment
for the oSender variable.

Clear as mud?

Regards,
Daniel

Stan said:
I need to change the Url property in a web service proxy class in a
generic way. The proxy class looks like this:
public class Sender : System.Web.Services.Protocols.SoapHttpClientProtocol {

...

If I instantiate the proxy class, I can set its Url property:

localhost.Sender oSender = new localhost.Sender(); // proxy class
oSender.Url = "http://server/webservice/s.asmx";

However, in order to set Url I need to look up a server name in
appSettings and replace the name of server in the existing Url from
'localhost' to a name from appSettings.
Therefore, I would like to make a generic method that would accept any web
service proxy class and do all work.
So, this method

public static void AdjustWebServiceProxyUrl(System.Web.Services.Protocols.WebClientProtocol
proxyClass)

allows me to call it

localhost.Sender oSender = new localhost.Sender(); // proxy class
AdjustWebServiceProxyUrl (oSender); // works fine

However, AdjustWebServiceProxyUrl method should change the Url property and return it.
In order to do that I need to pass it by reference and this is where I got stuck.

This code won't complie:

public static void AdjustWebServiceProxyUrl(ref
System.Web.Services.Protocols.WebClientProtocol proxyClass)
 

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