Wrox Programmer Forums
|
Visual Studio 2008 For discussing Visual Studio 2008. Please post code questions about a specific language (C#, VB, ASP.NET, etc) in the correct language forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Studio 2008 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 April 14th, 2010, 09:27 PM
Registered User
 
Join Date: Apr 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Squirming in my programming chair.

So I’m sitting here about 5 weeks into my VB 2008 journey and I don’t get it! I need help to understand a few things. I’ve worked in programming for 14 years and I’ve programmed SQL, VB, VBA and Access. I just don’t understand the consistency of the syntax of this language.

I will try and be brief but here’s a few things I would like to ask. In the first few pages of the book you introduce MessageBox.Show(n) WOW! Hold on a minute! Where did that switcharoo come from? In VB I’ve never done cmd.method(parms) Not like that!
Why would you need MessageBox.Show anyway? What else are you going to do with it? I know I can still call the old Msgbox command but I want to know where the idea of cmd.method(parms) come from?

Continuing the above theme, there is intNumber.ToString. Again, Why? That is confusing as we have parm.cmd. This is inconsistent with the above message box example. In VB it was a command, followed by a parameter. Now it seems like its switched up every which way.

Again continuing the above theme system.strings.Left(StrName,3). Again, why? I don’t have to do system.math.add (aNumber) so why the song and dance with the left command?

The overall point I am trying to make is how am I supposed to learn VB2008 if it is so random (or appears so random)? It seems like the structure of the language is extremely poorly thought out. I understand that they had to go completely OO but I am not sure this is what VB programmers really wanted. Someone please help me. I really want to learn this language but at every corner I seems to be disagreeing with it.

Thanks
 
Old April 15th, 2010, 03:21 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

I think what's confusing you is the difference between Shared and Instance methods. Consider this (fake) example:

Code:
 
Public Class Messagebox
  Public Shared Sub SharedShow()
  End Sub
  Public Sub InstanceShow()
  End Sub
End Class
Notice the Shared keyword on the MessageBox class the SharedShow method. Shared means the method applies to the entire class definition, and not to an instance of a class. As such, you can do this:

MessageBox.SharedShow()

Shared methods are great for utility methods that don't need their own state. They are convenient as you don't have to create an instance of the class first. The built-in MessageBox class is a great example of that: there's no need to force you to write more code and instantiate a class if you can just call a Shared method that pops up a message box.

Now, consider the second method which is not shared. In order to call it, you need to create an instance first:

Code:
 
Dim myBox As New Messagebox()
myBox.InstanceShow()
Here, InstanceShow is a method that can be called on an instance of MessageBox, and as such you have to declare and instantiate one first.

And to answer your answer questions:

Quote:
Why would you need MessageBox.Show anyway? What else are you going to do with it?
In this example, not much. The only useful shared method on MessageBox is Show. However, it has 21 overloads that help you show all kind of different messageboxes. Check the IntelliSense list for the Show method for more details.

Code:
Continuing the above theme, there is intNumber.ToString. Again, Why?
Assuming intNumber is an Integer, this class gets its method from Object. All objects in .NET inherit Object (except for Object itself). On the Object class, the ToString method is defined. It's default behavior (if not overriden by a child class) is to say its name. However, for Integer, the ToString mthod (inheirited from Object) has been overriden to return itself (the number) as a String. Here, ToString() is an instance method and as such you need an instance of the Integer (intNumber).

This works:

intNumber.ToString()

but this doesn't:

Integer.ToString()

A Shared method wouldn't make sense here as the object has its own internal state (the number). You don't want all Integers in your application to return the same number ;-)

Quote:
Again continuing the above theme system.strings.Left(StrName,3).
I don't understand this one, as the way it's written now, it doesn't compile. What you normally would do is call an instance method on the string:

Dim strName = "Imar"
Dim subString As String = strName.Substring(0, 3)
MessageBox.Show(subString)

This would display the text Ima. Maybe in this example, Left was used to show how you can still use the old VB methods such as MsgBox and Left?

