Solved

convert array structure to dictionary in vb.net

Posted on 2013-11-01
14
424 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
Comment Utility
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
Comment Utility
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
Comment Utility
Would a datatable be an option?
0
 
LVL 11

Author Comment

by:ugeb
Comment Utility
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
Comment Utility
No datatable can only hold values not references as far as I know.
0
 
LVL 11

Author Comment

by:ugeb
Comment Utility
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
Comment Utility
Your original code is using array of integers so I don't understand how you are using it to store structure.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 11

Author Comment

by:ugeb
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

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…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now