convert array structure to dictionary in vb.net

Hi,
This code snippet I found online allows me to index a class like I would an array, like x(5,4).  I need the same functionality except for using a dictionary instead of an array.  Can someone show me how to convert this class so that I can index it like x("a","b") (preferably  without the size constraints)? Thanks!
Public Class Updates
    Inherits System.Collections.CollectionBase
    Private m_Item(7, 7) As Integer

    Default Public Overloads Property Item(ByVal RowIndex As Int32, ByVal ColIndex As Int32) As Integer
        Get
            If (RowIndex < 0) Or (RowIndex > UBound(m_Item, 1)) Then Throw New IndexOutOfRangeException("RowIndex out of bounds")
            If (ColIndex < 0) Or (ColIndex > UBound(m_Item, 2)) Then Throw New IndexOutOfRangeException("ColIndex out of bounds")

            Return CType(m_Item(RowIndex, ColIndex), Integer)
        End Get
        Set(ByVal value As Integer)
            If (RowIndex < 0) Or (RowIndex > UBound(m_Item, 1)) Then Throw New IndexOutOfRangeException("RowIndex out of bounds")
            If (ColIndex < 0) Or (ColIndex > UBound(m_Item, 2)) Then Throw New IndexOutOfRangeException("ColIndex out of bounds")
            m_Item(RowIndex, ColIndex) = value
        End Set
    End Property
End Class

Open in new window

LVL 11
ugebAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
CodeCruiserConnect With a Mentor Commented:
>What I want is the above code adapted for a dictionary

And the two possibilities I mentioned are exactly for that. I won't be able to do it for you but thought I would try exploring the possibilities in case that provides you any pointers.
0
 
CodeCruiserCommented:
Do you want to use the dictionary in the same way? Dictionary is a key value pair store so it is only single dimension unlike the 2 dimension array you are currently using.
0
 
ugebAuthor Commented:
I am wanting to use the class as a 2-dimensional dictionary with easier access for reassigning values.  If I have Dictionary(Of String, Dictionary(Of String, MyStructType)), then it becomes very cumbersome to access MyStructType to change values without having to replace the whole struct and inner dictionary each time.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
CodeCruiserCommented:
Would a datatable be an option?
0
 
ugebAuthor Commented:
Interesting question, and I'm not sure as I'm not really familiar with the datatable.  I did a quick lookup and on the outset it looks promising.  The acid test is whether I can set values as though my variables were pointers.

x("a","b") would be of the MyStructType type, with variables price and dirty.  Could I do something like this with a datatable (though I know the syntax will vary somewhat)?
x("a","b") .price = 42
x("a","b") .dirty = True

The key here is that I can set the values within the structure without having to retrieve them, change them and reassign the container or create a new structure and reassign.
0
 
CodeCruiserCommented:
No datatable can only hold values not references as far as I know.
0
 
ugebAuthor Commented:
So a datatable won't work then, right?  What about my original question?  Do you know how to do that?
0
 
CodeCruiserCommented:
Your original code is using array of integers so I don't understand how you are using it to store structure.
0
 
ugebAuthor Commented:
I grabbed that snippet.  It also doesn't use strings and indexes.  My code needs a struct instead of an integer and string indexes which is why I was asking.
0
 
CodeCruiserCommented:
So again, you need it to be 2 dimensions? i.e an instance of struction at each row and cell position? Like in a grid? Or do you just need string indexes with structure being stored against it? Can you elaborate a bit on what you are trying to achieve?
0
 
ugebAuthor Commented:
Yes, it needs to be two dimensional.  It is a structure I use frequently in other languages and consistency is very valuable. It's essentially like a database in memory i.e. 2-D.  

Consider stock tickers like 'GOOG', 'EBAY', and 'MSFT'.  Each ticker will have fields associated with it (mostly the same), 'LAST', 'BID', 'VOLUME', etc. Both tickers and fields are dynamically input.

As stock prices change, each of these fields must be updated as it happens and delivered to another process as a group at specified intervals.  Only the values that change should be delivered.  Thus, each field has both a value and a dirty bit. In reality, there can be more items in the struct besides value and dirty.

In order to read and update values that change, I need to index them by both the ticker and field.  I cannot have a separate structure for each ticker or each field, so a 2-D structure indexed by strings is the best solution. Since VB doesn't pass by ref, then a class is what I have been led to believe by all conversations I've read on it.
0
 
CodeCruiserCommented:
I think you have two options

Use a Dictionary(string, Dictionary(string, integer))

Create a class with both your keys as its properties and then use that class in dictionary so it would become Dictionary(yourclass, integer)
0
 
ugebAuthor Commented:
My original question still stands.  I'm aware of different methods for achieving the same thing, and I've thought them through, but I don't like those solutions as they always end up inferior.

What I want is the above code adapted for a dictionary so I can use string indexes. Do you know how to do that?  I haven't heard of any good alternatives yet and your solution isn't what I'm looking for. I explored that possibility a while ago.

Are you able to convert the above snippet into a dictionary equivalent? At this point I think that's the only thing that will answer my questions.
0
 
ugebAuthor Commented:
The dictionary example you provided I had already tried and referenced above.  It didn't work because MyStruct is passed by value.

I couldn't see how your method of 'Dictionary(yourclass, integer) ' would get around the 'passed by val' problem of myStruct.  I finally ended up figuring out how to use my class in place of myStruct.  I didn't know classes were passed by reference, so, after some additional experimentation, I figured out  dictionary(of string, dictionary(of string, myClass)) worked (which is one of the things I was striving for in my initial post).  All I did was replace myStruct with MyClass and it worked.

I would still like to know the answer to my question, but I have something that works for me now.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.