PC Review


Reply
Thread Tools Rate Thread

Creating working with Class

 
 
JMay
Guest
Posts: n/a
 
      15th Nov 2008
I have in Module1

Global MyGuys()

Public Type Person
strFName As String
strLName As String
strAddress As String
strCity As String
strState As String
strZip As String
End Type

Public Sub LoadGuys()
Dim MyGuys(3) As Person
For i = 0 To 3
MyGuys(i).strFName = Cells(i + 5, 1)
MyGuys(i).strLName = Cells(i + 5, 2)
MyGuys(i).strAddress = Cells(i + 5, 3)
MyGuys(i).strCity = Cells(i + 5, 4)
MyGuys(i).strState = Cells(i + 5, 5)
MyGuys(i).strZip = Cells(i + 5, 6)
Next i
End Sub

=======================

After I run Loadguys Macro (which loads variables from my ws

in the immediate window I enter:

? MyGuys(2).strAddress

But get r/t 9 - Subscript out of range..
Can someone tell me why? What am I omitting not
to be able to bring up mytext

TIA,
JIM
 
Reply With Quote
 
 
 
 
Andy Pope
Guest
Posts: n/a
 
      15th Nov 2008
Hi,

The problem is scope.
Although you declared MyGuys as global you also declared an array with
the same name in the procedure. So the code would run and if you used
the command in the immediate window whilst still in that procedure you
would have seen correct value.

try this revision.
'--------------------
Global MyGuys() As Person

Public Type Person
strFName As String
strLName As String
strAddress As String
strCity As String
strState As String
strZip As String
End Type

Public Sub LoadGuys()
ReDim MyGuys(3) As Person

For i = 0 To 3
MyGuys(i).strFName = Cells(i + 5, 1)
MyGuys(i).strLName = Cells(i + 5, 2)
MyGuys(i).strAddress = Cells(i + 5, 3)
MyGuys(i).strCity = Cells(i + 5, 4)
MyGuys(i).strState = Cells(i + 5, 5)
MyGuys(i).strZip = Cells(i + 5, 6)
Next i

End Sub
'--------------------

Not sure what the class connection is as this all appeared to be in a
standard code module.

Cheers
Andy

JMay wrote:
> I have in Module1
>
> Global MyGuys()
>
> Public Type Person
> strFName As String
> strLName As String
> strAddress As String
> strCity As String
> strState As String
> strZip As String
> End Type
>
> Public Sub LoadGuys()
> Dim MyGuys(3) As Person
> For i = 0 To 3
> MyGuys(i).strFName = Cells(i + 5, 1)
> MyGuys(i).strLName = Cells(i + 5, 2)
> MyGuys(i).strAddress = Cells(i + 5, 3)
> MyGuys(i).strCity = Cells(i + 5, 4)
> MyGuys(i).strState = Cells(i + 5, 5)
> MyGuys(i).strZip = Cells(i + 5, 6)
> Next i
> End Sub
>
> =======================
>
> After I run Loadguys Macro (which loads variables from my ws
>
> in the immediate window I enter:
>
> ? MyGuys(2).strAddress
>
> But get r/t 9 - Subscript out of range..
> Can someone tell me why? What am I omitting not
> to be able to bring up mytext
>
> TIA,
> JIM


--

Andy Pope, Microsoft MVP - Excel
http://www.andypope.info
 
Reply With Quote
 
JMay
Guest
Posts: n/a
 
      15th Nov 2008
Thanks Andy,

This is as far as I've EVER gotten into user/defined classes, arrays, etc....
ooooooowwwww!!
Hope to use this as building block to further my ed
Jim

"Andy Pope" wrote:

> Hi,
>
> The problem is scope.
> Although you declared MyGuys as global you also declared an array with
> the same name in the procedure. So the code would run and if you used
> the command in the immediate window whilst still in that procedure you
> would have seen correct value.
>
> try this revision.
> '--------------------
> Global MyGuys() As Person
>
> Public Type Person
> strFName As String
> strLName As String
> strAddress As String
> strCity As String
> strState As String
> strZip As String
> End Type
>
> Public Sub LoadGuys()
> ReDim MyGuys(3) As Person
>
> For i = 0 To 3
> MyGuys(i).strFName = Cells(i + 5, 1)
> MyGuys(i).strLName = Cells(i + 5, 2)
> MyGuys(i).strAddress = Cells(i + 5, 3)
> MyGuys(i).strCity = Cells(i + 5, 4)
> MyGuys(i).strState = Cells(i + 5, 5)
> MyGuys(i).strZip = Cells(i + 5, 6)
> Next i
>
> End Sub
> '--------------------
>
> Not sure what the class connection is as this all appeared to be in a
> standard code module.
>
> Cheers
> Andy
>
> JMay wrote:
> > I have in Module1
> >
> > Global MyGuys()
> >
> > Public Type Person
> > strFName As String
> > strLName As String
> > strAddress As String
> > strCity As String
> > strState As String
> > strZip As String
> > End Type
> >
> > Public Sub LoadGuys()
> > Dim MyGuys(3) As Person
> > For i = 0 To 3
> > MyGuys(i).strFName = Cells(i + 5, 1)
> > MyGuys(i).strLName = Cells(i + 5, 2)
> > MyGuys(i).strAddress = Cells(i + 5, 3)
> > MyGuys(i).strCity = Cells(i + 5, 4)
> > MyGuys(i).strState = Cells(i + 5, 5)
> > MyGuys(i).strZip = Cells(i + 5, 6)
> > Next i
> > End Sub
> >
> > =======================
> >
> > After I run Loadguys Macro (which loads variables from my ws
> >
> > in the immediate window I enter:
> >
> > ? MyGuys(2).strAddress
> >
> > But get r/t 9 - Subscript out of range..
> > Can someone tell me why? What am I omitting not
> > to be able to bring up mytext
> >
> > TIA,
> > JIM

>
> --
>
> Andy Pope, Microsoft MVP - Excel
> http://www.andypope.info
>

 
Reply With Quote
 
JMay
Guest
Posts: n/a
 
      15th Nov 2008
Andy, Thought of just one-more Q;
Over the years in the VBE - I've inserted only User-Forms OR Modules
(Standard).
In my crude-simple example - would or could it be realed to using the one
option I've never used -- the (insert) Class Module option?

"JMay" wrote:

> Thanks Andy,
>
> This is as far as I've EVER gotten into user/defined classes, arrays, etc....
> ooooooowwwww!!
> Hope to use this as building block to further my ed
> Jim
>
> "Andy Pope" wrote:
>
> > Hi,
> >
> > The problem is scope.
> > Although you declared MyGuys as global you also declared an array with
> > the same name in the procedure. So the code would run and if you used
> > the command in the immediate window whilst still in that procedure you
> > would have seen correct value.
> >
> > try this revision.
> > '--------------------
> > Global MyGuys() As Person
> >
> > Public Type Person
> > strFName As String
> > strLName As String
> > strAddress As String
> > strCity As String
> > strState As String
> > strZip As String
> > End Type
> >
> > Public Sub LoadGuys()
> > ReDim MyGuys(3) As Person
> >
> > For i = 0 To 3
> > MyGuys(i).strFName = Cells(i + 5, 1)
> > MyGuys(i).strLName = Cells(i + 5, 2)
> > MyGuys(i).strAddress = Cells(i + 5, 3)
> > MyGuys(i).strCity = Cells(i + 5, 4)
> > MyGuys(i).strState = Cells(i + 5, 5)
> > MyGuys(i).strZip = Cells(i + 5, 6)
> > Next i
> >
> > End Sub
> > '--------------------
> >
> > Not sure what the class connection is as this all appeared to be in a
> > standard code module.
> >
> > Cheers
> > Andy
> >
> > JMay wrote:
> > > I have in Module1
> > >
> > > Global MyGuys()
> > >
> > > Public Type Person
> > > strFName As String
> > > strLName As String
> > > strAddress As String
> > > strCity As String
> > > strState As String
> > > strZip As String
> > > End Type
> > >
> > > Public Sub LoadGuys()
> > > Dim MyGuys(3) As Person
> > > For i = 0 To 3
> > > MyGuys(i).strFName = Cells(i + 5, 1)
> > > MyGuys(i).strLName = Cells(i + 5, 2)
> > > MyGuys(i).strAddress = Cells(i + 5, 3)
> > > MyGuys(i).strCity = Cells(i + 5, 4)
> > > MyGuys(i).strState = Cells(i + 5, 5)
> > > MyGuys(i).strZip = Cells(i + 5, 6)
> > > Next i
> > > End Sub
> > >
> > > =======================
> > >
> > > After I run Loadguys Macro (which loads variables from my ws
> > >
> > > in the immediate window I enter:
> > >
> > > ? MyGuys(2).strAddress
> > >
> > > But get r/t 9 - Subscript out of range..
> > > Can someone tell me why? What am I omitting not
> > > to be able to bring up mytext
> > >
> > > TIA,
> > > JIM

> >
> > --
> >
> > Andy Pope, Microsoft MVP - Excel
> > http://www.andypope.info
> >

 
Reply With Quote
 
Andy Pope
Guest
Posts: n/a
 
      15th Nov 2008
Here is a very quick working of you Type into a class.
In the class I used public variables for all but ZIP, which demonstrates
the Let/Get properties.

class code module named CPerson.
'-------------
Option Explicit

Public FirstName As String
Public LastName As String
Public Address As String
Public City As String
Public State As String
Private m_strZip As String
Public Property Let Zip(RHS As String)
m_strZip = RHS
End Property

Public Property Get Zip() As String
Zip = m_strZip
End Property
'-------------

Standard code module
'-------------
Option Explicit

Public m_colMyGuys As Collection

Public Sub LoadGuys()
Dim i
Dim clsPerson As CPerson

Set m_colMyGuys = New Collection
For i = 0 To 3
Set clsPerson = New CPerson

With clsPerson
.FirstName = Cells(i + 5, 1)
.LastName = Cells(i + 5, 2)
.Address = Cells(i + 5, 3)
.City = Cells(i + 5, 4)
.State = Cells(i + 5, 5)
.Zip = Cells(i + 5, 6)
End With

m_colMyGuys.Add clsPerson, CStr(m_colMyGuys.Count + 1)
Next i

End Sub

Sub DisplayTest()
' just to show usage of collection and class
Dim clsItem As CPerson

For Each clsItem In m_colMyGuys
Debug.Print clsItem.FirstName, clsItem.LastName, clsItem.Zip
Next

End Sub
'-------------

Personally I would create another class to handle the tasks currently
done by the collection but that is complicated for a 1st look see into
classes

Cheers
Andy



JMay wrote:
> Andy, Thought of just one-more Q;
> Over the years in the VBE - I've inserted only User-Forms OR Modules
> (Standard).
> In my crude-simple example - would or could it be realed to using the one
> option I've never used -- the (insert) Class Module option?
>
> "JMay" wrote:
>
>
>>Thanks Andy,
>>
>>This is as far as I've EVER gotten into user/defined classes, arrays, etc....
>>ooooooowwwww!!
>>Hope to use this as building block to further my ed
>>Jim
>>
>>"Andy Pope" wrote:
>>
>>
>>>Hi,
>>>
>>>The problem is scope.
>>>Although you declared MyGuys as global you also declared an array with
>>>the same name in the procedure. So the code would run and if you used
>>>the command in the immediate window whilst still in that procedure you
>>>would have seen correct value.
>>>
>>>try this revision.
>>>'--------------------
>>>Global MyGuys() As Person
>>>
>>>Public Type Person
>>> strFName As String
>>> strLName As String
>>> strAddress As String
>>> strCity As String
>>> strState As String
>>> strZip As String
>>>End Type
>>>
>>>Public Sub LoadGuys()
>>>ReDim MyGuys(3) As Person
>>>
>>>For i = 0 To 3
>>>MyGuys(i).strFName = Cells(i + 5, 1)
>>>MyGuys(i).strLName = Cells(i + 5, 2)
>>>MyGuys(i).strAddress = Cells(i + 5, 3)
>>>MyGuys(i).strCity = Cells(i + 5, 4)
>>>MyGuys(i).strState = Cells(i + 5, 5)
>>>MyGuys(i).strZip = Cells(i + 5, 6)
>>>Next i
>>>
>>>End Sub
>>>'--------------------
>>>
>>>Not sure what the class connection is as this all appeared to be in a
>>>standard code module.
>>>
>>>Cheers
>>>Andy
>>>
>>>JMay wrote:
>>>
>>>>I have in Module1
>>>>
>>>>Global MyGuys()
>>>>
>>>>Public Type Person
>>>> strFName As String
>>>> strLName As String
>>>> strAddress As String
>>>> strCity As String
>>>> strState As String
>>>> strZip As String
>>>>End Type
>>>>
>>>>Public Sub LoadGuys()
>>>>Dim MyGuys(3) As Person
>>>>For i = 0 To 3
>>>>MyGuys(i).strFName = Cells(i + 5, 1)
>>>>MyGuys(i).strLName = Cells(i + 5, 2)
>>>>MyGuys(i).strAddress = Cells(i + 5, 3)
>>>>MyGuys(i).strCity = Cells(i + 5, 4)
>>>>MyGuys(i).strState = Cells(i + 5, 5)
>>>>MyGuys(i).strZip = Cells(i + 5, 6)
>>>>Next i
>>>>End Sub
>>>>
>>>>=======================
>>>>
>>>>After I run Loadguys Macro (which loads variables from my ws
>>>>
>>>>in the immediate window I enter:
>>>>
>>>>? MyGuys(2).strAddress
>>>>
>>>>But get r/t 9 - Subscript out of range..
>>>>Can someone tell me why? What am I omitting not
>>>>to be able to bring up mytext
>>>>
>>>>TIA,
>>>>JIM
>>>
>>>--
>>>
>>>Andy Pope, Microsoft MVP - Excel
>>>http://www.andypope.info
>>>


--

Andy Pope, Microsoft MVP - Excel
http://www.andypope.info
 
Reply With Quote
 
JMay
Guest
Posts: n/a
 
      15th Nov 2008
Again, Thank you VERY much..
Jim

"Andy Pope" wrote:

> Here is a very quick working of you Type into a class.
> In the class I used public variables for all but ZIP, which demonstrates
> the Let/Get properties.
>
> class code module named CPerson.
> '-------------
> Option Explicit
>
> Public FirstName As String
> Public LastName As String
> Public Address As String
> Public City As String
> Public State As String
> Private m_strZip As String
> Public Property Let Zip(RHS As String)
> m_strZip = RHS
> End Property
>
> Public Property Get Zip() As String
> Zip = m_strZip
> End Property
> '-------------
>
> Standard code module
> '-------------
> Option Explicit
>
> Public m_colMyGuys As Collection
>
> Public Sub LoadGuys()
> Dim i
> Dim clsPerson As CPerson
>
> Set m_colMyGuys = New Collection
> For i = 0 To 3
> Set clsPerson = New CPerson
>
> With clsPerson
> .FirstName = Cells(i + 5, 1)
> .LastName = Cells(i + 5, 2)
> .Address = Cells(i + 5, 3)
> .City = Cells(i + 5, 4)
> .State = Cells(i + 5, 5)
> .Zip = Cells(i + 5, 6)
> End With
>
> m_colMyGuys.Add clsPerson, CStr(m_colMyGuys.Count + 1)
> Next i
>
> End Sub
>
> Sub DisplayTest()
> ' just to show usage of collection and class
> Dim clsItem As CPerson
>
> For Each clsItem In m_colMyGuys
> Debug.Print clsItem.FirstName, clsItem.LastName, clsItem.Zip
> Next
>
> End Sub
> '-------------
>
> Personally I would create another class to handle the tasks currently
> done by the collection but that is complicated for a 1st look see into
> classes
>
> Cheers
> Andy
>
>
>
> JMay wrote:
> > Andy, Thought of just one-more Q;
> > Over the years in the VBE - I've inserted only User-Forms OR Modules
> > (Standard).
> > In my crude-simple example - would or could it be realed to using the one
> > option I've never used -- the (insert) Class Module option?
> >
> > "JMay" wrote:
> >
> >
> >>Thanks Andy,
> >>
> >>This is as far as I've EVER gotten into user/defined classes, arrays, etc....
> >>ooooooowwwww!!
> >>Hope to use this as building block to further my ed
> >>Jim
> >>
> >>"Andy Pope" wrote:
> >>
> >>
> >>>Hi,
> >>>
> >>>The problem is scope.
> >>>Although you declared MyGuys as global you also declared an array with
> >>>the same name in the procedure. So the code would run and if you used
> >>>the command in the immediate window whilst still in that procedure you
> >>>would have seen correct value.
> >>>
> >>>try this revision.
> >>>'--------------------
> >>>Global MyGuys() As Person
> >>>
> >>>Public Type Person
> >>> strFName As String
> >>> strLName As String
> >>> strAddress As String
> >>> strCity As String
> >>> strState As String
> >>> strZip As String
> >>>End Type
> >>>
> >>>Public Sub LoadGuys()
> >>>ReDim MyGuys(3) As Person
> >>>
> >>>For i = 0 To 3
> >>>MyGuys(i).strFName = Cells(i + 5, 1)
> >>>MyGuys(i).strLName = Cells(i + 5, 2)
> >>>MyGuys(i).strAddress = Cells(i + 5, 3)
> >>>MyGuys(i).strCity = Cells(i + 5, 4)
> >>>MyGuys(i).strState = Cells(i + 5, 5)
> >>>MyGuys(i).strZip = Cells(i + 5, 6)
> >>>Next i
> >>>
> >>>End Sub
> >>>'--------------------
> >>>
> >>>Not sure what the class connection is as this all appeared to be in a
> >>>standard code module.
> >>>
> >>>Cheers
> >>>Andy
> >>>
> >>>JMay wrote:
> >>>
> >>>>I have in Module1
> >>>>
> >>>>Global MyGuys()
> >>>>
> >>>>Public Type Person
> >>>> strFName As String
> >>>> strLName As String
> >>>> strAddress As String
> >>>> strCity As String
> >>>> strState As String
> >>>> strZip As String
> >>>>End Type
> >>>>
> >>>>Public Sub LoadGuys()
> >>>>Dim MyGuys(3) As Person
> >>>>For i = 0 To 3
> >>>>MyGuys(i).strFName = Cells(i + 5, 1)
> >>>>MyGuys(i).strLName = Cells(i + 5, 2)
> >>>>MyGuys(i).strAddress = Cells(i + 5, 3)
> >>>>MyGuys(i).strCity = Cells(i + 5, 4)
> >>>>MyGuys(i).strState = Cells(i + 5, 5)
> >>>>MyGuys(i).strZip = Cells(i + 5, 6)
> >>>>Next i
> >>>>End Sub
> >>>>
> >>>>=======================
> >>>>
> >>>>After I run Loadguys Macro (which loads variables from my ws
> >>>>
> >>>>in the immediate window I enter:
> >>>>
> >>>>? MyGuys(2).strAddress
> >>>>
> >>>>But get r/t 9 - Subscript out of range..
> >>>>Can someone tell me why? What am I omitting not
> >>>>to be able to bring up mytext
> >>>>
> >>>>TIA,
> >>>>JIM
> >>>
> >>>--
> >>>
> >>>Andy Pope, Microsoft MVP - Excel
> >>>http://www.andypope.info
> >>>

>
> --
>
> Andy Pope, Microsoft MVP - Excel
> http://www.andypope.info
>

 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      15th Nov 2008
I have what I think is a rather good introduction to classes at
http://www.cpearson.com/excel/Classes.aspx

It should get you started down the right path.


Cordially,
Chip Pearson
Microsoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)



On Sat, 15 Nov 2008 07:36:01 -0800, JMay
<(E-Mail Removed)> wrote:

>Andy, Thought of just one-more Q;
>Over the years in the VBE - I've inserted only User-Forms OR Modules
>(Standard).
>In my crude-simple example - would or could it be realed to using the one
>option I've never used -- the (insert) Class Module option?
>
>"JMay" wrote:
>
>> Thanks Andy,
>>
>> This is as far as I've EVER gotten into user/defined classes, arrays, etc....
>> ooooooowwwww!!
>> Hope to use this as building block to further my ed
>> Jim
>>
>> "Andy Pope" wrote:
>>
>> > Hi,
>> >
>> > The problem is scope.
>> > Although you declared MyGuys as global you also declared an array with
>> > the same name in the procedure. So the code would run and if you used
>> > the command in the immediate window whilst still in that procedure you
>> > would have seen correct value.
>> >
>> > try this revision.
>> > '--------------------
>> > Global MyGuys() As Person
>> >
>> > Public Type Person
>> > strFName As String
>> > strLName As String
>> > strAddress As String
>> > strCity As String
>> > strState As String
>> > strZip As String
>> > End Type
>> >
>> > Public Sub LoadGuys()
>> > ReDim MyGuys(3) As Person
>> >
>> > For i = 0 To 3
>> > MyGuys(i).strFName = Cells(i + 5, 1)
>> > MyGuys(i).strLName = Cells(i + 5, 2)
>> > MyGuys(i).strAddress = Cells(i + 5, 3)
>> > MyGuys(i).strCity = Cells(i + 5, 4)
>> > MyGuys(i).strState = Cells(i + 5, 5)
>> > MyGuys(i).strZip = Cells(i + 5, 6)
>> > Next i
>> >
>> > End Sub
>> > '--------------------
>> >
>> > Not sure what the class connection is as this all appeared to be in a
>> > standard code module.
>> >
>> > Cheers
>> > Andy
>> >
>> > JMay wrote:
>> > > I have in Module1
>> > >
>> > > Global MyGuys()
>> > >
>> > > Public Type Person
>> > > strFName As String
>> > > strLName As String
>> > > strAddress As String
>> > > strCity As String
>> > > strState As String
>> > > strZip As String
>> > > End Type
>> > >
>> > > Public Sub LoadGuys()
>> > > Dim MyGuys(3) As Person
>> > > For i = 0 To 3
>> > > MyGuys(i).strFName = Cells(i + 5, 1)
>> > > MyGuys(i).strLName = Cells(i + 5, 2)
>> > > MyGuys(i).strAddress = Cells(i + 5, 3)
>> > > MyGuys(i).strCity = Cells(i + 5, 4)
>> > > MyGuys(i).strState = Cells(i + 5, 5)
>> > > MyGuys(i).strZip = Cells(i + 5, 6)
>> > > Next i
>> > > End Sub
>> > >
>> > > =======================
>> > >
>> > > After I run Loadguys Macro (which loads variables from my ws
>> > >
>> > > in the immediate window I enter:
>> > >
>> > > ? MyGuys(2).strAddress
>> > >
>> > > But get r/t 9 - Subscript out of range..
>> > > Can someone tell me why? What am I omitting not
>> > > to be able to bring up mytext
>> > >
>> > > TIA,
>> > > JIM
>> >
>> > --
>> >
>> > Andy Pope, Microsoft MVP - Excel
>> > http://www.andypope.info
>> >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
creating a simple class, a cat class? Ron Microsoft VB .NET 5 13th Apr 2007 03:19 AM
ImageList in base class not working correctly in inherited class =?Utf-8?B?QnJ1Y2UgUGFya2Vy?= Microsoft Dot NET Framework Forms 3 16th Dec 2005 02:20 AM
creating class TS Microsoft C# .NET 4 14th Jul 2005 08:42 AM
Help with Creating a Class Paul Say Microsoft ASP .NET 2 22nd Mar 2004 10:49 PM
Re: Creating COM out of a .NET class Ed Kaim [MSFT] Microsoft Dot NET Framework 0 19th Feb 2004 09:13 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:54 PM.