Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to pass ArrayList as ref?

Posted on 2002-06-15
4
Medium Priority
?
574 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 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

618 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