Wrox Programmer Forums
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 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 September 19th, 2006, 06:29 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 180
Thanks: 1
Thanked 1 Time in 1 Post
Send a message via ICQ to dpkbahuguna Send a message via MSN to dpkbahuguna Send a message via Yahoo to dpkbahuguna
Default Class' Confusion

 Hi Forums !!!

     Can somone help me. I have to know the structure of this syntax...
     In the given syntax what are the all like..
     as i know..

      Book is the Object of class ok it is right
      ADD is the class, as i understand
      but WORKSHEET is what..
      is it Function is yes then what is the return type of this function...

     Set Book = AppXL.Workbooks
     Set Wsheet = Book.Add.Worksheets(1)

     Pls friend show me the Right way.....


     Thanks...


DPK..
__________________
DPK..
 
Old September 19th, 2006, 10:32 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Worksheet is also a class with properties and methods (subs and functions) on it, such as the Name property and the Calculate method.

In your code:

Book is an object of the Workbooks class
The line: "Set Wsheet = Book.Add.Worksheets(1)" creates a workbook using the Workbooks.Add method which returns a workbook object (which has a Worksheets object with 3 default worksheets in it at this time). However, rather than assigning this object to a reference variable, your code uses the created Worksheets property of the Workbook object to return the first Worksheet in the Worksheets collection.

So...
Book - Is a Workbooks object

Book.Add - Is a method on the Workbooks object which adds a workbook to the Workbooks collection

Book.Add.Worksheets - Is an object refering to the Worksheets collection object of the Workbook just added to the Workbooks

Book.Add.Worksheets(1) is using the default Item method of the Worksheets collection to access a Worksheet object. In this case, the first of 3 default worksheets. In other words, your code could also have been written like this:
Set Wsheet = Book.Add.Worksheets.Item(1)



Woody Z http://www.learntoprogramnow.com
 
Old September 19th, 2006, 11:39 PM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 180
Thanks: 1
Thanked 1 Time in 1 Post
Send a message via ICQ to dpkbahuguna Send a message via MSN to dpkbahuguna Send a message via Yahoo to dpkbahuguna
Default

Quote:
quote:Originally posted by woodyz
 Worksheet is also a class with properties and methods (subs and functions) on it, such as the Name property and the Calculate method.

In your code:

Book is an object of the Workbooks class
The line: "Set Wsheet = Book.Add.Worksheets(1)" creates a workbook using the Workbooks.Add method which returns a workbook object (which has a Worksheets object with 3 default worksheets in it at this time). However, rather than assigning this object to a reference variable, your code uses the created Worksheets property of the Workbook object to return the first Worksheet in the Worksheets collection.

So...
Book - Is a Workbooks object

Book.Add - Is a method on the Workbooks object which adds a workbook to the Workbooks collection

Book.Add.Worksheets - Is an object refering to the Worksheets collection object of the Workbook just added to the Workbooks

Book.Add.Worksheets(1) is using the default Item method of the Worksheets collection to access a Worksheet object. In this case, the first of 3 default worksheets. In other words, your code could also have been written like this:
Set Wsheet = Book.Add.Worksheets.Item(1)



Woody Z http://www.learntoprogramnow.com
--------------------------------------------------
Thanks woodyz !

As u said that

The line: "Set Wsheet = Book.Add.Worksheets(1)" creates a workbook using the Workbooks.Add method



so if Add is a method then how can we use the .(DOT) operator after the function name and one thing more that there is no parenthesis here too.

And as you said that

Book.Add.Worksheets - Is an object refering to the Worksheets collection object of the Workbook just added to the Workbooks


As I am getting in this line you want to say that worksheets is the class..
So if it is class then how a class can pass the parameter..
as it is passing like worksheets(1)

I am really so confused..
So pls woodyz help me here
Waiting for your reply…


DPK..
 
Old September 20th, 2006, 04:24 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:...so if Add is a method then how can we use the .(DOT) operator after the function name and one thing more that there is no parenthesis here too.
This is becuase the Add method has a return type of Workbook, which is an object with methods and properties that are accessible using the dot notation.

Quote:
quote:
Book.Add.Worksheets - Is an object refering to the Worksheets collection object of the Workbook just added to the Workbooks


As I am getting in this line you want to say that worksheets is the class..
So if it is class then how a class can pass the parameter..
as it is passing like worksheets(1)
Okay - in that line of code the Worksheets is a collection class that has a default method of "Item".
This is another way you could write the same line:

Set Wsheet = Book.Add.Worksheets.Item(1)

This should be a little clearer - the parenthesis are actually for passing the index parameter to the Item method. You can also pass in a Key, rather than the index most of the time with collections in Visual Basic. I assume it is the same here with Excel.

Restated: Worksheets is a class that is a collection of Worksheet objects. "Item" is a method of the Worksheets class that allows you to access the Worksheet items in the collection. A class can have a method marked as the Default method which is the method that will be used on that class when the programmer does not use a ".Method" notation. The purpose of this is to make it a little more natural to use some classes - in this case, it is a collection class, and perhaps it feels a little more natural to access its items with "Worksheets(1)" than with "Worksheets.Item(1)". It is up to the developer using the object to decide what they like best.

Make better sense now?

I suggest that if you are working with VB6, that you write some simple classes of your own to start testing these ideas. If you have a class with a method that returns an object of another class you will start to see how you can chain together method calls using the dot notation. Learning to write classes will speed up your understanding of how to use classes. When properly done, writing classes in your programs makes things a lot easier - it is just that at first it seems more complicated and difficult... but once you get a feel for what it is all about, you might find it simplifies the programs you write.

Further, learning to write a collection class is very useful. This is a bit more complicated, but there is lots of info on the internet on how to do this, and most of the books with this info in it are now extremely cheap to buy on half.com or other internet sites.

I hope this helps.

Woody Z http://www.learntoprogramnow.com
 
Old September 20th, 2006, 11:16 PM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 180
Thanks: 1
Thanked 1 Time in 1 Post
Send a message via ICQ to dpkbahuguna Send a message via MSN to dpkbahuguna Send a message via Yahoo to dpkbahuguna
Default

Thanks Woodyz thank you very much...!!!!!

     I have Idea about the classes, I worked in JAVA too.
     I was just confused with Default function as you told me here
     and thanks for that.
     Now I have got the concept.

Thank you very much !!!!! :)

DPK..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Errata confusion morganstein BOOK: Visual Basic 2008 Programmer's Reference ISBN: 978-0-470-18262-8 0 October 10th, 2008 05:11 PM
Confusion with PLATFORM, Help me Please! dpkbahuguna .NET Framework 2.0 10 September 28th, 2007 08:41 AM
foreach confusion stokerfed C# 2 June 13th, 2007 02:30 PM
.net Confusion reyboy General .NET 1 December 29th, 2004 08:35 AM





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