Solved

How to pass ArrayList as ref?

Posted on 2002-06-15
4
552 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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!
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now