Results 1 to 9 of 9

Thread: Two-dimensional array question

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    37

    Two-dimensional array question

    Is it possible to have a two-dimensional array/list/collection such as the following.

    The first dimension is to have 2 elements, 0 and 1. The second dimension I would like to be able to have a different number of elements for each of the elements in the first dimension.

    To explain better, I'd like X in array(0, X) to go up to say 4 and Y in array(1, Y) to only go up to say 2.

    So there would be:

    array(0,0)
    array(0,1)
    array(0,2)
    array(0,3)
    array(0,4)
    array(1,0)
    array(1,1)
    array(1,2)

    but no array(1,3) or array(1,4).

    I'd also need to be able to use Redim Preserve to increase the size of the second dimension for each of the first dimension's elements.

    I suspect this is not possible but if it is it would be of benefit to me so I will be greatly appreciative of any assistance.

    Could an array of arrays be the answer?

  2. #2
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Two-dimensional array question

    Can you tell us what kind of data you're trying to store? Doesn't seem to fit well with an array but creating your own objects might make things easier.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    37

    Re: Two-dimensional array question

    The data type is Short.

    I've done some searching and found out how to use a jagged array. So to start with I've got

    Dim PlayerCard(0 To 1)() As Short
    PlayerCard(0) = New Short(0 To 0) {}
    PlayerCard(0)(0) = 7

    My problem is I'm not sure how to resize PlayerCard(0) while preserving its values.

    I know I can then do

    PlayerCard(0) = New Short(0 to 1) {PlayerCard(0)(0), 0}

    to add a second element but when the number of elements gets larger this method will become very ineffecient. Is there a better way?

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    37

    Re: Two-dimensional array question

    I think I found what I need. I used Array.Resize(PlayerCard(0),Int) which preserves the values.

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Two-dimensional array question

    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    37

    Re: Two-dimensional array question

    Blackjack actually.

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Two-dimensional array question

    The deal and hands functionality could be used. You would need to write the hand evaluator for black jack, which should not be all that hard.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    37

    Re: Two-dimensional array question

    I've already written the logic from scratch but thanks for the suggestion.

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Two-dimensional array question

    I just mentioned it because it has all the images for each card in it, as well as the concepts for a deck, and hands.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width