Hope this clarifies things a bit.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!

Last edited by Imar; April 15th, 2010 at 03:56 AM.. Reason: Changed MessageBox class to SharedShow method
 
Old April 15th, 2010, 10:12 AM
Registered User
 
Join Date: Apr 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply. I probably should have explained things a little better. What I was trying to say is that I think VB6 syntax was a little more structured.

What is confusing me the most is the location of all the functions and methods I use in my code.

I will give a better example here.

VB 6 vs VB 2008

covert integer to string
VB6 CStr(IntNumber)
VB2008 IntNumber.ToString

get left 3 chracters of a string
VB6 Left(StrName,3)
VB2008 Sytem.Strings.Left(StrName,3)

display a message box
VB6 Msgbox("Hello")
VB2008 Messagebox.show("Hello")

back color on textbox
VB6 TextBox.BackColor = "red"
VB2008 TextBox.BackColor = Color.Red

In VB6, the syntax is structured and intuative. It is always commands/properties on the left and settings/parameters on the right. But this isn't the case in VB 2008. It's all over the place and makes it difficult to learn. For example, in Vb 2008, if I wanted to trim a string, I wouldn't know if I should do:

Trim(StrName)
StrName.Trim or
System.strings.Trim(StrName).

I am sure only one of those is valid syntax and I know intellisense would only allow me to type the correct one. But it's not intuitive.

Why did they need to muddy the waters with the syntax? Am I the only one who is confused by the changes? Or do other people have the same issue?

Is this just all part and parcel of learning VB 2008? Seems like a lot of people have just accepted it and moved on.

Last edited by PhillD; April 15th, 2010 at 10:15 AM..
 
Old April 15th, 2010, 10:51 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Phil, I have the feeling that you didn´t understood the object paradigm. Could be that the problem???

Before you call for functions to make several things. Now every object has it's own function. That maybe look weird, but using that paradigm, you can inherit from a integer to build for example your own odd numbers integer class (silly, but could be done)...
Obviously Imar is better explaining this kind of things, this are my 2 cents...
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 
Old April 15th, 2010, 04:43 PM
Registered User
 
Join Date: Apr 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think you hit the nail on the head. I guess I don't understand it. Well perhaps I do, but I'm not sure if I agree with it. It just seems to have made life more "complicated".
 
Old April 16th, 2010, 02:40 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
Well perhaps I do, but I'm not sure if I agree with it. It just seems to have made life more "complicated".
I can imagine it looks more complicated now. But I am pretty sure once you get the hang of it, you'll appreciate the cleanlines of this model. No more global functions that come out of nowhere, but a much cleaner and eventually intuitive way to deal with yoour code.

I would recommend an introduction to object oriented programming, next to your VB book. That way, you'll get a much better understanding of how OO workds, what benefits it has, and why you need to write the syntax you're writing now.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!

Last edited by Imar; April 16th, 2010 at 03:31 AM..
 
Old April 17th, 2010, 07:44 PM
Registered User
 
Join Date: Apr 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks again. I have the beginners VB 2008 book. Guess I need to adapt. Hopefully it will begin to make more sense.
 
Old April 18th, 2010, 03:56 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hopefully it does. If not, be sure to post follow up questions here.

Good luck adapting..... ;-)

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Squirming in my Prgramming Chair PhillD BOOK: Beginning Microsoft Visual Basic 2008 ISBN: 978-0-470-19134-7 1 April 14th, 2010 09:29 PM
device DLL programming in client side programming hendyhanusin ASP.NET 1.0 and 1.1 Professional 2 February 19th, 2009 12:01 PM
device DLL programming in client side programming hendyhanusin ASP.NET 1.0 and 1.1 Basics 0 March 21st, 2007 08:05 AM
device DLL programming in client side programming hendyhanusin ASP.NET 2.0 Professional 1 March 21st, 2007 08:04 AM
Qt programming, bugs in first programming sateeshgalla BOOK Beginning Linux Programming, 3rd Edition 0 October 14th, 2005 08:19 AM





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