Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 2008 > Visual Basic 2008 Essentials
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Visual Basic 2008 Essentials If you are new to Visual Basic programming with version 2008, this is the place to start your questions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2008 Essentials 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 June 8th, 2008, 02:26 PM
Registered User
 
Join Date: Jun 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Is this forum too advanced for me?

I have been trying to learn Visual Basic for a loooong time. I progressed through chapter 12 of Visual Basic 2005, but realized I just ain't getting it. I backed up to chapter 10. I kinda-sorta understand what is going on with the code, but I believe I am not grasping some of the basic fundamentals.

For example: In chapter eleven I am studying code from the book:

'Public Members

Public Name As String
Public URL As String


I understand what these "members" do and what they are for. But I don't understand what a "member" is. I don't understand why they can't be variables preceded with a "DIM" keyword. I have tried to research what a "member" is but . . .

I have reviewed lots of the questions on this forum and these "beginners" are WAY beyond me.

Can you please direct me to a forum for dumb farm boys? Or maybe even a book.

Reply With Quote
  #2 (permalink)  
Old June 8th, 2008, 03:33 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Will all of the dumb farm boys please stand up! ::Stands up:: ;] I don't think you need directed anywhere else as most the "regulars" here gauge their responses based upon the types of questions asked.

To answer your question a member is anything that belongs to a class: methods, properties, variables they are all considered members of a class. For a good analogy think of a class as a type of club for example the Boy Scouts of America (for the sake of complexity lets just focus on a single entity within the BSA: a troop).

A troop is composed of different people, functions, and things: scouts, tents, activites, and soforth. Each of these things can be considered a member of a given troop becaue they belong to said troop. If you apply this to a VB Class it might look like this:

Public Class Troop27

  Public Scout1 as String
  Public Scout2 as String
  Public Tent1 as String


  Public Sub GoCamping()
    'Send the scouts camping
  End Sub
End Class

So all a member is is something that exists within a class! To further answer your question as to why you wouldn't just declare the variables Name and URL with DIM keyword has to do with "scope". To get an idea of what scope is, focus on the method from my previous example called GoCamping() where the scouts of the current troop would go camping.

