Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Microsoft Visual Basic 2008 ISBN: 978-0-470-19134-7
This is the forum to discuss the Wrox book Beginning Microsoft Visual Basic 2008 by Thearon Willis, Bryan Newsome; ISBN: 9780470191347
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Microsoft Visual Basic 2008 ISBN: 978-0-470-19134-7 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 Display Modes
  #1 (permalink)  
Old October 19th, 2008, 05:54 PM
Registered User
 
Join Date: Oct 2008
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 3 exercises

Ok I did the first exercise in chapter 3.

The book requests that you decalare 2 variables, preform an operation on the variables and display the result!

My question is this! If I follow the books request to the "T" my code looked like this!

Quote:
quote:
        'Declare the two variables and set there value
        Dim dblNumberA As Double = 100
        Dim dblNumberB As Double = 10


        'Preform the operation
        dblNumberA = dblNumberA + dblNumberB

        'Display the result
        MessageBox.Show(dblNumberA, "Integer Exercise")


Besides keeping variables uniform and not reseting previous variable. Would there be any disadvantages or advantages to delacring a 3rd variable "dblResult" and setting its value to what ever the operation returns.

Like this:

Quote:
quote:Private Sub btnIntEx_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIntEx.Click
        'Declare the two variables and set there value
        Dim dblNumberA As Double = 100
        Dim dblNumberB As Double = 10
        Dim dblResult As Double

        'Prefor the operation
        dblResult = dblNumberA + dblNumberB 'I had originally used dblResult as the operation result, changed to conform to the books request.
        'Personally I would rather make a new variable then reset an existing!

        'Display the result
        MessageBox.Show(dblResult, "Integer Exercise")

    End Sub



I tested both codes and they both do the same thing. The only draw I can see from option 2 is that it requires more memory then option 1.

Any other ideas?

Reply With Quote
  #2 (permalink)  
Old January 7th, 2009, 06:43 PM
Registered User
 
Join Date: Jan 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm new to programming and VB myself but I'll still try to explain, though you probably found out already.

The results of both methods are the same, but the use is different and the use depends on what you try to accomplish.

In chapter 3 loops are not an issue yet, but when you use something like:

i = i + 1 or

i +=1 (both are the same)

It can be used for iterations and loops that should stop after a predefined number of steps.

Your second option is used for normal calculations like:

c = a + b

I hope it explained it a little.

Bart
Reply With Quote
  #3 (permalink)  
Old January 7th, 2009, 07:20 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I have to ask a silly question, first:

Why is your code
Code:
MessageBox.Show(dblResult, "Integer Exercise")
instead of
Code:
MessageBox.Show(dblResult, "Double Exercise")
???

Okay, enough with the attempt at humor.

The answer to your question is really pretty simple:

You would, indeed, use a third variable to hold the result of the computation if that result will be used more than once. In other words, if you needed to (example only) store that result in the database and also display it to the user.

You gain better performance (admittedly, only a little...but lots of little gains can make a difference!) by doing this instead of making the computer perform the same operation several times. True, you use a tiny bit more memory by adding the third variable, but generally (not always!) memory is "cheaper" than CPU time.

Now, there is clearly nothing "wrong" in using that results variable under any circumstances. Even if it's only then used once. And some programmers will indeed always create the variable, just to emphasize what the "meaning" of the operation is. Example:
Code:
Dim SellingPrice As Double = UnitPrice * ( 100.0 - DiscountPercentage ) / 100
So, unfortunately for you, the "correct" answer here is the same as the answer to almost every programming question you can ask:
It depends ...
Reply With Quote
  #4 (permalink)  
Old January 7th, 2009, 07:25 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Quote:
Originally Posted by Bart View Post
In chapter 3 loops are not an issue yet, but when you use something like:

i = i + 1 or

i +=1 (both are the same)

It can be used for iterations and loops that should stop after a predefined number of steps.
Well, yes. But...

But there's really nothing "wrong" with doing
Code:
Dim i As Integer = 0
Do While i < 10
    Dim foo As Integer = i + 1
    ... some other code ...
    i = foo
Loop
Now, I grant you, if you have no other use for foo in the "other code", then this is mildly silly. But it's not "wrong" in any real sense.

