|
|
How To Access Child Node / Collection Items In A Class - Ways of accessing the child nodes of a Tree Node | ||
Discussion by turbopowerdmaxsteel with 6 Replies.
Last Update: January 5, 2007, 8:37 am | |||
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..
I fixed the Topic title and Description for you
[/note]
Wed Jan 3, 2007 Reply New Discussion
QUOTE (turbopowerdmaxsteel)
CODE
Dim A As New TreeNode
MsgBox(A.Nodes(15).Text)
MsgBox(A.Nodes("WTF").Text)
CODE
MsgBox(Responses("ASL").Text)
Link: view Post: 95014
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.
Wed Jan 3, 2007 Reply New Discussion
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.
Wed Jan 3, 2007 Reply New Discussion
QUOTE (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.
Link: view Post: 95036
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.
Wed Jan 3, 2007 Reply New Discussion
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
Cheers,
m^e
Fri Jan 5, 2007 Reply New Discussion
QUOTE (miCRoSCoPiC)
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.
Link: view Post: 95166
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
Fri Jan 5, 2007 Reply New Discussion
Fri Jan 5, 2007 Reply New Discussion
Keyboard Buttons (28)
|
(2) Multithreading Woes
|
Index




