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 .
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.
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 ...
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?
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?
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>
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.