dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: help on defining arrays

  1. #1
    Join Date
    Jun 2004
    Posts
    4

    help on defining arrays

    Hello all,
    I'm just beginner to the VB. Actually I have been working on an API of a CAD software.

    I have been creating many sub functions and many of the variables need to be shared between them.

    For an Example:
    dim...

    sub getdimention()
    ...
    End sub

    sub getlines()
    ...
    End sub

    sub getcount()
    ...
    End sub

    I have couple of doubts:
    1. In the function getcount() I'm storing an integer value in "cnt". I want to use the same in get dimension. Where to declare the variable and how to get the values in to the getdimension function. (i'm expecting something like return in C)
    2. I have an array in getdimention - that is dynamic. I mean to say the array will vary based on the value of "cnt" that Iím getting in getcount function. How to define such array...


    With Warm Regards,
    bsvk

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Some tips

    a dinamically array is declared as follow:
    Dim myArray() as Long

    then it is dimensioned this way:

    Redim myArray(5,5)

    'this is bidimensional, 0 to 5, 0 to 5
    'thus it has 6x6 cells

    If you have to redim preseving the content, you code:
    Redim Preserve myArray(5, 7)
    'the above will add 2 cells in second dimension without loosing all
    'what you had in your array.
    'Remember: you can redim only last dimension of an array.
    'Thus: no matter if your array has one ddimension only.
    but you cannot do on previous one: Redim myArray(3,7) as
    you're noit allowed to change first dimension

    To clear an array in a fast way, destroing dimensions if dinamically added, you can use Erase. Ie:
    Erase myArray

    about scope: if you declare your array at top of module where your functions and subs are, every ffunction and sub there could
    use it.
    But you can also have subs that pass byRef the array to change it, and even functions that return the array.
    ie:
    Code:
    Private Sub ChabngeTHis(byRef myArray() as long)
         Redim Preserve myArray(5,9)
         'This will change the parms of caller!
    end sub
    
    or (and note this is the Return you asked for!) 
    Private Function IamTheArray() as long()
       'use a local array:
        dim myLocal() as long
        redim myLocal(5,10)
       IamTheArray=myLocal   'here, this returns it!
    End Function
    There are some issues in using arrays. Start playing around and you will find out...
    ...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
    Jun 2004
    Posts
    4
    Hello Cimperiali,
    thanks for your qiuck responce. I have tried the same. But it is giving an error "runtime error 9. Subscript out of range".
    Below the code...
    ------------------------------------------------------------------------------
    Dim Feature_name() As String

    Sub Getfeatures()
    Dim oPartDoc As PartDocument
    Dim ofeature As PartFeature
    Dim Feature_name(10) As String
    Set oPartDoc = ThisApplication.ActiveDocument
    For Each ofeature In oPartDoc.ComponentDefinition.Features
    If ofeature.Type = kExtrudeFeatureObject Then
    Feature_name(i) = ofeature.name
    End If
    Next
    End sub

    Sub print_trial()
    For i = 0 To 9
    Debug.Print "name of the feature: "; Feature_name(i)
    Next i
    End Sub

    sub get_count()
    cnt = 10 ' sample only. this will be derived from a big function.
    Endsub
    ------------------------------------------------------------------------------
    My first Question is :
    I want to redefine/define an array by the size of "cnt" in Getfeatures() function.

    With Warm Regards,
    bsvk.

  4. #4
    Join Date
    Jun 2004
    Posts
    4
    Hello..
    sorry for the small mistake on my reply... the code of Getfeatures() as follows...

    Sub Getfeatures()
    Dim oPartDoc As PartDocument
    Dim ofeature As PartFeature
    ReDim Feature_name(10)
    Set oPartDoc = ThisApplication.ActiveDocument
    For Each ofeature In oPartDoc.ComponentDefinition.Features
    If ofeature.Type = kExtrudeFeatureObject Then
    Feature_name(i) = ofeature.name
    End If
    Next
    End sub

    With Warm Regards,
    bsvk

  5. #5
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    Try:
    Code:
    Sub Getfeatures()
       Dim oPartDoc As PartDocument
       Dim ofeature As PartFeature
        
       ReDim Feature_name(10)
       Dim Counter As Long
       Set oPartDoc = ThisApplication.ActiveDocument
       For Each ofeature In oPartDoc.ComponentDefinition.Features
          If ofeature.Type = kExtrudeFeatureObject Then
             Feature_name(Counter) = ofeature.Name
             Counter = Counter + 1
             If Counter > UBound(Feature_name) Then
                'add 10 cells 
                ReDim Preserve Feature_name(UBound(Feature_name) + 10)
             End If
          End If
       Next
       'Now redim the array for as many elements as you have 
        
       If Counter > 0 Then
          If UBound(Feature_name) > Counter - 1 Then
             ReDim Preserve Feature_name(Counter - 1)
              
          End If
       Else
          'no elements at all!
          ReDim Feature_name(0) 'one cell containing an empty string
       End If
    End Sub
    ...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.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)