+ Reply to Thread
Results 1 to 7 of 7

Creating a new Object

  1. #1
    ADG
    Guest

    Creating a new Object

    I am experimentins with the Collection Object and want to create a new object
    to hold a user defined type.
    I want to store my data in the user defined type and keep track of the sets
    of data using the collection. I have never used the Class module before, can
    anyone point me in the right direction?
    --
    Tony Green

  2. #2
    Harald Staff
    Guest

    Re: Creating a new Object

    Hi Tony

    Crashcourse:

    Create a new class module. Name it "MyClass1" (without the quotes). Paste
    this code into it:

    '************ MyClass1 code ***********
    Option Explicit

    Public LID As Long
    Public SName As String

    Public Sub SpeakMe()
    MsgBox "My name is " & SName & " and my ID is " & LID
    End Sub
    '*************** end MyClass1code ************

    Insert a standard module. Paste this code itno it:

    '************ Module 1 code: ************

    Option Explicit

    Public TheCol As Collection

    Sub InitMe()
    Dim i As Long
    Dim NewClass As MyClass1
    Set TheCol = Nothing
    Set TheCol = New Collection

    For i = 1 To 1000
    Set NewClass = New MyClass1
    NewClass.LID = i
    NewClass.SName = Chr((i Mod 26) + 65) & _
    Chr(Int(Rnd * 26) + 97)
    TheCol.Add NewClass, "C" & NewClass.LID
    Next

    End Sub

    Sub TestMe()
    Dim i As Long
    Dim ThisClass As MyClass1
    i = Val(InputBox("ID to speak:"))
    Select Case i
    Case 1 To TheCol.Count
    Set ThisClass = TheCol("C" & i)
    Call ThisClass.SpeakMe
    Set ThisClass = Nothing
    Case Else
    End Select
    End Sub

    '************ End Module 1 code: ************

    Now runf initme and then several ibstances of TestMe. I believe you will
    figure our what happens.

    This has endless possibilities, so don't give up if it's a little confuring
    the first time.

    After a while, you may replace Public LID As Long with "property let" and
    "property get" code, those are event macros running when you assign and read
    the variables. But that is the next level...

    HTH. Best wishes Harald

    "ADG" <ADG@discussions.microsoft.com> skrev i melding
    news:4BE3E558-23B2-4486-B58F-B32EB8338F5E@microsoft.com...
    > I am experimentins with the Collection Object and want to create a new

    object
    > to hold a user defined type.
    > I want to store my data in the user defined type and keep track of the

    sets
    > of data using the collection. I have never used the Class module before,

    can
    > anyone point me in the right direction?
    > --
    > Tony Green




  3. #3
    Harald Staff
    Guest

    Re: Creating a new Object

    Ouch. Apologies for all typos, this went just a little too fast. And put

    Set NewClass = Nothing
    right after
    TheCol.Add NewClass, "C" & NewClass.LID

    Best wishes Harald

    "Harald Staff" <innocent@enron.invalid> skrev i melding
    news:eY1pi5WgGHA.4976@TK2MSFTNGP02.phx.gbl...
    > Hi Tony
    >
    > Crashcourse:
    >
    > Create a new class module. Name it "MyClass1" (without the quotes). Paste
    > this code into it:
    >
    > '************ MyClass1 code ***********
    > Option Explicit
    >
    > Public LID As Long
    > Public SName As String
    >
    > Public Sub SpeakMe()
    > MsgBox "My name is " & SName & " and my ID is " & LID
    > End Sub
    > '*************** end MyClass1code ************
    >
    > Insert a standard module. Paste this code itno it:
    >
    > '************ Module 1 code: ************
    >
    > Option Explicit
    >
    > Public TheCol As Collection
    >
    > Sub InitMe()
    > Dim i As Long
    > Dim NewClass As MyClass1
    > Set TheCol = Nothing
    > Set TheCol = New Collection
    >
    > For i = 1 To 1000
    > Set NewClass = New MyClass1
    > NewClass.LID = i
    > NewClass.SName = Chr((i Mod 26) + 65) & _
    > Chr(Int(Rnd * 26) + 97)
    > TheCol.Add NewClass, "C" & NewClass.LID
    > Next
    >
    > End Sub
    >
    > Sub TestMe()
    > Dim i As Long
    > Dim ThisClass As MyClass1
    > i = Val(InputBox("ID to speak:"))
    > Select Case i
    > Case 1 To TheCol.Count
    > Set ThisClass = TheCol("C" & i)
    > Call ThisClass.SpeakMe
    > Set ThisClass = Nothing
    > Case Else
    > End Select
    > End Sub
    >
    > '************ End Module 1 code: ************
    >
    > Now runf initme and then several ibstances of TestMe. I believe you will
    > figure our what happens.
    >
    > This has endless possibilities, so don't give up if it's a little

    confuring
    > the first time.
    >
    > After a while, you may replace Public LID As Long with "property let" and
    > "property get" code, those are event macros running when you assign and

    read
    > the variables. But that is the next level...
    >
    > HTH. Best wishes Harald
    >
    > "ADG" <ADG@discussions.microsoft.com> skrev i melding
    > news:4BE3E558-23B2-4486-B58F-B32EB8338F5E@microsoft.com...
    > > I am experimentins with the Collection Object and want to create a new

    > object
    > > to hold a user defined type.
    > > I want to store my data in the user defined type and keep track of the

    > sets
    > > of data using the collection. I have never used the Class module before,

    > can
    > > anyone point me in the right direction?
    > > --
    > > Tony Green

    >
    >




  4. #4
    WhytheQ
    Guest

    Re: Creating a new Object

    Hi Harald,
    Maybe you can help?
    About a year ago I gave up on a project because I got stuck!! it
    involved class modules
    The first class was called ClsCard and is supposed to represent a
    playing card.
    The code in the class module was as below.
    Please let me know if you think the logic of my code is ok as the main
    reason for the project was to try to get into class modules.
    The problem i had was what to do with the ace, as the value can be
    either 1 or 11- how can the class module handle this??
    Any help greatly appreciated.
    Jason

    Option Explicit

    Private mCardSuit As String
    Private mCardValue As Integer
    Private mCardColour As String
    Private mCardName As String
    Private mIsPictureCard As Boolean

    Public Enum enumName
    two = 2
    Three = 3
    Four = 4
    Five = 5
    Six = 6
    Seven = 7
    Eight = 8
    Nine = 9
    Ten = 10
    Jack = 11
    Queen = 12
    King = 13
    Ace = 14
    End Enum
    Public Enum enumAces
    High = 1
    Low = 2
    Udecided = 3
    End Enum
    Public Enum enumSuitName
    Club = 1
    Diamond = 2
    Heart = 3
    Spade = 4
    End Enum

    '********CARD'S NAME PROPERTY****************
    '********************************************
    Property Let CardName(ByVal clientCardName As enumName)

    Select Case clientCardName
    Case Is = two
    mCardName = "Two"
    mCardValue = 2
    mIsPictureCard = False
    Case Is = Three
    mCardName = "Three"
    mCardValue = 3
    mIsPictureCard = False
    Case Is = Four
    mCardName = "Four"
    mCardValue = 4
    mIsPictureCard = False
    Case Is = Five
    mCardName = "Five"
    mCardValue = 5
    mIsPictureCard = False
    Case Is = Six
    mCardName = "Six"
    mCardValue = 6
    mIsPictureCard = False
    Case Is = Seven
    mCardName = "Seven"
    mCardValue = 7
    mIsPictureCard = False
    Case Is = Eight
    mCardName = "Eight"
    mCardValue = 8
    mIsPictureCard = False
    Case Is = Nine
    mCardName = "Nine"
    mCardValue = 9
    mIsPictureCard = False
    Case Is = Ten
    mCardName = "Ten"
    mCardValue = 10
    mIsPictureCard = False
    Case Is = Jack
    mCardName = "Jack"
    mCardValue = 10
    mIsPictureCard = True
    Case Is = Queen
    mCardName = "Queen"
    mCardValue = 10
    mIsPictureCard = True
    Case Is = King
    mCardName = "King"
    mCardValue = 10
    mIsPictureCard = True
    Case Is = Ace
    mCardName = "Ace"
    mCardValue = 11
    mIsPictureCard = False
    End Select

    End Property
    Property Get GetCardName() As String
    GetCardName = mCardName
    End Property
    '
    ''******CARD'S ISPICTURECARD PROPERTY:read only*****
    ''**************************************************
    Property Get IsPictureCard() As String
    IsPictureCard = mIsPictureCard
    End Property
    '
    ''***********CARD'S SUIT PROPERTY*******************
    ''**************************************************
    Property Let CardSuit(clientSuitName As enumSuitName)

    Select Case clientSuitName
    Case Is = 1
    mCardSuit = "Clubs"
    mCardColour = "Black"
    Case Is = 2
    mCardSuit = "Diamonds"
    mCardColour = "Red"
    Case Is = 3
    mCardSuit = "Hearts"
    mCardColour = "Red"
    Case Is = 4
    mCardSuit = "Spades"
    mCardColour = "Black"
    End Select

    End Property
    Property Get GetCardSuit() As String
    GetCardSuit = mCardSuit
    End Property
    '
    ''********CARD'S COLOUR PROPERTY:read only***********
    ''***************************************************
    Property Get GetCardColour() As String
    GetCardColour = mCardColour
    End Property
    '
    ''******CARD'S VALUE PROPERTY:read only**************
    ''***************************************************
    Property Get GetCardValue() As Integer
    GetCardValue = mCardValue
    End Property





    Harald Staff wrote:

    > Ouch. Apologies for all typos, this went just a little too fast. And put
    >
    > Set NewClass = Nothing
    > right after
    > TheCol.Add NewClass, "C" & NewClass.LID
    >
    > Best wishes Harald
    >
    > "Harald Staff" <innocent@enron.invalid> skrev i melding
    > news:eY1pi5WgGHA.4976@TK2MSFTNGP02.phx.gbl...
    > > Hi Tony
    > >
    > > Crashcourse:
    > >
    > > Create a new class module. Name it "MyClass1" (without the quotes). Paste
    > > this code into it:
    > >
    > > '************ MyClass1 code ***********
    > > Option Explicit
    > >
    > > Public LID As Long
    > > Public SName As String
    > >
    > > Public Sub SpeakMe()
    > > MsgBox "My name is " & SName & " and my ID is " & LID
    > > End Sub
    > > '*************** end MyClass1code ************
    > >
    > > Insert a standard module. Paste this code itno it:
    > >
    > > '************ Module 1 code: ************
    > >
    > > Option Explicit
    > >
    > > Public TheCol As Collection
    > >
    > > Sub InitMe()
    > > Dim i As Long
    > > Dim NewClass As MyClass1
    > > Set TheCol = Nothing
    > > Set TheCol = New Collection
    > >
    > > For i = 1 To 1000
    > > Set NewClass = New MyClass1
    > > NewClass.LID = i
    > > NewClass.SName = Chr((i Mod 26) + 65) & _
    > > Chr(Int(Rnd * 26) + 97)
    > > TheCol.Add NewClass, "C" & NewClass.LID
    > > Next
    > >
    > > End Sub
    > >
    > > Sub TestMe()
    > > Dim i As Long
    > > Dim ThisClass As MyClass1
    > > i = Val(InputBox("ID to speak:"))
    > > Select Case i
    > > Case 1 To TheCol.Count
    > > Set ThisClass = TheCol("C" & i)
    > > Call ThisClass.SpeakMe
    > > Set ThisClass = Nothing
    > > Case Else
    > > End Select
    > > End Sub
    > >
    > > '************ End Module 1 code: ************
    > >
    > > Now runf initme and then several ibstances of TestMe. I believe you will
    > > figure our what happens.
    > >
    > > This has endless possibilities, so don't give up if it's a little

    > confuring
    > > the first time.
    > >
    > > After a while, you may replace Public LID As Long with "property let" and
    > > "property get" code, those are event macros running when you assign and

    > read
    > > the variables. But that is the next level...
    > >
    > > HTH. Best wishes Harald
    > >
    > > "ADG" <ADG@discussions.microsoft.com> skrev i melding
    > > news:4BE3E558-23B2-4486-B58F-B32EB8338F5E@microsoft.com...
    > > > I am experimentins with the Collection Object and want to create a new

    > > object
    > > > to hold a user defined type.
    > > > I want to store my data in the user defined type and keep track of the

    > > sets
    > > > of data using the collection. I have never used the Class module before,

    > > can
    > > > anyone point me in the right direction?
    > > > --
    > > > Tony Green

    > >
    > >



  5. #5
    ADG
    Guest

    Re: Creating a new Object

    Many thanks, example was perfect. I have adapted it into a particular
    spreadsheet that I am using.

    Another novice question, is it posible to step through the collection in Key
    order, or is it only possible to use a loop to go from 1 to collection count?
    --
    Tony Green


    "Harald Staff" wrote:

    > Ouch. Apologies for all typos, this went just a little too fast. And put
    >
    > Set NewClass = Nothing
    > right after
    > TheCol.Add NewClass, "C" & NewClass.LID
    >
    > Best wishes Harald
    >
    > "Harald Staff" <innocent@enron.invalid> skrev i melding
    > news:eY1pi5WgGHA.4976@TK2MSFTNGP02.phx.gbl...
    > > Hi Tony
    > >
    > > Crashcourse:
    > >
    > > Create a new class module. Name it "MyClass1" (without the quotes). Paste
    > > this code into it:
    > >
    > > '************ MyClass1 code ***********
    > > Option Explicit
    > >
    > > Public LID As Long
    > > Public SName As String
    > >
    > > Public Sub SpeakMe()
    > > MsgBox "My name is " & SName & " and my ID is " & LID
    > > End Sub
    > > '*************** end MyClass1code ************
    > >
    > > Insert a standard module. Paste this code itno it:
    > >
    > > '************ Module 1 code: ************
    > >
    > > Option Explicit
    > >
    > > Public TheCol As Collection
    > >
    > > Sub InitMe()
    > > Dim i As Long
    > > Dim NewClass As MyClass1
    > > Set TheCol = Nothing
    > > Set TheCol = New Collection
    > >
    > > For i = 1 To 1000
    > > Set NewClass = New MyClass1
    > > NewClass.LID = i
    > > NewClass.SName = Chr((i Mod 26) + 65) & _
    > > Chr(Int(Rnd * 26) + 97)
    > > TheCol.Add NewClass, "C" & NewClass.LID
    > > Next
    > >
    > > End Sub
    > >
    > > Sub TestMe()
    > > Dim i As Long
    > > Dim ThisClass As MyClass1
    > > i = Val(InputBox("ID to speak:"))
    > > Select Case i
    > > Case 1 To TheCol.Count
    > > Set ThisClass = TheCol("C" & i)
    > > Call ThisClass.SpeakMe
    > > Set ThisClass = Nothing
    > > Case Else
    > > End Select
    > > End Sub
    > >
    > > '************ End Module 1 code: ************
    > >
    > > Now runf initme and then several ibstances of TestMe. I believe you will
    > > figure our what happens.
    > >
    > > This has endless possibilities, so don't give up if it's a little

    > confuring
    > > the first time.
    > >
    > > After a while, you may replace Public LID As Long with "property let" and
    > > "property get" code, those are event macros running when you assign and

    > read
    > > the variables. But that is the next level...
    > >
    > > HTH. Best wishes Harald
    > >
    > > "ADG" <ADG@discussions.microsoft.com> skrev i melding
    > > news:4BE3E558-23B2-4486-B58F-B32EB8338F5E@microsoft.com...
    > > > I am experimentins with the Collection Object and want to create a new

    > > object
    > > > to hold a user defined type.
    > > > I want to store my data in the user defined type and keep track of the

    > > sets
    > > > of data using the collection. I have never used the Class module before,

    > > can
    > > > anyone point me in the right direction?
    > > > --
    > > > Tony Green

    > >
    > >

    >
    >
    >


  6. #6
    Harald Staff
    Guest

    Re: Creating a new Object

    Hi Tony

    Glad it was useful.

    As long as you know what's in the collection, you can do things like

    For Each ThisClass in TheCol
    Call ThisClass.SpeakMe
    Next

    The key is a string. Keys aren't necessarily ordered, they just have to be
    unique.

    Now don't ask. Experiment.

    HTH. Best wishes Harald

    "ADG" <ADG@discussions.microsoft.com> skrev i melding
    news:A386C4F7-E16C-4443-B996-9757618B71F4@microsoft.com...
    > Many thanks, example was perfect. I have adapted it into a particular
    > spreadsheet that I am using.
    >
    > Another novice question, is it posible to step through the collection in

    Key
    > order, or is it only possible to use a loop to go from 1 to collection

    count?
    > --
    > Tony Green
    >
    >
    > "Harald Staff" wrote:
    >
    > > Ouch. Apologies for all typos, this went just a little too fast. And put
    > >
    > > Set NewClass = Nothing
    > > right after
    > > TheCol.Add NewClass, "C" & NewClass.LID
    > >
    > > Best wishes Harald
    > >
    > > "Harald Staff" <innocent@enron.invalid> skrev i melding
    > > news:eY1pi5WgGHA.4976@TK2MSFTNGP02.phx.gbl...
    > > > Hi Tony
    > > >
    > > > Crashcourse:
    > > >
    > > > Create a new class module. Name it "MyClass1" (without the quotes).

    Paste
    > > > this code into it:
    > > >
    > > > '************ MyClass1 code ***********
    > > > Option Explicit
    > > >
    > > > Public LID As Long
    > > > Public SName As String
    > > >
    > > > Public Sub SpeakMe()
    > > > MsgBox "My name is " & SName & " and my ID is " & LID
    > > > End Sub
    > > > '*************** end MyClass1code ************
    > > >
    > > > Insert a standard module. Paste this code itno it:
    > > >
    > > > '************ Module 1 code: ************
    > > >
    > > > Option Explicit
    > > >
    > > > Public TheCol As Collection
    > > >
    > > > Sub InitMe()
    > > > Dim i As Long
    > > > Dim NewClass As MyClass1
    > > > Set TheCol = Nothing
    > > > Set TheCol = New Collection
    > > >
    > > > For i = 1 To 1000
    > > > Set NewClass = New MyClass1
    > > > NewClass.LID = i
    > > > NewClass.SName = Chr((i Mod 26) + 65) & _
    > > > Chr(Int(Rnd * 26) + 97)
    > > > TheCol.Add NewClass, "C" & NewClass.LID
    > > > Next
    > > >
    > > > End Sub
    > > >
    > > > Sub TestMe()
    > > > Dim i As Long
    > > > Dim ThisClass As MyClass1
    > > > i = Val(InputBox("ID to speak:"))
    > > > Select Case i
    > > > Case 1 To TheCol.Count
    > > > Set ThisClass = TheCol("C" & i)
    > > > Call ThisClass.SpeakMe
    > > > Set ThisClass = Nothing
    > > > Case Else
    > > > End Select
    > > > End Sub
    > > >
    > > > '************ End Module 1 code: ************
    > > >
    > > > Now runf initme and then several ibstances of TestMe. I believe you

    will
    > > > figure our what happens.
    > > >
    > > > This has endless possibilities, so don't give up if it's a little

    > > confuring
    > > > the first time.
    > > >
    > > > After a while, you may replace Public LID As Long with "property let"

    and
    > > > "property get" code, those are event macros running when you assign

    and
    > > read
    > > > the variables. But that is the next level...
    > > >
    > > > HTH. Best wishes Harald
    > > >
    > > > "ADG" <ADG@discussions.microsoft.com> skrev i melding
    > > > news:4BE3E558-23B2-4486-B58F-B32EB8338F5E@microsoft.com...
    > > > > I am experimentins with the Collection Object and want to create a

    new
    > > > object
    > > > > to hold a user defined type.
    > > > > I want to store my data in the user defined type and keep track of

    the
    > > > sets
    > > > > of data using the collection. I have never used the Class module

    before,
    > > > can
    > > > > anyone point me in the right direction?
    > > > > --
    > > > > Tony Green
    > > >
    > > >

    > >
    > >
    > >




  7. #7
    Harald Staff
    Guest

    Re: Creating a new Object

    Hi Jason

    I would say that a card is a card and an ace is an ace. If some logic (like
    a card game rule) gives an ace two meanings, depenging on this or that
    (usually what returns the most valuable hand) then this decision (value 1 or
    14) should be dealt with by the logic, the game, not by the card itself. So
    leave the class as is, give the ace a single value and let your
    poker/blackjack/whatever program decide how to use it.

    My 0.02 only, but nobody else has replied so far :-)
    Best wishes Harald

    "WhytheQ" <WhytheQ@gmail.com> skrev i melding
    news:1148733799.734409.36360@y43g2000cwc.googlegroups.com...
    > Hi Harald,
    > Maybe you can help?
    > About a year ago I gave up on a project because I got stuck!! it
    > involved class modules
    > The first class was called ClsCard and is supposed to represent a
    > playing card.
    > The code in the class module was as below.
    > Please let me know if you think the logic of my code is ok as the main
    > reason for the project was to try to get into class modules.
    > The problem i had was what to do with the ace, as the value can be
    > either 1 or 11- how can the class module handle this??
    > Any help greatly appreciated.
    > Jason




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1