CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2002
    Location
    Armenia
    Posts
    62

    two dimensional arrays

    I can't imagine how VB works with arrays. What I need is to pass first element of two dimensional array to function like this:

    Code:
     function f(a() as Byte)
     ....
     end function
    
     dim v(10,10) as Byte
    
     call f(v(0))
    Besides code like this doesn't work also:

    Code:
     dim a(10,10) as Byte
     dim b(10) as Byte
     a(0) = b
    thanks...

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        Dim v(10, 10) As Byte
        'f expect an array
        'byref -
        'If you want to send a dimension,
        'you have to recreate a monodimensional array:
        Dim MonoV() As Byte
        ReDim MonoV(UBound(v, 1)) 'Ubound of first dimension of "v"
        Dim Counter As Long
        For Counter = 0 To UBound(MonoV)
            MonoV(Counter) = v(0, Counter)
        Next
        Call f(MonoV)
         
        'if you want to pass only a single cell,
        'you have to create an array of one cell only:
        ReDim MonoV(0)
        MonoV(0) = v(0, 0)
        Call f(MonoV)
         
        'If you want, you can send the whole array
        '-better say: you can passs the pointer to
        'your bidimensional array:
        Call f(v)
    End Sub
    Function f(a() As Byte)
    On Error Resume Next
        Dim dimensions As Integer
        dimensions = UBound(a, 2)
        If Err.Number <> 0 Then
            'you do not have a bidim array!
            dimensions = 1
        Else
            dimensions = 2
        End If
        Err.Clear
        On Error GoTo 0
        Select Case dimensions
            Case 1
                Debug.Print UBound(a, 1)
            Case 2
                Debug.Print UBound(a, 2)
        End Select
    End Function
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Aug 2002
    Location
    Armenia
    Posts
    62
    It's an interesting code, but actually not the very thing that I need. I missed to mention earlier, that values of twodimensinal array must be used later, after function's call, so I need to pass pointer to that array.
    As regards to Function f(a() As Byte) , it works only with onedimensional arrays. I can't modify it and add any dimension checking procedures.
    Of course, it's possible to pass onedimensional array and then copy all elemets to twodimensional, but is there eny prettiest way in VB to arrange with same kind of tasks?
    Thank you very much.

  4. #4
    Join Date
    Aug 2002
    Location
    Armenia
    Posts
    62
    By the way, is there any function in VB, that converts String to array of bytes?

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

Featured