Solved

How to pass ArrayList as ref?

Posted on 2002-06-15
4
549 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks. Which objects are pass-by-value and which are pass-by-ref (by default)?
0
 

Expert Comment

by:Aibras
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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!
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 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

11 Experts available now in Live!

Get 1:1 Help Now