November 2008
Beginner
696 pages
17h 43m
English
Exercise 9-1. You’ll use the following program for this exercise. Either type it into Visual Studio, or copy it from this book’s website. Note that this is spaghetti code—you’d never write method calls like this, but that’s why this is the debugging chapter.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Exercise_9_1
{
class Tester
{
public void Run( )
{
int myInt = 42;
float myFloat = 9.685f;
Console.WriteLine("Before starting: \n value of myInt:
{0} \n value of myFloat: {1}", myInt, myFloat);
// pass the variables by reference
Multiply( ref myInt, ref myFloat );
Console.WriteLine("After finishing: \n value of myInt:
{0} \n value of myFloat: {1}", myInt, myFloat);
}
private static void Multiply (ref int theInt,
ref float theFloat)
{
theInt = theInt * 2;
theFloat = theFloat *2;
Divide( ref theInt, ref theFloat);
}
private static void Divide (ref int theInt,
ref float theFloat)
{
theInt = theInt / 3;
theFloat = theFloat / 3;
Add(ref theInt, ref theFloat);
}
public static void Add(ref int theInt,
ref float theFloat)
{
theInt = theInt + theInt;
theFloat = theFloat + theFloat;
}
static void Main( )
{
Tester t = new Tester( );
t.Run( );
}
}
}Place a breakpoint in Run( ) on the following line, and then run the program:
Console.WriteLine("Before starting: \n value of myInt:
{0} \n value of myFloat: {1}", myInt, myFloat);What are the values of myInt and myFloat at the breakpoint?
Step into the Multiply( ) method, ...
Read now
Unlock full access