104 105 } // end class FrmMaximum 106 }


int intValue2 = 5;

private void btnEnter_Click(
object sender, System.EventArgs e )

{ int intValue1 = 10;
int intValue2 = 3;

Test( ref intValue1 );

lblDisplay.Text = Convert.ToString( intValue1 );

} // end method btnEnter_Click

void Test( ref int intValue1 )
{
intValue1 *= intValue2;

} // end method Test


Label lblDisplay displays the value of variable intValue1 (50). When the code

invokes the method Test, it passes intValue1 pass-by-reference. Any changes made to intValue1 in the method Test are reflected in btnEnter_Click’s local variable intValue1. When method Test multiplies intValue1 by intValue2, intValue2 is the class instance variable, whose value is 5. Method Test does not have access to btnEnter_Click’s local variable intValue2. The complete code reads:





// ScopeTest.cs



using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;



namespace ScopeTest

{

///

/// Summary description for FrmScopeTest.

///


public class FrmScopeTest : System.Windows.Forms.Form

{

private System.Windows.Forms.Button btnEnter;

private System.Windows.Forms.Label lblDisplay;

private System.Windows.Forms.Label lblResult;

///

/// Required designer variable.

///


private System.ComponentModel.Container components = null;







public FrmScopeTest()

{

//

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

}



// performs calculations with variables of different scopes

[STAThread]

static void Main()

{

Application.Run( new FrmScopeTest() );

}



// performs calculations with variables of different scopes

private void btnEnter_Click(

object sender, System.EventArgs e )

{

int intValue1 = 10;

int intValue2 = 3;

Test( ref intValue1 );

lblDisplay.Text = Convert.ToString( intValue1 );

} // end method btnEnter_Click

// multiplies two variables

void Test( ref int intValue1 )

{

intValue1 *= intValue2;

} // end method Test

} // end class FrmScopeTest

}



Computer Science & Information Technology

You might also like to view...

Arrays static method ________ applies a BinaryOperator to the current and previous array elements and stores the result in the current element.

a. prefix b. parallelSet c. parallelApply d. parallelPrefix

Computer Science & Information Technology

What happens to the size of an Access database when a table is linked to an Excel spreadsheet?

A) The linked table adds space to the database size. B) The linked table adds space byte for byte. C) The size of the database decreases in size. D) The size of the database remains the same.

Computer Science & Information Technology

efine a base class named Computer whose components are wordSize (in bits), memorySize (in megabytes), storageSize (in megabytes), and speed (in megahertz). Derive a Laptop class that is a kind of Computer, but also specifies the object's length, width, height (inches), and weight (pounds). Member functions for both classes should include a default constructor and a constructor that initializes

all components. What will be an ideal response?

Computer Science & Information Technology

Explain the difference between an absolute and a relative pathname, and why one might be used over the other.

What will be an ideal response?

Computer Science & Information Technology