How add new Sub procedure to Items of List?-VBForums
Results 1 to 11 of 11

Thread: How add new Sub procedure to Items of List?

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2013
    Posts
    32

    Exclamation How add new Sub procedure to Items of List?

    Hi...

    How add Sub procedure to Items property of CustomClass
    example

    Code:
    Dim aa As New myList
    
    aa.Items.Add("abc")
    aa.Items(2).Xy '<<<<< Xy
    
    
    -----------------------------------
    
    Class myList
    
        Private _Items As New List(Of String)
        Property Items() As List(Of String)
            Get
                Return _Items
            End Get
            Set(ByVal value As List(Of String))
                _Items = value
            End Set
        End Property
    
    End Class
    Last edited by a1024; Jun 4th, 2014 at 03:22 AM.

  2. #2
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How add new Sub procedure to Items of List?

    Why do you want an Items property? You already have the Item property and you add things to the list by using myList.Add("abc"). The Item(index) property is the actual string and you can't subclass the string data type. If you want your collection to have other methods then just create a new subclass.
    Code:
    Public Class MyCollection
      Inherits List(Of String)
    
      Public Sub Xy(ByVal index As Integer)
        'do whatever
      End Sub
    End Class
    Now you can do this:
    Code:
    Dim myList As New MyCollection
    myList.Add("abc")
    myList.Xy(index)
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2013
    Posts
    32

    Re: How add new Sub procedure to Items of List?

    Thank you to reply...

    may not have explained my request

    i want to create Items propert as Items of ListBox but i need add procedure to item of Items property
    for example: (Me.ListBox1.Items(0).ToString) ToString procedure
    Maybe this code illustrates the idea
    Code:
    MyCalss.Items.Add("abc")
    MyCalss.Items(0).Xy ' as Me.ListBox1.Items(0).ToString 'ToString'
    Last edited by a1024; Jun 4th, 2014 at 04:13 AM.

  4. #4
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How add new Sub procedure to Items of List?

    I still don't see why you need an Items property when a List(Of T) already have the Item property. What you should do is this:
    Code:
    Public Class MyItem
      Public Sub Xy()
        'do whatever
      End Sub
      
      Public Property Value As String
    
      Public Sub New(value As String)
        Me.Value = value
      End Sub
    End Class
    And then instead of creating a List(Of String) create a List(Of MyItem)
    Code:
    Dim myList As New List(Of MyItem)
    myList.Add(New MyItem("abc"))
    myList.Item(0).Xy
    Dim x As String = myList.Item(0).Value
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2013
    Posts
    32

    Re: How add new Sub procedure to Items of List?

    Thank you...

    I need to instance from my class (donnt from List(Of.. )

    this my try but not work
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim mm As New MyClass1
    
            mm.Items.Add("aaa") ' error
    
            MsgBox(mm.Items.Count)
    
            MsgBox(mm.Items(0).Value) 
    
            mm.Items(0).Xy()
    
        End Sub
    End Class
    
    
    
    Class MyClass1
    
        Private _Items As New List(Of MyItem)
        Property Items() As List(Of MyItem)
            Get
                Return _Items
            End Get
            Set(ByVal value As List(Of MyItem))
                _Items = value
            End Set
        End Property
    End Class
    
    Class MyItem
        Public Sub Xy()
            'do whatever
            MsgBox("Xy procedure")
        End Sub
    
        Private _Value As String
        Public Property Value() As String
            Get
                Return _Value
            End Get
            Set(ByVal value As String)
                _Value = value
            End Set
        End Property
    
        Public Sub New(ByVal value As String)
            _Value = value
        End Sub
    End Class

  6. #6
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How add new Sub procedure to Items of List?

    OK you need 3 classes for that:
    Code:
    Public Class MyClass1
      Private _items As New ItemCollection
      Public ReadOnly Property Items As ItemCollection
        Get
          Return _items
        End Get
      End Property
    End Class
    
    Public Class ItemCollection
      Private _list As New List(Of MyItem)
    
      Public Sub Add(value As String)
        _list.Add(New MyItem(value))
      End Sub
    
      Default Public ReadOnly Property Item(index As Integer) As MyItem
        Get
          Return _list(index)
        End Get
      End Property
    End Class
    
    Public Class MyItem
      Public Sub Xy()
        'do whatever
        MsgBox("Xy procedure")
      End Sub
    
      Private _value As String
      Public Property Value() As String
        Get
          Return _value
        End Get
        Set(ByVal value As String)
          _value = value
        End Set
      End Property
    
      Public Sub New(ByVal value As String)
        _Value = value
      End Sub
    End Class
    This will allow you to do this:
    Code:
        Dim list As New MyClass1
        list.Items.Add("abc")
        list.Items(0).Xy()
        Dim value As String = list.Items(0).Value
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  7. #7

    Thread Starter
    Member
    Join Date
    Feb 2013
    Posts
    32

    Re: How add new Sub procedure to Items of List?

    Thank you so much to help me

    One thing remained please.... i want to know count of Items
    Code:
    Dim list As New MyClass1
    list.Items.Add("abc")
    list.Items(0).Xy()
    MsgBox(list.Items(0).Value)
    
    MsgBox(list.Items.Count) '<<<<< How i know count of Items
    Thank you agian....
    Last edited by a1024; Jun 4th, 2014 at 06:43 AM.

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2013
    Posts
    32

    Re: How add new Sub procedure to Items of List?

    Thank you ...

    I create Count procedure inside ItemCollection class
    Code:
    Public Class ItemCollection
        Private _list As New List(Of MyItem)
    
        Public Sub Add(ByVal value As String)
            _list.Add(New MyItem(value))
        End Sub
    
        Public Function Count() As Integer
            Return _list.Count
        End Function
    
        Default Public ReadOnly Property Item(ByVal index As Integer) As MyItem
            Get
                Return _list(index)
            End Get
        End Property
    End Class

  9. #9

    Thread Starter
    Member
    Join Date
    Feb 2013
    Posts
    32

    Re: How add new Sub procedure to Items of List?

    Thank you so much Joacim Andersson +100
    Last edited by a1024; Jun 4th, 2014 at 06:43 AM.

  10. #10
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How add new Sub procedure to Items of List?

    Add this property to the ItemCollection class:
    Code:
    Public ReadOnly Property Count As Integer
      Get
        Return _list.Count
      End Get
    End Property
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  11. #11

    Thread Starter
    Member
    Join Date
    Feb 2013
    Posts
    32

    Re: How add new Sub procedure to Items of List?

    Thank you so much Joacim Andersson +100

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.