Solved

convert array structure to dictionary in vb.net

Posted on 2013-11-01
14
440 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 500 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

810 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