Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
Question: GetSalesData: this method must get 10 sales amount values from a user. A sales amount value is seen as valid, if it can be converted to a decimal C# data type. If the value is valid, it must be stored in a one dimensinal array, which is received as an out method parameter. A count must be kept of all invalid values. This count is then returned by the method. A for loop must be used for the conversion. Note: try/catch blocks are required.
 
Here is my code so far but I get an error which states: Error 1 The out parameter 'sales' must be assigned to before control leaves the current method C:\Users\Asus User\Desktop\w212079735\S1P1_SalesData\S1P1_SalesData\Program.cs 34 13 S1P1_SalesData
 
MY CODE:
 static void Main(string[] args)
        {
            decimal[] salesAmount = {};
            GetSalesData(out salesAmount);
        }
 
        public static int GetSalesData(out decimal []sales)
        {   
            int invalidEntries = 0;
            decimal[] amounts= new decimal[10];
 
            for (int counter = 0; counter < amounts.Length; counter++)
            {
                try
                {
                    Console.Write("Please Enter a Sales Amount: ");
                    amounts[counter] = Convert.ToDecimal(Console.ReadLine());
 
                }
                catch (FormatException)
                {
                    invalidEntries++;
                }
            }
            return invalidEntries;
 
        }
Posted 18-Feb-13 9:29am
Edited 18-Feb-13 11:02am
v3
Comments
richcb at 18-Feb-13 15:34pm
   
I don't think you can have "[]" before a variable. []sales is not correct C# syntax.
Tebogo C. Leshotho at 18-Feb-13 15:40pm
   
Thank you for your response, Well the thing is I want that method parameter to have values of an array that comes inside the GetSalesData method,hence I used the [ ] to indicate that the values are from only valid entries, hence on top there is an empty list that will store only valid values.
richcb at 18-Feb-13 15:42pm
   
In order to do that, you would simply do sales[] as you did for the decimal array. But Griff likely has the correct solution for you.
Tebogo C. Leshotho at 18-Feb-13 16:00pm
   
Thanx, Griff gave me a solution. And I kind of now understand the whole concept.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Change the name from "sales" to "amounts".
Then remove the "decimal[]" from the line:
            decimal[] amounts= new decimal[10];
You will end up with:
public static int GetSalesData(out decimal[] amounts)
{
    int invalidEntries = 0;
    amounts= new decimal[10];
 
    for (int counter = 0; counter < amounts.Length; counter++)
    {
        try
        {
            Console.Write("Please Enter a Sales Amount: ");
            amounts[counter] = Convert.ToDecimal(Console.ReadLine());
 
        }
        catch (FormatException)
        {
            invalidEntries++;
        }
    }
    return invalidEntries;
 
}
And no compilation error!
 
It's an out parameter - you must assign a value to it before you exit the method or it won't compile!
  Permalink  
Comments
Jibesh at 18-Feb-13 15:43pm
   
Right!!! +5
Tebogo C. Leshotho at 18-Feb-13 15:53pm
   
Thank you a lot, now it makes sense, I now understand that when the 10 values are entered they will be stored directly to amounts and then (out) to the Main. I dont know if you get what I mean.
OriginalGriff at 18-Feb-13 16:31pm
   
Yes, I understand, and you are exactly right!
Matt T Heffron at 18-Feb-13 16:09pm
   
You beat me to it.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You never assign a value to sales, or even reference it in the GetSalesData method.
 
You seem to be confusing an out parameter with a ref parameter with a "regular" parameter.
 
An out parameter passes the reference to the variable that is provided in the method call but ignores any value incoming and requires that the called method provide a value for the parameter before returning. This value will be accessable in the calling code via the passed variable. This is why you are getting the error.
 
A ref parameter also passes the reference to the variable that is provided in the method call but allows access to that value within the method, still allowing for the method to assign a new value to the passed variable, totally replacing the value held in the variable.
 
A "regular" parameter:
* for a value type (e.g., int, double, struct): the actual value of the argument is passed to the method;
* for a reference type (e.g., string, array, class): the reference to the argument object is passed to the method.
 
With reference types, the contents of the passed object may be manipulated and methods on the object may be called (according to the access permissions on the fields, properties, methods,... of the object).
 
Specifically, in your case, either:
* make the initial value in the declaration of salesAmount to be the correct size array,
remove the out from the declaration of GetSalesData and from the calling point,
add a check for sales == null (throw an ArgumentNullException),
add a check that sales.Length is large enough to hold the data,
remove the declaration of amounts,
and change the remaining usage of amounts to be sales.
OR
* change all usage of amounts to be sales,
and change the local declaration of amounts (now sales) to be a simple assignment, not a declaration.
I'd also remove the initialization of salesAmount as GetSalesData will always provide a value for it.
 
Further, I would not use the length of the array as the control of the loop. I'd probably have a named constant. (Unless this method will be called with different length arrays to allow for different amounts of data collected.)
  Permalink  
Comments
Tebogo C. Leshotho at 18-Feb-13 16:20pm
   
Well I hear your suggestion but at the moment I am only required to deal with Functions and Exception Handling on a book called Begining Visual C# 2010 (Chapter 6 & 7 only), and also the question is very strict that I must use out parameters. Thank you for your suggestion, I will refer back to it as we do advanced techniques.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 CPallini 375
1 Sergey Alexandrovich Kryukov 370
2 BillWoodruff 314
3 George Jonsson 274
4 OriginalGriff 257
0 OriginalGriff 5,030
1 CPallini 4,115
2 Sergey Alexandrovich Kryukov 3,554
3 George Jonsson 2,826
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100