Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > BOOK: Beginning Visual C# 2005
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Visual C# 2005
This is the forum to discuss the Wrox book Beginning Visual C# 2005 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, Eric White; ISBN: 9780764578472
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 23rd, 2008, 02:55 PM
Registered User
 
Join Date: Mar 2008
Location: , , USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default ch06ex05 pg 146 problem

With only minor change to a name, i attempted to run this example. No luck. I get the error //D:\TrainingFiles\BegVCSharp\Chapter06\ch06ex05a\ch 06ex05a\ch06ex05a\Class1.cs(58): Use of unassigned local variable 'myProcess'. Any ideas? Does anyone get it to run? Here is the code:
    class Class1
    {
        //CREATE THE DELEGATE PART -- MUCH LIKE A POINTER
        delegate double ProcessDelegate(double param1, double param2);

        //COUPLE OF NORMAL FUNCTIONS
        static double Multiply(double param1, double param2)
        {
            return param1 * param2;
        }

        static double Divide(double param1, double param2)
        {
            return param1 / param2;
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            //
            ProcessDelegate myProcess; //declare the delegate

            Console.Write("Enter 2 numbers, separated by a comma: ");
            string input = Console.ReadLine();

            int commaPos = input.IndexOf(",");
            double p1 = Convert.ToDouble(input.Substring(0, commaPos));
            double p2 = Convert.ToDouble(input.Substring(commaPos + 1, input.Length - commaPos - 1));

            //SET UP THE DELEGATE TO USE
            Console.Write("Enter M to Multiply or D to Divide: ");
            input = Console.ReadLine().ToUpper();

            //myProcess = new ProcessDelegate(Multiply);

            if (input == "M") //bad form, since "q" input caused divide
            {
                myProcess = new ProcessDelegate(Multiply);
            }
            else
            {
                if (input == "D")
                {
                    myProcess = new ProcessDelegate(Divide);
                }
            }

            Console.WriteLine("Result: {0} ", (myProcess(p1, p2))); //use the delegate
            //we get this error
            //D:\TrainingFiles\BegVCSharp\Chapter06\ch06ex05a\ch 06ex05a\ch06ex05a\Class1.cs(58): Use of unassigned local variable 'myProcess'

            Console.Read();
        }
    }


If you are not part of the solution, there is good money to be made by prolonging the problem.
Reply With Quote
  #2 (permalink)  
Old March 30th, 2008, 11:39 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

This warning says that there are situations where myProcess may have no value. That is, when you enter a value other than M or D, myProcess will still be null.

To avoid the error, assign null to myProcess when you declare it:
Code:
ProcessDelegate myProcess = null; //declare the delegate
Additionally, you may want to check if it's null before using it:
Code:
if (myProcess != null)
{
  Console.WriteLine("Result: {0} ", (myProcess(p1, p2))); 
}
Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Code chapter 5 pg 145-146 PHP Newb BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 3 August 25th, 2006 08:33 PM
Try It Out Page 146 Chapter 5 sosborne BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 1 August 7th, 2006 05:53 PM
Rules on use of == double equals (see p142, 146) ababb BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 January 20th, 2005 05:58 PM



All times are GMT -4. The time now is 02:56 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.