Because I have declared the method with the Public keyword it means that other classes (troops) within my application can also use the GoCamping() method (think of this like posting a flyer in a public place for a party, you don't really know who is going to show up since you have posted it public place thus have no control over the attendees). So lets assume that the scout master of the Troop27 class has said that GoCamping() is a private event and that no other troops should be allowed to GoCamping(). To solve this problem you would simply change the Public keyword to Private and this would block all other troops from being able to use GoCamping(). This is scope. When the method was declared with the Public keyword it means that any other class file could access this method but, by changing the declaration to private, you have ensured that only methods from within the Troop27 class can call GoCamping().

What does this have to do with the Dim keyword? Well if you had changed your variable declaration to:

Dim Name As String
Dim URL As String

You are implictly declaring those variables with the Private keyword, thus preventing them from being accessed from outside your current class.

I could further expand this example but I think it best for you to read this and try and digest it, if you get it great, you can continue to ask questions but if there is something you still aren't getting, we can further go over this example.

hth.
-Doug

Welcome to the forums btw!

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
Reply With Quote
  #3 (permalink)  
Old June 9th, 2008, 03:38 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

To add a little:

Declaring a variable outside a procedure opens the options to make that variable accessible only within the module where you are declaring it (private), or make it available outside that module (public). In both cases you are "Dim"ming it. Within a procedure (whether a sub, function or property) there is no option to produce a variable with scope outside the procedure. So within a procedure, they keywor Dim is used. Outside any procedures, Dim is not very explicit. (If it had been me, I would have prohibitted using Dim outside a proceure, forcing writers of code to be explicit with the scope.

For the purpose of what I just wrote, Module means within a file explicitly called a module, or within a "Class module." In VB6, code was in one or the other, but not both. Those boundaries are not as rigid in .NET...

If, as you say, “I understand what these ‘members’ do and what they are for,” then you understand much better than you realize. Hang in there.
Reply With Quote
  #4 (permalink)  
Old June 9th, 2008, 05:26 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

To live up to my screen name...

DParsons wrote:
"To solve this problem you would simply change the Public keyword to Private and this would block all other troops from being able to use GoCamping(). This is scope."

Ummm...No, it is not.

The scope of a variable is *NOT* changed by whether it is public or private. If the variable is a member of the class, it *still* has "class scope." Only the accessibility of the variable is changed by choosing public or private.

Oh, and let's not forget about the other two accessibility modifiers: Friend and Protected, of course. Though I will grant you that those probably don't need to be learned by beginners.

Sorry for being the Old Pedant, but that's what I am.
Reply With Quote
  #5 (permalink)  
Old June 9th, 2008, 05:29 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Brian wrote: "[i]I would have prohibited using Dim outside a procedure, forcing writers of code to be explicit with the scope."

APPLAUSE!!!

YEAH!!!

But given that the VB.NET team couldn't even get the default setting of OPTION STRICT to default to ON (probably the most nutso decision made), I guess it's not surprising that they got this wrong.
Reply With Quote
  #6 (permalink)  
Old June 9th, 2008, 07:17 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

First, mmm yep, I miss spoke with regard to accessiblity vs. scope, its bound to happen from time to time.

Second up, lets not criticize the VB team without being equally as critical to the C# Team. Do I agree with Brian that you should not be able to use Dim keyword outside of methods, yes absolutley, you should make the intent of your variables very clearly known by using a precceding keyword (Public, Private, etc) so that other developers that might pick up your code know what is going on (or can at least get an idea). To this end, C# does an equally poor job of this since I can declare a private class variable as:

string foo;

or

prviate string foo;

Anyway thanks for catching my goof.

-Doug

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
Reply With Quote
  #7 (permalink)  
Old June 9th, 2008, 08:07 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Excellent point about C# missing the boat, too.

Of course, they have the precedent of C++ and Java to say that incompleteness is adequate. <grin class="evil" style="smirk">

But regardless, I still say the biggest disservice the VB marketing people did was push the VB architects into making OPTION STRICT OFF be the default. I know some of the people on the team who fought that one but ultimately had to give in to the marketing types.

I say "disservice" because all it REALLY does is move the bug-finding exercise away from compile time and into run time, when of course it is more difficult. Not to mention that it can hide some pretty subtle bugs that may not creep up until a product has been in the field a while. [Favorite example: something like
     a = b * c
with all 3 variables undeclared. So it works until some day two years from now when some user enters a value of "foo" that ends up in the c variable and then the multiply blows up. Just plain SILLY to catch that at run time!]

Oh, well.

Fun stuff.
Reply With Quote
  #8 (permalink)  
Old June 9th, 2008, 08:31 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Very good points. One could argue, however, that with proper Unit Testing/QA this type of bug would be caught before a "complete" version went out the door. I realize that this is a basic example that you are using to illustrate your point and agree that setting OPTION STRICT to off by default is just damn silly but, then again, there is the Microsoft way of doing something and the right way of doing something. ;]


-Doug


================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
Reply With Quote
  #9 (permalink)  
Old June 11th, 2008, 07:27 PM
Registered User
 
Join Date: Jun 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You guys are tooo funny. That said . . I would like to say thank you for all your input. I have not used forums much in the past (more of a book guy). It has been hard for me to accept that such VALUABLY HELPFUL information is so readily available. I sure appreciate you guys.

I DID experiment with a "PUBLIC Dim varname As String" statement (outside of a procedure) The IDE changed it to " Public varname As String"

Thank you for your prompt response dparsons. BrianWren's addition and Old Pedant's input also deepened a wrinkle in my brain as well.

Thanks again!

(I'll be back)

Reply With Quote
  #10 (permalink)  
Old June 11th, 2008, 08:49 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Yeah, another case of the MS left hand not knowning what the MS right hand wrote.

If you just use notepad to write your VB code and then compile/run, it is quite happy combining PUBLIC and DIM. It's just VS that tries to be "friendly" and "correct" you to what it thinks is the standardized form. Even though the docs say that DIM is optional.

Oh, well.

Not that it matters. You can just forget about the existence of DIM for all that it matters and actually end up with better code, per all three of us, I think.
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
Where is Advanced Search ? MikeW2 BOOK: Beginning SharePoint 2007: Building Team Solutions with MOSS 2007 ISBN: 978-0-470-12449-9 2 August 13th, 2007 03:36 AM
What is Advanced PHP raaj Beginning PHP 6 December 21st, 2006 12:51 PM
Looking for advanced CSS dimo414 BOOK: Professional CSS: Cascading Style Sheets for Web Design 0 May 29th, 2006 10:57 PM
How can I creat a forum like P2P Forum? pcassiano ASP.NET 1.0 and 1.1 Basics 1 October 31st, 2005 05:00 AM
converting Forum.aspx to Forum.ascx (help) drfunkie BOOK: ASP.NET Website Programming Problem-Design-Solution 1 July 11th, 2003 12:27 PM



All times are GMT -4. The time now is 09:49 AM.


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