Welcome Guest ( Log In | Register )




                Web Hosting Guide

How To Access Child Node / Collection Items In A Class, Ways of accessing the child nodes of a Tree Node
turbopowerdmaxst...
post Jan 3 2007, 11:31 AM
Post #1


Premium Member
Group Icon

Group: [HOSTED]
Posts: 447
Joined: 16-February 06
From: Kolkata, India
Member No.: 11,322
myCENTs:81.67


CODE

            Dim A As New TreeNode
            MsgBox(A.Nodes(15).Text)
            MsgBox(A.Nodes("WTF").Text)


Here are two ways of accessing the child nodes of a Tree Node, in this case, node A.
In the first case, we have accessed the child node with Index = 15 of node A.
In the second case, we have accessed the child node "WTF" by its key.

I want to be able to access the members of the Responses class in a similar fashion. Example:-

CODE

        MsgBox(Responses("ASL").Text)



Anybody knows how to go about creating the class?

What the... I missed out on the subject of the Topic..

[note=Mark420]
I fixed the Topic title and Description for you wink.gif
[/note]
Go to the top of the page
 
+Quote Post
 
New Topic
Replies (1 - 6)
faulty.lee
post Jan 3 2007, 06:17 PM
Post #2


Super Member
Group Icon

Group: [HOSTED]
Posts: 500
Joined: 5-November 06
Member No.: 17,016
myCENTs:79.88


QUOTE(turbopowerdmaxsteel @ Jan 3 2007, 07:31 PM) [snapback]95014[/snapback]

CODE

            Dim A As New TreeNode
            MsgBox(A.Nodes(15).Text)
            MsgBox(A.Nodes("WTF").Text)


CODE

        MsgBox(Responses("ASL").Text)



I don't quite see the connection between the 2. Do you mean that you want to access the Responses as a Nodes? Are you referring to VB or ASP? It seems more like ASP to me. I think you need to give us more information regarding what you're trying to achieve.
Go to the top of the page
 
+Quote Post
turbopowerdmaxst...
post Jan 3 2007, 06:28 PM
Post #3


Premium Member
Group Icon

Group: [HOSTED]
Posts: 447
Joined: 16-February 06
From: Kolkata, India
Member No.: 11,322
myCENTs:81.67


Its in VB .NET. I used the Node just an example, the same thing can be done using the Items collection of the ListView class. The idea is to create a collection of the class Responses and be able to access the individual items by using "key as String" instead of the regular way of doing "Index as Integer".

Plainly speaking, we can access the individual elements of an array via the index number. How do we access the elements by a string "key" as we can in PHP. I don't think its possible to do the same for arrays in VB .NET, but doing that for a class is certainly. The TreeNode and the ListViewItem classes can be seen exhibiting this property.
Go to the top of the page
 
+Quote Post
faulty.lee
post Jan 3 2007, 07:00 PM
Post #4


Super Member
Group Icon

Group: [HOSTED]
Posts: 500
Joined: 5-November 06
Member No.: 17,016
myCENTs:79.88


QUOTE(turbopowerdmaxsteel @ Jan 4 2007, 02:28 AM) [snapback]95036[/snapback]

Its in VB .NET. I used the Node just an example, the same thing can be done using the Items collection of the ListView class. The idea is to create a collection of the class Responses and be able to access the individual items by using "key as String" instead of the regular way of doing "Index as Integer".

Plainly speaking, we can access the individual elements of an array via the index number. How do we access the elements by a string "key" as we can in PHP. I don't think its possible to do the same for arrays in VB .NET, but doing that for a class is certainly. The TreeNode and the ListViewItem classes can be seen exhibiting this property.


You can use Microsoft.VisualBasic.Collection. It has a built in key access for it's item. If you need to implement into your own class then you can try this

CODE

