Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

convert array structure to dictionary in vb.net

Posted on 2013-11-01
14
Medium Priority
?
607 Views
Last Modified: 2013-11-06
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

0
Comment
Question by:ugeb
  • 7
  • 7
14 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39617728
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
 
LVL 11

Author Comment

by:ugeb
ID: 39617743
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39617758
Would a datatable be an option?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Author Comment

by:ugeb
ID: 39617808
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39618131
No datatable can only hold values not references as far as I know.
0
 
LVL 11

Author Comment

by:ugeb
ID: 39618152
So a datatable won't work then, right?  What about my original question?  Do you know how to do that?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39618169
Your original code is using array of integers so I don't understand how you are using it to store structure.
0
 
LVL 11

Author Comment

by:ugeb
ID: 39618172
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39619326
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
 
LVL 11

Author Comment

by:ugeb
ID: 39619442
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39619910
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
 
LVL 11

Author Comment

by:ugeb
ID: 39620198
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
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 1500 total points
ID: 39620209
>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
 
LVL 11

Author Closing Comment

by:ugeb
ID: 39629089
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question