Wrox Programmer Forums
|
BOOK: Visual Basic 2005 Programmer's Reference
This is the forum to discuss the Wrox book Visual Basic 2005 Programmer's Reference by Rod Stephens; ISBN: 9780764571985
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Visual Basic 2005 Programmer's Reference 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 March 30th, 2006, 06:01 PM
Registered User
 
Join Date: Mar 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Appendix P: Can't Use My Namespace

I have been programming in VB 6.0 for about 5 years. I avoided .NET 2002 and 2003, so I am now using your book to sling-shot me forward into the world of object-oriented programming and .NET.

I have written my first VB 2005 Windows Application, a simple form (a Class simply called MyFirstProject), and have successfully added controls, menus, tooltips, etc. to this form using the new object-oriented ways if VB/VS 2005. And, it works just fine, until I wanted to try some of the wonderful features, like serial ports, in the My Namespace (in Appendix P).

I have tried adding a Reference to "My Namespace" using the [Add] button on the References dialog, but I can't find anything close to it. I must have missed some basic point in your book about how to use/declare/import/reference Namespaces. So, just how do I get my simple class module (a form) to reference the many wonderful features of the My Namespace????? I have tried many of your examples and they just don't work. I've tried to reference them with fully qualified namespace names, and tried all sorts of Imports statements, but without success.

My VB/VS 2005 is a late "beta" that Microsoft gave out as "final" at a recent seminar in Richmond. Do you think its missing the "My Namespace"? What is it called in the [Add] reference dialog?

Truman Byron Thomas
 
Old March 31st, 2006, 10:16 AM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

My isn't a "real" namespace in the same sense that others are. It is auto-generated for your application within the hidden files so you don't need to add a reference to it. You shouldn't have to do anything to get My to work. Just open a code window, type "My." and you should see an IntelliSense dropdown appear.

If your application's name is WindowsApplication1, then if you look in the Object Browser, you should be able to see it in WindowsApplication1/WindowsApplication1.My.

The actual Namespace declarations are in auto-generated files. To see them, click the "Show All Files Button." You can find "Namespace My" statements in these files:

    My Project/Application.myapp/Application.Designer.vb
    My Project/Settings.settings/Settings.Designer.vb

You'll find the My.Resources part in:

    My Project/Resources.resx/Resources.Designer.vb


Rod
RodStephens@vb-helper.com
Author of "Visual Basic 2005 Programmer's Reference"
http://www.vb-helper.com/vb_prog_ref.htm
 
Old April 3rd, 2006, 09:49 AM
Registered User
 
Join Date: Mar 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your quick reply. I can now address the My Namespace on my form and use Intellisense to complete my code. However, I still get serious unexplained errors trying to use a simple Serial Port device(which I have been using for years with VB 6). Maybe an example would help:

On page 968 of the book, in 1st paragraph after header Ports I tried to do exactly what you say:

Dim ports as String()
ports = My.Computer.Ports.SerialPortNames

But, I get a difficult to fathom error message. After some hand wringing, and vain attempts to use CStr(), I tried to do what the error message seemed to say and declared what it wanted:

Dim ports As System.Collections.ObjectModel.ReadOnlyCollection( Of String)
ports = My.Computer.Ports.SerialPortNames

And, sure enough the error went away, and I now have an array of 3 elements of "something" containing "COM2", "COM1", and "COM10" strings.

As an old VB 6.0 Old Timer this kind of result is frightening. Why do things like this seem so simple in print, yet they are so complicated in practice? Its a miracle I finally figured it out! What am I missing about using VB 2005 that would lead me to know the correct answer in the first place? Would the "Object Viewer" (which I have not used yet) have helped me understand this, when Intellisense did not seen to help?

Is the SerialPort "class", that you talk about in the remainder of this section of Appendix P, starting with 3rd paragraph, actually unrelated to the My.Computer.Ports namespace? Is it, perhaps, the SerialPort objects you can add to your form from the ToolBox under Components?

One final question. After I get my Serial Ports working, I still have many I/O devices to access via TCP/IP/Ethernet connections. VB 2005 seems to be totally silent about network devices (i.e., Winsock controls in VB 6). On same page of book, My.Computer.Network certainly seems to be a dud in that regard. What is the VB 2005 equivalent of such Socket devices? If you did not intend to talk about such devices in our book, can you recommend where I might find this information.


