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...

When a project manager creates a WBS, the manager will define the main goals of the scope of a project and then identify the tasks that must go into completing these goals

Indicate whether the statement is true or false

Computer Science & Information Technology

COGNITIVE ASSESSMENT Who is responsible for the security of data and information stored on computers and mobile devices within an organization?

A. computer security specialist B. security system project manager C. security analyst D. digital forensics analyst

Computer Science & Information Technology

____ from Technology Pathways is a forensics data analysis tool. You can use it to acquire and analyze data from several different file systems.

a. Guidance EnCase b.NTI SafeBack c. DataArrest SnapCopy d. ProDiscover Basic

Computer Science & Information Technology

For what reason would you want to use the Font dialog box?

a. It is the best way to multiple changes in font. b. There's no good reason to use the font dialog box. c. It provides font options that are not available on the Ribbon. d. It is the best way to select font theme colors.

Computer Science & Information Technology