C# (passing by reference) 
Author Message
 C# (passing by reference)

Dears,
i have the following Function in VB
Private Sub GenerateSoapException(ByRef SoapExcept As SoapException, ByVal
ErrMsg As String)

in one other method i call it simply as the following code.

Dim SoapEx As SoapException

try

    .....

Catch ex As Exception

GenerateSoapException(SoapEx, ex.Message)

Throw SoapEx

.....

NOW THAT WAS WORKING WITH VB,
EVEN I DID NOT INSTANTIATE OBJECT SoapEx.

now in C#
private void GenerateSoapExcption(ref SoapException SoapExcept,string
ErrMsg)

try{...}

catch(Exception e){

GenerateSoapExcption(ref SoapEx,e.Message);

throw SoapEx;

Quote:
}

NOW IN THIS C# CODE IT REQUEIRS ME TO INSTANTIATE(Create an instance) of the
OBJECT SoapEX

before sending it to GenerateSoapException Method.

WHAT IS HAPPING HERE?

CAN ANY ONE HELP.



Sun, 21 Nov 2004 15:23:33 GMT  
 C# (passing by reference)
Suhail,

    You could always set the reference to null before passing it.  This is
really what VB does (I assume, it tries to make everything easier).  If the
assignment isn't made, then it would default to null.  C# does not do this.

    Hope this helps.

--
               - Nicholas Paldino [.NET MVP]


Quote:
> Dears,
> i have the following Function in VB
> Private Sub GenerateSoapException(ByRef SoapExcept As SoapException, ByVal
> ErrMsg As String)

> in one other method i call it simply as the following code.

> Dim SoapEx As SoapException

> try

>     .....

> Catch ex As Exception

> GenerateSoapException(SoapEx, ex.Message)

> Throw SoapEx

> .....

> NOW THAT WAS WORKING WITH VB,
> EVEN I DID NOT INSTANTIATE OBJECT SoapEx.

> now in C#
> private void GenerateSoapExcption(ref SoapException SoapExcept,string
> ErrMsg)

> try{...}

> catch(Exception e){

> GenerateSoapExcption(ref SoapEx,e.Message);

> throw SoapEx;

> }

> NOW IN THIS C# CODE IT REQUEIRS ME TO INSTANTIATE(Create an instance) of
the
> OBJECT SoapEX

> before sending it to GenerateSoapException Method.

> WHAT IS HAPPING HERE?

> CAN ANY ONE HELP.



Sun, 21 Nov 2004 21:16:25 GMT  
 C# (passing by reference)
don't use the ref keyword!  Use the 'out' keyword instead.

When writing a method, you use ref when you want to manipulate something and
know it was initialized before the method was called.  If you are going to
ignore the value of the object that was passed in, you declare the arguement
as 'out'.

Whatever way the method is declared is the way it must be called.

You may get unpredictable results if you call a method expecting a 'ref'
parameter with a variable that has a null value!

I recommend looking at the help files for the 'ref' and 'out' keywords.

Mike


Quote:
> Dears,
> i have the following Function in VB
> Private Sub GenerateSoapException(ByRef SoapExcept As SoapException, ByVal
> ErrMsg As String)

> in one other method i call it simply as the following code.

> Dim SoapEx As SoapException

> try

>     .....

> Catch ex As Exception

> GenerateSoapException(SoapEx, ex.Message)

> Throw SoapEx

> .....

> NOW THAT WAS WORKING WITH VB,
> EVEN I DID NOT INSTANTIATE OBJECT SoapEx.

> now in C#
> private void GenerateSoapExcption(ref SoapException SoapExcept,string
> ErrMsg)

> try{...}

> catch(Exception e){

> GenerateSoapExcption(ref SoapEx,e.Message);

> throw SoapEx;

> }

> NOW IN THIS C# CODE IT REQUEIRS ME TO INSTANTIATE(Create an instance) of
the
> OBJECT SoapEX

> before sending it to GenerateSoapException Method.

> WHAT IS HAPPING HERE?

> CAN ANY ONE HELP.



Mon, 22 Nov 2004 04:53:46 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Passing values by reference from C# to a C++ managed application

2. C# this passed by reference

3. Passing values by reference from C# to a C++ managed application

4. Passing by Reference Or Passing by Pointer

5. C++ References and C# references Question?

6. Passing Form by Page Reference

7. Passing a reference to an object

8. Passing string across socket object reference

9. Pass an array as reference

10. Another question: If objects are passed by reference...

11. Passing Arguments by Reference

12. Pass by Reference duplicating memory

 

 
Powered by phpBB® Forum Software