Wrox Programmer Forums
|
BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3
This is the forum to discuss the Wrox book Beginning C# 3.0 : An Introduction to Object Oriented Programming by Jack Purdum; ISBN: 9780470261293
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 3rd, 2009, 08:27 AM
Nat Nat is offline
Registered User
 
Join Date: Dec 2009
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
Default Use of integers in Chapter 7

First of all - loving the book. OOP is completely new to me although I have coded before in FORTRAN and a bit of HTML and ASP and I have found the structure and techniques brilliant.

In Chapter 7 on page 168 the code:
Code:
 last = (int) randomNumber.Next(max);
is used. Why is the "(int)" necessary? and why is it not used when assigning to "current" three lines later?

Secondly in the factorial exercise at the end of the chapter could one use a "long" type integer as well as a "double"?

Thanks in advance for answers!
 
Old December 3rd, 2009, 05:15 PM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

Not got this far so I could be wrong but it is an explicit conversion which casts the value held in last into an int datatype.

Is what I would have said before I looked at the code provided in the book as last is already of type int?

Furthermore if you do happen to cast a variable that is already the same type and you make a mistake will the code, compile or provide a warning?
 
Old December 3rd, 2009, 05:23 PM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

I just tested this inthe code below:

Code:
 private void button1_Click(object sender, EventArgs e)
        {
            int last;

            Random number = new Random();

            last = number.Next();

            textBox1.Text = last.ToString();
runs exactly the same as:

Code:
 private void button1_Click(object sender, EventArgs e)
        {
            int last;

            Random number = new Random();

            last = (int) number.Next();

            textBox1.Text = last.ToString();
I don't know if I have missed something here but have you tried running the code without the (int) ? does it run as intended?
 
Old December 3rd, 2009, 10:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

The cast isn't necessary, and the compiler will ignore it. Random.Next() returns an Int32. No cast required.

Both of the blocks of code Will posted compile to 1 line of code:

Code:
int last = new Random().Next();
 
Old December 4th, 2009, 01:22 AM
Friend of Wrox
 
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default Cast

You're all right...the cast is NOT necessary. However, it reflects a habit I got into when I was programming in C and earlier versions of C#. The random number generator used to return a value between 0 and 1.0 and was a double data type. Since a double requires 8 bytes and an int only uses 4 bytes, this would be an example of data narrowing (see page 67). Trying to assign an 8 byte thingie into a 4 byte thingie (i.e., data narrowing) runs the risk of losing information, you should always use a cast. It's like trying to pour 8 gallons of data into a 4 gallon bucket; some of the data is going to slop over the sides of the smaller bucket. That's why casts must always be used when data narrowing.

If you reverse the assignment direction; that is, assigning a 4 byte object into an 8 byte object, this is called data widening. Pouring 4 gallons of data into an 8 gallon bucket runs no risk of data lost and the use of a cast is not required. However, I always try to code so that I use a cast when data objects do not match. In other words, the cast is also a form of documentation that tells the reader of my code that I really intended to do this. In commercial code, I even do this with data widening. (There's no performance hit, since the compiler throws it away.)

The cast in this example is not necessary at all since the new random number generator defaults to returning an int data type. My mind was probably stuck with the old versions I used to use that always returned a double. Always keep in mind, however, that casts should always be used when assigning different data types, if for no other reason that it documents what you're doing. I think it's a good idea to always use casts whenever the data types don't match.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
 
Old December 4th, 2009, 01:29 AM
Friend of Wrox
 
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default Factorial question

I forgot to answer the factorial question. Because factorial are normally integral values, you could a long for the data type. However, even as big as a long is, relatively small numbers overflow the factorial calculation quickly. (If I remember correctly, the number is less than 100! to overflow a long.) If you need really big factorials, you can use Sterlings Approximation with a double data type.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
The Following User Says Thank You to DrPurdum For This Useful Post:
Nat (December 4th, 2009)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Unsigned integers AndrewH Java Basics 1 September 26th, 2006 02:46 PM
getting Integers from the query string TadReeves JSP Basics 2 March 16th, 2005 01:22 PM
Using integers with text boxes clojinted General .NET 1 February 19th, 2005 08:12 PM
how can i convert strings into integers amahja56 C++ Programming 2 February 27th, 2004 06:27 PM
Sum of All Integers Between 2 numbers bicho Classic ASP Basics 4 July 6th, 2003 01:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.