Solved

How to pass ArrayList as ref?

Posted on 2002-06-15
4
561 Views
Last Modified: 2008-02-26
hi-

i have a synched ArrayList

ArrayList synchedResultList = ArrayList.Synchronized(resultList);

i pass it to a method like so

myMethod(ref synchedResultList);

and my method is declared like

void myMethod(ref ArrayList list)...

it appears that only a copy of the list
is being passed back.

thanks - bg
0
Comment
Question by:BeerGod
[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
4 Comments
 
LVL 23

Expert Comment

by:naveenkohli
ID: 7080709
What did you expect to be returned? What exactly are you trying to do in myMethod? When you will pass a argument by "ref" that means memory will be allocated by client. And if you will pass in "null" argument value, then method can allocated memory for your array.
If you could give more detaila bout the problem, probabl somebody can point out the problem.

Naveen
0
 

Accepted Solution

by:
Aibras earned 50 total points
ID: 7084387
class Class1
     {
          // ArrayList is "ref" Type By Default Because it is Class
          // and all Classes is "ref" type
          // try this code

          static void Main(string[] args)
          {
               ArrayList arr = new ArrayList();

               refArrayList(arr); // Passing by value arr is "ref" by default

               Console.WriteLine(arr[0]);


          }
          private static void refArrayList(ArrayList A)
          {
               A.Add(15);
          }
0
 

Author Comment

by:BeerGod
ID: 7084657
Thanks. Which objects are pass-by-value and which are pass-by-ref (by default)?
0
 

Expert Comment

by:Aibras
ID: 7089642
Any primitive data types can be passed by
value
ex:
int i=0;
myMethod(i);
float x=1.22F;
myMethod2(x);
if you want to pass it as a "ref" type
you can prefix it with "ref" keyword
ex:
int i=10;
myMethod3(ref i)
.
.
.
static void myMethod3(ref arg)
{...}

you must know that int,float,long .... are struct types
that mean you can pass it as value because struct types store in stack
or as a ref type by using ref keyword
but
object,string,ArrayLlist,Stack,Queue.... are
class type that mean you can pass them as a value
and change any thing in them as a classes "ref by default
" because class types store in heap
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Calculate number of nights between two dates 5 91
How to read text with RegEx... 2 34
pressing download button in c# 7 36
Selenium and Xpath 4 31
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 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