Public Class Responses
    Private _responses As Microsoft.VisualBasic.Collection

    Public Sub New()
        Me._responses = New Microsoft.VisualBasic.Collection
    End Sub

    Default Public ReadOnly Property Item(ByVal Key As String) As String
        ' You can define the default type for this property
        ' Only readonly as per default of Collection.Item
        Get
            'You need to cast it if you have turn on Strict Mode
            Return CType(Me._responses(Key), String)
        End Get
    End Property

    ' This is to ease the adding function
    ' In case you have a different type you want to store
    Public Sub Add(ByVal Value As String, ByVal Key As String)
        Me._responses.Add(Value, Key)
    End Sub
End Class


to use it

CODE

        Dim a As Responses = New Responses
        a.Add("testing", "key1")
        MsgBox(a("key1"))


You can add as many member function as you like to customize or overrides the original base function. I didn't override the Add function exactly, but you should override all of "Add" if you want to implement any one of it, that way is to avoid confusion or improperly accessing the incorrect base function.

Go to the top of the page
 
+Quote Post
miCRoSCoPiC^eaRt...
post Jan 5 2007, 05:37 AM
Post #5


PsYcheDeLiC dR3aMeR
Group Icon

Group: Admin
Posts: 2,248
Joined: 29-January 05
From: Bangkok, Thailand
Member No.: 2,411
myCENTs:19.10


You can definitely use the Microsoft.VisualBasic.Collection class as faulty.lee showed you - but a more elegant approach would be to create a separate class (inherited from CollectionBase) to hold your items and then use this class inside your Responses class.

For example - say I create a class called ResponsesCollection:
CODE

Imports System.Collections

Public Class ResponsesCollection
    Inherits CollectionBase

End Class


CollectionBase provides you with an inbuilt searchable & sortable collection called List which can be accessed by both keys and index number. Any class inheriting CollectionBase gains these properties... The members methods which can operate on the List are Add, AddRange, Remove, RemoveAt, Clear, IndexOf, Item, Contains etc. Once you inherit a class from CollectionBase, you don't need to declare the List separately... you can directly access it as List.

A simple implementation of the members would be...
CODE

Imports System.Collections

Public Class ResponsesCollection
     Inherits CollectionBase

        Public Sub Add ( value As Object )
        
            List.Add ( value )

        End Sub

        ' AddRange
        Public Sub AddRange( value[] As Object )
        
            ' Add items
            ForEach vItem As Object in value
                Add( vItem )

        End Sub

        ' Remove
        Public Sub Remove( value As Object )
        
            ' Remove item
            List.Remove( value )

        End Sub

        ' RemoveAt
        Public Sub RemoveAt( index As Integer )
        

            ' If index is out of bounds - throw exception
            If index > Count - 1 Or index < 0
                Throw New ArgumentOutOfRangeException( "Index out of bounds." )

            ' If all is fine, remove item at specified index
            List.RemoveAt( index )

        End Sub

        ' Clear
        Public Sub Clear()

            ' Clear List
            List.Clear()

        End Sub

        ' IndexOf
        Public Function IndexOf( value As Object ) As Integer
        

            Return List.IndexOf( value );

        End Function

        ' Item
        Public Function Item( index As Integer ) As Object

            ' If index is out of bounds - throw exception
            If index > Count - 1 Or index < 0
                Throw New ArgumentOutOfRangeException( "Index out of bounds." )

            Return CType( List(index), Object)
            
        End Function

        ' Insert
        Public Sub Insert( index As Integer, value As Object )

            List.Insert( index, value )

        End Sub

        ' Contains
        Public Function Contains( value As Object ) As Boolean

            Return List.Contains( value );

        End Function

End Class


Once you're done with the collection class as shown above, go over to your Responses class and implement this class by a familiar object name (say Item) inside it... make sure you instantiate it in the constructor.
CODE

Public Class Responses

    Public Item As ResponsesCollection

    ........
    ...........
    ' Constructor
    Public Sub New

    ' Other constructor code
    ......
    ' Instantiate Collection
    Item = New ResponsesCollection

    ....

    End Sub

End Class


From now on whenever you instantiate Responses class anywhere in code, you'll be able to access/modify the list by Responses.Item (x) or Responses.Item.Add (x) or Responses.Item.RemoveAt (x) etc....

Let me know if this helped smile.gif

Cheers,
m^e
Go to the top of the page
 