Truman Byron Thomas
 
Old April 6th, 2006, 08:31 AM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Sorry about that. The book wasn't precise enough. You can treat My.Computer.Ports.SerialPortNames sort of like an array as in:

    Debug.WriteLine(My.Computer.Ports.SerialPortNames( 1))

But as you discovered it's really a collection. A Read-only collection in fact. VB 2005 has gone a bit overboard with read-only collections. Generally the distinction doesn't matter and you can do things like loop through the items and see what they are but you can't modify the collection (add or remove items) and as you found you can't assign the whole thing to a string array.

> What am I missing about using VB 2005 that would lead me to
> know the correct answer in the first place? Would the
> "Object Viewer" (which I have not used yet) have helped me
> understand this, when Intellisense did not seen to help?

There are a couple of tricks. The ObjectViewer can help. I don't like the interface nearly as much as the VB 6 version but it's got more data in it.

IntelliSense can sometimes help. If you can get IntelliSense to appear, it often can give you a little description and data a type for a property, return types for functions, parameter types and descriptions, etc.

Finally there's the online help. It doesn't always say much more than IntelliSense but it's worth a try.

> Is the SerialPort "class", that you talk about in the remainder
> of this section of Appendix P, starting with 3rd paragraph,
> actually unrelated to the My.Computer.Ports namespace?

They are related. I think My.Computer.Ports.SerialPortNames is basically a shortcut to using the SerialPorts class's GetPortNames method. Remember that the My namespaces don't really add anything new, they're just shortcuts to features that are available elsewhere.

> Is it, perhaps, the SerialPort objects you can add to your
> form from the ToolBox under Components?

Yes. Also keep in mind that all components and controls are classes so you can create them at run time, too. You don't always have to add one to the form at design time, although that's often convenient.

> What is the VB 2005 equivalent of such Socket devices?

Unfortunately I've done very little of this so I don't have any good examples on my site.x

Here's "An Introduction to the .Net Socket Class" that includes some examples:

    http://www.vbcity.com/forums/faq.asp?fid=15&cat=Winsock

Here's an article called "VB.NET TCP Client - Server Socket Communications:"

    http://www.eggheadcafe.com/articles/20020323.asp

I hope that helps!


Rod
RodStephens@vb-helper.com
Author of "Visual Basic 2005 Programmer's Reference"
http://www.vb-helper.com/vb_prog_ref.htm
 
Old April 13th, 2006, 10:42 AM
Registered User
 
Join Date: Mar 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you so much for this information. The links to information and coding examples for 'Socket' devices were extremely helpful to me.

I had doubts about the following statement in your last post:

"I think My.Computer.Ports.SerialPortNames is basically a shortcut to using the SerialPorts class's GetPortNames method. Remember that the My namespaces don't really add anything new, they're just shortcuts to features that are available elsewhere."

However, I was trying to call the SerialPortNames() method for a declared (instantiated) serial port variable. That gave an error. Instead, I had to call it for the full class name to get it to work.

    sar = System.IO.Ports.SerialPort.SerialPortNames() or simply
    sar = SerialPort.SerialPortNames() if you have the first part declared with an Imports statement.

In this case it actually returns a string array of COM port names in the string array variable 'sar', just like I wanted in the first place.

I have also seen (in Microsoft's online help) calling methods with a brackted [] class name, as in:

    [classname or variable].method()

What is that all about, and where can I find the definition of brackets in VB 2005? I could not find it in your book.


Truman Byron Thomas





Similar Threads
Thread Thread Starter Forum Replies Last Post
A New Appendix B DanM BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 12 August 14th, 2010 04:53 AM
Appendix Example SQLScott BOOK: Professional WCF Programming: .NET Dev with Windows Communication Found ISBN: 9780470089842 11 January 30th, 2010 06:39 PM
Appendix A john_tempest BOOK: Professional C# 2005 with .NET 3.0 ISBN: 978-0-470-12472-7 1 August 27th, 2007 06:43 AM
Appendix B pkara2006 ASP.NET 2.0 Basics 0 August 20th, 2006 09:38 PM





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