What’s wrong with this code? This code should remove all commas from strTest. Find the error(s) in the following code.


string strTest = "Bug,2,Bug";
strTest = strTest.Replace( "" );


The Replace method takes two arguments: one substring to search for, and another

substring to replace all matching occurrences of the first argument. The complete incorrect code reads:





// RemoveCommas.cs (Incorrect)



using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;



namespace RemoveCommas

{

///

/// Summary description for FrmRemoveCommas.

///


public class FrmRemoveCommas : System.Windows.Forms.Form

{

private System.Windows.Forms.Label lblResult;

private System.Windows.Forms.Label lblOriginalOut;

private System.Windows.Forms.Label lblModified;

private System.Windows.Forms.Label lblOriginal;

///

/// Required designer variable.

///


private System.ComponentModel.Container components = null;



public FrmRemoveCommas()

{

//

// 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 FrmRemoveCommas() );

}



// removes all commas from the string and displays the result

private void FrmRemoveCommas_Load(

object sender, System.EventArgs e )

{

string strTest = "Bug,2,Bug";

strTest = strTest.Replace( "" );



lblResult.Text = strTest;



} // end method FrmRemoveCommas_Load



} // end class FrmRemoveCommas

}









The complete corrected code should read:



// RemoveCommas.cs (Correct)



using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;



namespace RemoveCommas

{

///

/// Summary description for FrmRemoveCommas.

///


public class FrmRemoveCommas : System.Windows.Forms.Form

{

private System.Windows.Forms.Label lblResult;

private System.Windows.Forms.Label lblOriginalOut;

private System.Windows.Forms.Label lblModified;

private System.Windows.Forms.Label lblOriginal;

///

/// Required designer variable.

///


private System.ComponentModel.Container components = null;



public FrmRemoveCommas()

{

//

// 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 FrmRemoveCommas() );

}



// removes all commas from the string and displays the result

private void FrmRemoveCommas_Load(

object sender, System.EventArgs e )

{

string strTest = "Bug,2,Bug";

strTest = strTest.Replace( ",", "" );



lblResult.Text = strTest;



} // end method FrmRemoveCommas_Load



} // end class FrmRemoveCommas

}



Computer Science & Information Technology

You might also like to view...

What is the difference between static and dynamic data structures?

What will be an ideal response?

Computer Science & Information Technology

Which media device uses the characteristic continuous?

A. embossed B. print C. projection D. screen

Computer Science & Information Technology

Create suitable names for Boolean variables to be used as flags. The variables will be true for each of the following situations and false otherwise. State names in a positive way and avoid including the terms not or no as part of the name.

When there are no more data items available for processing

Computer Science & Information Technology

The ____ tool is used to select stroke, fill, and text attributes so they can be copied from one object to another.

A. Eyedropper B. Eraser C. Ink Bottle D. Paint Bucket

Computer Science & Information Technology