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

Thread: Arrays

  1. #1
    Join Date
    Jun 2001
    Location
    Sri Lanka
    Posts
    272

    Arrays

    There is an array having dimensioned to 100 elements
    Data is filled up to the 4th element
    Eg.
    Dim s(100) as string
    s(0) = "aa"
    s(1) = "bb"
    s(2) = "cc"
    s(3) = "dd"


    Is there a direct method to find the number of data filled elements (in this case, 4) without using a loop to find the element number having the "" element.
    Also I don't want to use Collections.
    Srinika


    If u don't know how to Rate an answer, then Rate my answer to learn, If u know, then practice it

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

    Re: Arrays

    The only way i can think of is:
    While writing data to array, store in a variable the index of last element written.


    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...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
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Arrays

    First a little aside:

    By default, Visual basic arrays are zero based, which means that:

    Dim s(100) as string



    actually has 101 elements.
    To explicitly set the lower bound use:

    Dim s(1 to 100) as string




    OK - so to find the first empty element...



    Dim lArrayBound as Long

    for lArrayBound = LBound(s) to UBound(s)
    If s(lArrayBound) = "" then
    Exit for
    End If
    next lArrayBound

    'next free element is held in lArrayBound now...




    HTH,
    D.

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    Check out the new downloads - ImageMap.ocx is the VB control that emulates an HTML image map, EventVB.OCX for adding new events to your VB form and adding System Tray support simply, MCL Hotkey for implemenmting system-wide hotkeys in your application...all with source code included.
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  4. #4
    Join Date
    Jun 2001
    Location
    Sri Lanka
    Posts
    272

    Re: Arrays

    In my Question I told that --No Loops--
    So that way will not work out


    If u don't know how to Rate an answer, then Rate my answer to learn, If u know, then practice it

  5. #5
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Arrays

    In that case you are going to have to increase/decrease the size of your array as needed.


    Dim s(1 to 1) as string

    private Sub AddElement(sNewElement as string)

    If s(1) = "" then
    s(1) = sNewElement
    else
    Redim Preserve s(1 to (UBound(s)+1)) as string
    s(UBound(s)) = sNewElement
    End If
    End Sub





    HTH,
    D.


    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    Check out the new downloads - ImageMap.ocx is the VB control that emulates an HTML image map, EventVB.OCX for adding new events to your VB form and adding System Tray support simply, MCL Hotkey for implemenmting system-wide hotkeys in your application...all with source code included.
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

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

    Re: Yes.

    This is how, normally. But for some strange way I do think the one who quested has its 101 array dimensioned and do not want it to be less...

    ;-)

    Stay with us, you Guru.

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...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
  •  





Click Here to Expand Forum to Full Width

Featured