Asked By
Gustav
240 points
N/A
Posted on - 05/16/2011
I am using W# ith Microsoft .Net Framework 3.5. I am writing a desktop application for calculating variances in a set of values. I have read about the new Microsoft .Net Framework technology called Lambda functions.
I tried this in the application where I had written a loop to find numbers that are fully devisable by nine. The application compiles correctly, but when I run the program I am getting a runtime exception. Is it because I am now using a lambda function in the program?
Answered By
Mathias
0 points
N/A
#98485
Getting a run-time error when using a Lambda function in C#
Lambda functions are a new feature introduced in Microsoft .Net Framework 3.0. It allows a programmer to define inline functions without actually having to type the function explicitly. Lambda functions are written in form of "expressions".
The left hand side of a Lambda function defines the input parameters and the output parameters if any.The right hand defines the function expression itself. An example of a lambda function using C# is given below:
Func<int,int> calculate = x => x * 10;
The Func type defines that the "calculate" function has one input parameter and one output parameter. Both of them are of type int.
When the program is compiled, the Microsoft .Net compiler generates the method implementation. The variable "x" is automatically  derived from the expression. The programmer does not have to define it.
Answered By
Edvard
0 points
N/A
#98486
Getting a run-time error when using a Lambda function in C#
Type inference technology, as mentioned by Mathias, is a ground breaking technology incorporated into Microsoft .Net Framework 3.0. It frees the programmer from defining the data type. The compiler does the needful by adding additional code automatically.
Microsoft .Net uses strongly typed data. Strongly typed data means that there are no "variant" data types. Even when you do not define the data type in code, the Microsoft .Net compiler actually adds the code to the compiled executable. Therefore, you cannot get a run-time error when you are using a lambda expression in your source code.
Most likely cause for the runtime error when executing the program would be a data comparison or a type mismatch when casting a variable to another variable.
Answered By
Gustav
240 points
N/A
#98487
Getting a run-time error when using a Lambda function in C#
Why am I getting a run-time error when running the program written in Microsoft C#?
I have the following code that is causing me immense grief!
   List<int> numbers = getNumberDataFromDB();
    var numberfilter = numbers.FindAll((int n) => { return n % 9 == 0; });
    textBox1.Text = numberfilter[0].ToString();
The above code is the one and only Lambda expression used in my source code.
I am retrieving the list of numbers from the database.
The database is a Microsoft SQL Server 2008 Developer Edition.
Answered By
Ingrid
0 points
N/A
#98488
Getting a run-time error when using a Lambda function in C#
The error you are experiencing may not be related to the Lambda expression at all. A run-time error in a Microsoft .Net desktop application could happen for various other reasons.
It is always better to examine the stack trace when the error happens. Microsoft .Net error screens are improved in comparison to its predecessors. The error dialog box shows the entire stack trace, with the option to continue or terminate. It is quite useful considering that a big crash is likely to cause a data loss in the application.
Having said that, Gustav, can you post the error screen on this thread please? This is so that we can have a closer look at the actual error that is causing the run-time crash of your desktop .net application.
Answered By
Gustav
240 points
N/A
#98489
Getting a run-time error when using a Lambda function in C#
I have attached the run-time error that I am experiencing. I have also expanded the details section in the Error dialog box and posted the stack trace here so you can have a closer look.
************** Exception Text **************
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
  at System.ThrowHelper.ThrowArgumentOutOfRangeException()
  at System.Collections.Generic.List`1.get_Item(Int32 index)
  at WindowsFormsApplication1.Form1.button1_Click(Object sender, EventArgs e) in c:WindowsFormsApplication1Form1.cs:line 24
  at System.Windows.Forms.Control.OnClick(EventArgs e)
  at System.Windows.Forms.Button.OnClick(EventArgs e)
  at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
  at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
  at System.Windows.Forms.Control.WndProc(Message& m)
  at System.Windows.Forms.ButtonBase.WndProc(Message& m)
  at System.Windows.Forms.Button.WndProc(Message& m)
  at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
  at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
  at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Answered By
Mathias
0 points
N/A
#98490
Getting a run-time error when using a Lambda function in C#
The exception class is named as Argument Out of Range exception. This means that you have supplied an argument , which is not valid for the operation.Â
You are actually experiencing an Array Index Out Of Bounds exception. This exception is raised when you attempt to access an array or list element by a named index which is not present. For example, if you have an array variable with ten values in it and you attempt to call the eleventh element, Microsoft .Net Framework will though and Array Index Out of Bounds exception.Â
I had a closer look at your code. If the database does not have any numbers that are divisible by nine, the Lambda expression will not return a list of values. When this happens, your variable number filter  actually does not have any elements at all. You are attempting to access the zero position without actually checking if the array contains any elements.
That is the reason for the error!
Answered By
Ingrid
0 points
N/A
#98491
Getting a run-time error when using a Lambda function in C#
Check the size of the variable array to see if it contains any elements. When you are expecting a list variable, be sure to check its length property to see if it contains any elements at all. It is always a good practice to check for the length or count of a list variable prior to accessing it.
A simple check such as the following will eliminate the run-time error:
if (numberFilter.Count > 0)
{
textBox1.Text = numberFilter[0].ToString();
}
Here we are checking if the numberFilter variable contains any elements. If the count is greater than zero, we attempt to access the first element. If the count is zero, we do not access any elements at all. You can always resort to a try catch block but it is not recommended as raising an exception is a resource-intensive process.
Answered By
Gustav
240 points
N/A
#98492
Getting a run-time error when using a Lambda function in C#
Your information was very useful and helped me understand Lambda functions and common programming pitfalls!
Microsoft .Net Framework 3.5 is truly magnificent! I have been looking at the wrong place all this time for the root cause of the run-time error! I will be more careful next time.
Thank you experts, Mathias, Edvard and Gustuv.
Thank you!