Solved

convert array structure to dictionary in vb.net

Posted on 2013-11-01
14
511 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

623 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