This code should iterate through an array of Packages in the objList ArrayList and print each package’s number in the lblDisplay Label. Assume objList has already been created and has had packages added to it. Find the error(s) in the following code.
foreach ( ArrayList objValue in objList )
{
lblDisplay.Text += ( " " + objValue.PackageNumber );
}
The reference that specifies the element is of the wrong type to iterate through this
list of Packages. The element reference should be of type Package. The complete incorrect code reads:
// PackageNumber.cs (Incorrect)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace PackageNumber
{
///
/// Summary description for FrmPackageNumber.
///
public class FrmPackageNumber : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblDisplay;
private System.Windows.Forms.Label lblPackageNumbers;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
public FrmPackageNumber()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent
// call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
// Windows Form Designer generated code
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run( new FrmPackageNumber() );
}
// prints each package number in an ArrayList
private void FrmPackageNumber_Load(
object sender, System.EventArgs e )
{
ArrayList objList = new ArrayList();
objList.Add( new Package( 3 ) );
objList.Add( new Package( 15 ) );
objList.Add( new Package( 89 ) );
objList.Add( new Package( 101 ) );
objList.Add( new Package( 150 ) );
objList.Add( new Package( 176 ) );
objList.Add( new Package( 215 ) );
objList.Add( new Package( 226 ) );
foreach ( ArrayList objValue in objList )
{
lblDisplay.Text += ( " " + objValue.PackageNumber );
}
} // end method FrmPackageNumber_Load
} // end class FrmPackageNumber
}
The complete corrected code should read:
// PackageNumber.cs (Correct)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace PackageNumber
{
///
/// Summary description for FrmPackageNumber.
///
public class FrmPackageNumber : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblDisplay;
private System.Windows.Forms.Label lblPackageNumbers;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
public FrmPackageNumber()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent
// call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
// Windows Form Designer generated code
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run( new FrmPackageNumber() );
}
// prints each package number in an ArrayList
private void FrmPackageNumber_Load(
object sender, System.EventArgs e )
{
ArrayList objList = new ArrayList();
objList.Add( new Package( 3 ) );
objList.Add( new Package( 15 ) );
objList.Add( new Package( 89 ) );
objList.Add( new Package( 101 ) );
objList.Add( new Package( 150 ) );
objList.Add( new Package( 176 ) );
objList.Add( new Package( 215 ) );
objList.Add( new Package( 226 ) );
foreach ( Package objValue in objList )
{
lblDisplay.Text += ( " " + objValue.PackageNumber );
}
} // end method FrmPackageNumber_Load
} // end class FrmPackageNumber
}
Computer Science & Information Technology
You might also like to view...
You generally work with two documents during a mail merge, the main document and the ________ source
Fill in the blank(s) with correct word
Computer Science & Information Technology
Saving an Excel worksheet as a ________ separates the cells of each row with tab characters
A) macro B) comma separated values C) text file D) template
Computer Science & Information Technology