Just never forget your major guiding principle:
It depends...
Reply With Quote
  #5 (permalink)  
Old January 8th, 2009, 02:54 AM
Registered User
 
Join Date: Jan 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Old Pedant View Post
It depends...
That's how I started my reply: it depends on what you're trying to accomplish.

In loops one can and many times has to use results from somewhere else in the code, but for most simple loop this will not be used, like your foo example. I once had a math teacher that had as personal motto: Why do it the simple way when one can do it the hard way...?
Though your foo example isn't wrong, it sure isn't the easiest way.

C++ isn't called C++ for nothing as well. Why didn't they make it "C = Foo + Y" you think?

Bart
Reply With Quote
  #6 (permalink)  
Old January 8th, 2009, 05:14 AM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

No, no...you miss the point!

C++ is, of course, just D !!! The grade it should get.

But then C# is really D-flat. So it's no improvement.

<grin style="monstrous" />

Last edited by Old Pedant; January 8th, 2009 at 05:19 AM.
Reply With Quote
  #7 (permalink)  
Old January 8th, 2009, 05:17 AM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Actually, by its own rules, the value of the expression
Code:
    C++
(as, for example, if you did something like
Code:
    x = C++;
is just the value of C!!!

So the value of C++ is no more than the value of C.

Now...*AFTER* you use C++, the next time you use it (since by now the post-increment has happened) its value has dropped to D.

Or maybe we should go with the old musical joke:
Quote:
If you don't C# you'll Bb.
*********************

p.s.: Actually, I like C++. And C#. But I'd probably pick Java if all other things were equal.
Reply With Quote
  #8 (permalink)  
Old January 8th, 2009, 05:40 PM
Registered User
 
Join Date: Jan 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Old Pedant View Post

p.s.: Actually, I like C++. And C#. But I'd probably pick Java if all other things were equal.
You're funny, but beating around the bush a little ;)

But hopefully not too far off topic (this thread seems to be pretty dead anyway) what's so different about Java? The GUI?

When I've finished this book (I'm now at ch.12) I want to start learning C# and later on when I have the time probably some C++ just for fun in the future (I already did a couple of chapters on the MS site about C++). I don't expect to ever create full applications with C++, though that seems to become easier as well, so who knows... So far I only have Visual Studio Express and I'm still in doubt what to buy, the standard or pro version.

But isn't it just better to mainly learn .NET languages, C# and VB, for not too professional (I mean a good amateur level) desktop and web apps? What would be the benefit of learning Java (J++ and/or J#) as well?

Bart
Reply With Quote
  #9 (permalink)  
Old January 8th, 2009, 05:59 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Certainly would *NOT* use Java with Windows or .NET.

But I do a lot of JSP work on Linux, which of course uses Java.

There's not a whole lot of conceptual difference between Java and C#. Syntax is nearly the same. Semantics, ditto. Mostly it's the wealth of free libraries/source code that is out there for Java, since it's an older language than C#.

I do admit that Visual Studio is a draw; it's not perfect, but it's nearest competitor in the Open Source world (for Java) is Eclipse. And Eclipse doesn't understand JSP "natively"; you have to add in several "plug ins" to get the same kind of interaction you do with VS. But the plugins are developed by different people and have differing levels of quality and and and... It's quite usable, but just not as "polished" as VS. Ehhh...but it's also free. <shrug>
Reply With Quote
  #10 (permalink)  
Old January 8th, 2009, 06:40 PM
Registered User
 
Join Date: Jan 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

For what I've read in VS ( I couldn't come up with an alternative) it's possible to develop in Java (I don't remember what version, but I assume J#) when you get the right plugin. But C# (VB.Net still in development) can run on Linux as well when compiled with Mono. So for what you just told me there would be no direct need for me to develop something in whatever version of Java, as I don't yet feel the need to point my arrows on Linux (just yet) and if ever I could use Mono, which is for free as well.
So I don't think I'll be going much further than VB and C# in VS for now.

Bart
Reply With Quote
Reply


Thread Tools
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
Chapter 2 - End of chapter exercises whizzkid1892 BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 1 July 30th, 2008 01:02 PM



All times are GMT -4. The time now is 08:04 AM.


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