Results 1 to 6 of 6

Thread: [2005] Class Setup

  1. #1

    Thread Starter
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    [2005] Class Setup

    This is something I have been wondering and not sure how they accomplish this but I want to try to mimic its behavior if possible in a class I am writting.

    I don't know what its called or how to explain it so I will try to show by example.

    Say I have a list box on a form and to add an item into the list box you type.
    VB Code:
    1. ListBox1.Items.Add(item)

    Items is a property of the ListBox1 which makes sense but then how do you add the extra function after the items. Or how do I add the .Add function to that items property?

    For example I have a class that holds Address Information so I have a property named Name and I want after the name and add function like above to add the name.
    VB Code:
    1. Address1.Names.Add(name)

    Is that possible?

    Sorry its kinda of confusing to explain but I have always been interested of how thats done.

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,383

    Re: [2005] Class Setup

    items is a collection object (specifically a System.Windows.Forms.ListBox.ObjectCollection)

    it is a property of the listbox, but it also is its own class in itself... which is why it has methods and properties...

  3. #3
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: [2005] Class Setup

    What you are asking (I think) is how to make a collection. There are a few ways to do it, but I am going to talk about one. In 05 vb got a new feature called generics. A generic collection allows you to create a collection of basicly any type of object you want. Previously you had to make an individual collection for each object.

    So a generic collection is a class that inherits the CollectionBase class. From there you override the functionality it has like add and remove, and you cam also make your own functionality as well.

    Here is a link to show you how to get started with generic collections.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  4. #4
    Addicted Member
    Join Date
    Mar 2006
    Posts
    235

    Question Re: [2005] Class Setup

    I'm not really sure what your asking.

    I was just reading this article http://www.devx.com/codemag/Article/31188/0/page/1 and remember your post.

  5. #5
    Member
    Join Date
    Jan 2006
    Posts
    39

    Re: [2005] Class Setup

    jumpercables,

    I am currently working on something similar, and have hit some pitfalls. Specifically, I can't access indexed items in the collection. If you figure it out, please post back what you know.

    VB Code:
    1. Public Class Motor
    2.     Private mcolInputs As Inputs
    3.  
    4.     Public ReadOnly Property Inputs() As Inputs
    5.         Get
    6.             Inputs = mcolInputs
    7.         End Get
    8.     End Property
    9.     Public Sub New()
    10.         mcolInputs = New Inputs
    11.     End Sub
    12. End Class
    13.  
    14. Public Class Inputs
    15.     Private mcolInputs As Collection
    16.  
    17.     Public Sub New()
    18.         mcolInputs = New Collection
    19.     End Sub
    20.     Public Function Add(ByVal lngNumber As Long, _
    21.         ByVal strDescription As String, _
    22.         Optional ByVal strKey As String = "") As MotorInput
    23.  
    24.         Dim instMotorInput As New MotorInput
    25.         With instMotorInput
    26.             .Number = lngNumber
    27.             .Description = strDescription
    28.         End With
    29.         If strKey = "" Then
    30.             mcolInputs.Add(instMotorInput)
    31.         Else
    32.             mcolInputs.Add(instMotorInput, strKey)
    33.         End If
    34.         Add = instMotorInput
    35.     End Function
    36.     Public Sub Remove(ByVal strKey As String)
    37.         mcolInputs.Remove(strKey)
    38.     End Sub
    39.     Public Sub Remove(ByVal lngIndex As Long)
    40.         mcolInputs.Remove(CType(lngIndex, Integer))
    41.     End Sub
    42.     Public Function Count() As Long
    43.         Count = mcolInputs.Count
    44.     End Function
    45. End Class
    46.  
    47. Public Class MotorInput
    48.     Private mlngNumber As Long
    49.     Private mstrDescription As String
    50.     Private mboolStatus As Boolean
    51.  
    52.     Public Property Number() As Long
    53.         Get
    54.             Number = mlngNumber
    55.         End Get
    56.         Set(ByVal value As Long)
    57.             mlngNumber = value
    58.         End Set
    59.     End Property
    60.     Public Property Description() As String
    61.         Get
    62.             Description = mstrDescription
    63.         End Get
    64.         Set(ByVal value As String)
    65.             mstrDescription = value
    66.         End Set
    67.     End Property
    68.     Public ReadOnly Property Status() As Boolean
    69.         Get
    70.             Status = mboolStatus
    71.         End Get
    72.     End Property
    73.  
    74. End Class

  6. #6
    Member
    Join Date
    Jan 2006
    Posts
    39

    Re: [2005] Class Setup

    Alright. So I added this block of code to Public Class Inputs in my previous post that allows me to access specific items in the collection.
    VB Code:
    1. Public Function Item(ByVal lngIndex As Long) As MotorInput
    2.         Item = mcolInputs.Item(CType(lngIndex, Integer))
    3.     End Function
    4.     Public Function Item(ByVal strKey As String) As MotorInput
    5.         Item = mcolInputs.Item(strKey)
    6.     End Function

    Is this what you were trying to do?

Posting Permissions

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



Click Here to Expand Forum to Full Width