Nov 20, 2009

How To Access Child Node / Collection Items In A Class - Ways of accessing the child nodes of a Tree Node

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General > BASIC / Visual Basic (.NET)

How To Access Child Node / Collection Items In A Class - Ways of accessing the child nodes of a Tree Node

turbopowerdmaxsteel
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..

Notice from Mark420:

I fixed the Topic title and Description for you wink.gif

 

 

 


Comment/Reply (w/o sign-up)

faulty.lee
QUOTE(turbopowerdmaxsteel @ Jan 3 2007, 07:31 PM) *

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.

 

 

 


Comment/Reply (w/o sign-up)

turbopowerdmaxsteel
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.

Comment/Reply (w/o sign-up)

faulty.lee
QUOTE(turbopowerdmaxsteel @ Jan 4 2007, 02:28 AM) *

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.


Comment/Reply (w/o sign-up)

miCRoSCoPiC^eaRthLinG
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

Comment/Reply (w/o sign-up)

faulty.lee
QUOTE(miCRoSCoPiC^eaRthLinG @ Jan 5 2007, 01:37 PM) *

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

Comment/Reply (w/o sign-up)

turbopowerdmaxsteel
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.

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : access, child, node, ways, accessing, child, nodes, tree, node

  1. Auto-number Help In Access Db & Vb .net
    (6)
  2. VB.NET & MS Access Issue
    (7)
    Alright, I haven't had much experience with vb.net or ms access as it is, let alone using them
    together, so I need some advice on the best way to do this. I need to create a program that
    basically is a form to fill out with information, and upon filling it out it can be saved. Saving
    consists of making a row in a ms access database and placing each field as a column entry within
    this new row. Then I need to be able to retrieve this information from the DB and fill out the form
    as it was originally if the user chooses to load. This is all fine and wasn't hard to....
  3. VB.NET / MS Access Question
    (6)
    Jeez .. i can see people already starting to take up sticks and stones to beat me up .. well .. i
    know m annoying .. and this always happens when my questions are related to programming
    /biggrin.gif" style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif" /> Newayz ..
    to the point .. I have an MS Access DB .. I need a VB Code to retrieve the value from a field in
    the table. Let me just put it in a better way. I have a table that has a column: Sno. .. under
    sno. i have numbers like 1,2,3,4,5,6,7,8 .... now i created a form where i could view the rec....
  4. VB6-MS Access Question
    help please (14)
    hi guys, I am developing an application in Visual Basic 6.0 and using MS Access as my backend. What
    i want is that my database should not open when someone doublec clicks on the .mdb file. But my
    application should be able to access it. What can be a possible solution to this problem? Please
    help. Thanx in advance.....

    1. Looking for access, child, node, ways, accessing, child, nodes, tree, node

See Also,

*SIMILAR VIDEOS*
Searching Video's for access, child, node, ways, accessing, child, nodes, tree, node
advertisement



How To Access Child Node / Collection Items In A Class - Ways of accessing the child nodes of a Tree Node

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com