473,406 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

simple out parameter as ArrayList

Why is it that I get these errors on this function

1.) The out parameter 'Avalues' must be assigned to before control leaves
the current method
2.) Use of unassigned local variable 'Avalues'

public bool getStringFieldAsArrayList( out ArrayList Avalues)
{
bool retVal=false ;
Avalues.Add("fooAvalues");
return retVal;
}
BUT NOT for this:
public void getOutString( out string test)
{
test ="foo";
}
They seem to be constructed the same way...
-
Andrew
Nov 17 '05 #1
3 7738
andrewcw <an******@acw.com> wrote:
Why is it that I get these errors on this function

1.) The out parameter 'Avalues' must be assigned to before control leaves
the current method
2.) Use of unassigned local variable 'Avalues'

public bool getStringFieldAsArrayList( out ArrayList Avalues)
{
bool retVal=false ;
Avalues.Add("fooAvalues");
return retVal;
}

BUT NOT for this:
public void getOutString( out string test)
{
test ="foo";
}
They seem to be constructed the same way...


No they're not. An out parameter *must* be assigned to before the end
of the method (unless you're throwing an exception) and is treated as
unassigned until you've assigned to it.

For the second point, think of it like a local variable - it's like
you're trying to do:

ArrayList Avalues;
Avalues.Add("fooAvalues"); // This wouldn't work

and in the second case

string test;
test = "foo";

In the second case, you're assigning to the variable. In the first case
you're trying to use the value as if it's already assigned.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
because you don't need the 'out' modifier at all. If all you are trying to
do is add an element to an array, then you are NOT modifying the value of
the Avalues variable. You are modifying the list that the variable points
to.

Remove the 'out' keyword and your method will not only compile, it will
work.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"andrewcw" <an******@acw.com> wrote in message
news:29**********************************@microsof t.com...
Why is it that I get these errors on this function

1.) The out parameter 'Avalues' must be assigned to before control leaves
the current method
2.) Use of unassigned local variable 'Avalues'

public bool getStringFieldAsArrayList( out ArrayList Avalues)
{
bool retVal=false ;
Avalues.Add("fooAvalues");
return retVal;
}
BUT NOT for this:
public void getOutString( out string test)
{
test ="foo";
}
They seem to be constructed the same way...
-
Andrew

Nov 17 '05 #3
Thank you Jon and Nick. I understand now I dont need the out. I understand
also now that the 2 functions are different.

I did not understand what it meant to assign in the ArrayList case. I had
wondered if it was because an ArrayList is an object , but I simply did not
understand what assign means as in terms of an ArrayList
.. Now I figured out with your suggestions -> .
Avalues= new ArrayList(); //inside the functions makes it compile

Thanks !!
--
Andrew
"andrewcw" wrote:
Why is it that I get these errors on this function

1.) The out parameter 'Avalues' must be assigned to before control leaves
the current method
2.) Use of unassigned local variable 'Avalues'

public bool getStringFieldAsArrayList( out ArrayList Avalues)
{
bool retVal=false ;
Avalues.Add("fooAvalues");
return retVal;
}
BUT NOT for this:
public void getOutString( out string test)
{
test ="foo";
}
They seem to be constructed the same way...
-
Andrew

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Ryan Taylor | last post by:
Hello. I am trying to overload a method so that I have four possible working copies. The only difference between the four methods is what to search by and in what manner to return the results....
5
by: Pete Hearn | last post by:
Hello All, New to the whole C#/Webservice/ADO.NET thing, so apologies in advance if this is a daft question! I have a webservice which returns a dataset - no problem there and all very...
6
by: Vern | last post by:
I'd like to make the following a generic method that all my forms can call to validate all the fields on the form. So how do I pass the form object that is represented as "this" in the following...
1
by: sd | last post by:
QUESTION: How can my ASP page - which uses language="VBScript" - pass a "System.Collections.ArrayList" object - as a parameter - to a C# method in the middle-tier ??? ----- Is it possible at...
1
by: tony | last post by:
Hello! I just wonder if you out there have any your suggestion about this matter. I have a class in this class I have an ArrayList. In one instance method in this class I fill this ArrayList...
8
by: =?Utf-8?B?UmF2aQ==?= | last post by:
Hi, I'm trying to pass values of different data-types to a web-service. I thought it would be easier to box these values and pass them as a System.object parameter, like public void...
1
by: dev24 | last post by:
Hi all, I am writing a simple method which when entered with an Int parameter performs an sql query, creates a result set, uses that resultset to get values from the object created by another...
6
by: dev24 | last post by:
Hi all, I am writing a simple method which when entered with an Int parameter performs an sql query, creates a result set, uses that resultset to get values from the object created by another...
2
by: Picqua | last post by:
I'm having trouble with a class that accepts an ArrayList as a parameter. The code looks something like this: ArrayList arrayOne = new ArrayList(); arrayOne.Add("Hello ");...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.