+Quote Post
faulty.lee
post Jan 5 2007, 06:00 AM
Post #6


Super Member
Group Icon

Group: [HOSTED]
Posts: 500
Joined: 5-November 06
Member No.: 17,016
myCENTs:79.88


QUOTE(miCRoSCoPiC^eaRthLinG @ Jan 5 2007, 01:37 PM) [snapback]95166[/snapback]

You can definitely use the Microsoft.VisualBasic.Collection class as faulty.lee showed you - but a more elegant approach would be to create a separate class (inherited from CollectionBase) to hold your items and then use this class inside your Responses class.


Hi miCRoSCoPiC^eaRthLinG,

I do agree with you, in fact i tries to avoid the VisualBasic namespace as much as possible. But looking at the situation, i think visualbasic.collection is much suited to get started. Maybe after turbopowerdmaxsteel got used to it, then he can try the actual more powerful collectionbase class.

turbopowerdmaxsteel, one thing to take note though, the starting index is different for visualbasic.collection and the collectionbase class. Visual basic one started with 1, for backward compatibility, where as collectionbase or system.collections starting with 0, similar to array

EDIT :
You can also inherits the visualbasic.collection. my last comment
QUOTE

You can add as many member function as you like to customize or overrides the original base function. I didn't override the Add function exactly, but you should override all of "Add" if you want to implement any one of it, that way is to avoid confusion or improperly accessing the incorrect base function.

actually referring to inheritance. Sorry for the mistake

Regards
faulty

This post has been edited by faulty.lee: Jan 5 2007, 06:03 AM
Go to the top of the page
 
+Quote Post
turbopowerdmaxst...
post Jan 5 2007, 08:37 AM
Post #7


Premium Member
Group Icon

Group: [HOSTED]
Posts: 447
Joined: 16-February 06
From: Kolkata, India
Member No.: 11,322
myCENTs:81.67


Woah, that's a lot of detail. Thanx for the help, guys. It'll take a while to get accustomed to all these. I'll start off with the Microsoft.VisualBasic.Collection implementation.

This post has been edited by turbopowerdmaxsteel: Jan 5 2007, 08:54 AM
Go to the top of the page
 
+Quote Post

Reply to this topicNew Topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts 0 ssp2010 0 Today, 05:23 PM
Last post by: ssp2010
No New Posts   0 magiccode9 57 1st March 2010 - 09:15 AM
Last post by: magiccode9
No new   29 tamer3kz 4,493 24th February 2010 - 05:41 AM
Last post by: iG-
No New Posts   16 sandeep 2,664 8th February 2010 - 09:19 AM
Last post by: iG-Vikram
No New Posts   5 Jeigh 3,634 3rd February 2010 - 07:16 PM
Last post by: iG-Juan
No New Posts   12 soleimanian 5,755 6th January 2010 - 11:08 AM
Last post by: iG-trlkly
No New Posts   6 turbopowerdmaxsteel 3,301 30th December 2009 - 04:10 AM
Last post by: iG-Coco
No New Posts   11 himanshurulez 5,064 29th December 2009 - 07:34 PM
Last post by: iG-
No New Posts 7 Kushika 3,652 23rd December 2009 - 06:18 AM
Last post by: iG-Mohd Ajaz Ali
No New Posts   2 kencycles 1,001 10th December 2009 - 05:05 PM
Last post by: iG-Johan
No New Posts   12 dserban 1,881 9th December 2009 - 01:19 PM
Last post by: wutske
No New Posts 6 kanade 3,007 8th December 2009 - 09:00 PM
Last post by: yordan
No New Posts 14 miCRoSCoPiC^eaRthLinG 14,951 5th December 2009 - 02:25 PM
Last post by: iG-Deepak
No New Posts   2 xhrist14n 236 27th November 2009 - 07:02 PM
Last post by: Spencer
No New Posts   3 manifest139 129 22nd November 2009 - 09:55 PM
Last post by: yordan


Web Hosting Powered by ComputingHost.com.
HONESTY ROCKS! truth rules.
Creative Commons License