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

Thread: adding arrays

  1. #1
    Join Date
    Nov 2004
    Posts
    32

    adding arrays

    hello guys

    i have a quick question

    how can i add an array to each other ................ for example

    i created an array like ...... price(100)

    and it collect the prices which is added fron text box

    and i want to add them to each other and asign the total to a variable called ......total and print it in a form


    thanks

  2. #2
    Join Date
    Nov 2004
    Posts
    32

    Re: adding arrays

    any one has an idea

  3. #3
    Join Date
    Dec 2001
    Posts
    6,332

    Re: adding arrays

    I suppose you could iterate through the array with a simple loop.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  4. #4
    Join Date
    Nov 2004
    Posts
    32

    Re: adding arrays

    wizbang

    how lets say that i have a hundred array

    price (100)

    how to get the total of them

    i did this:


    for counter = 0 to 100

    total = price(counter) + price (counter)

    next counter

    but this gave me the wrong summtion

    any one has more ideas

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

    Re: adding arrays

    do you want a single total for the 101 prices?
    then simply:
    Code:
    Dim Total as double
    Dim Counter as Long
    For Counter =lbound(price) to ubound(price) 
       Total= Total+price(Counter)
    Next
    msgbox format(Total,"@,@@@,@00.00")
    ...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.

  6. #6
    Join Date
    Nov 2004
    Posts
    32

    Re: adding arrays

    thanks man it is working fine

    now i have anthor quetion

    how can i set them prices and the total to zero..... i mena by this to clear the variable price(counter) and set it to zero


    thanks again

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

    Re: adding arrays

    the total:
    Code:
    Total=0
    A single cell:
    Code:
    price(55)=0
    the whole array (if it was a fixed length one, like in:
    Dim Price(100) as Double
    Code:
    Erase price
    If it is not a fixed length array (or if it is not
    a numeric one and you want a zero inside each cell)
    then:
    Code:
    Dim Counter as long
    for Counter=lBound(Price) to Ubound(Price)
        Price(Counter)=0
    next
    Last edited by Cimperiali; November 21st, 2004 at 01:36 PM.
    ...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.

  8. #8
    Join Date
    Nov 2004
    Posts
    32

    Re: adding arrays

    thanks cimperiali

    it worked

    i have some more quetions

    now what i am buliding is a program which connect to database that is local to my hard drive using ado like this


    Set adoCon = New ADODB.Connection
    Set adoRecordset = New ADODB.Recordset
    connectString = "Provider = Microsoft.Jet.OLEDB.4.0;" _
    & "Data Source=fatabase\sample.mdb"


    adoCon.Open connectString
    adoRecordset.Open "product", adoCon

    so now if i want to compile my program and use it as an exe, what should ido

    note this"Data Source=fatabase\sample.mdb" .....how can i use it universally

    thanks

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

    Re: adding arrays

    Many ways.
    You could have an ini file (or a registry key) where to put the path of db.
    Or, more easily, you could have the db in same folder of your exe and code
    like the following (it will work also in develope if you open your project by
    doubleclicking on it via resource explorer. It will not work fine if you open Vb
    ide and then open the last opened project...) :
    Code:
    connectString = "Provider = Microsoft.Jet.OLEDB.4.0;" _
    & "Data Source=" & AddSlash(App.Path) & "sample.mdb"
    where addSlash is a function like the following:
    Code:
    Public Function AddSlash(ByVal sPath As String) As String
        sPath = Trim(sPath)
        If Len(sPath) > 0 Then
            If Right$(sPath, 1) <> "/" Then
                If Right$(sPath, 1) <> "\" Then
                    sPath = sPath & "\"
                End If
            End If
            AddSlash = sPath
        End If
    End Function
    By the way: you could put the db in a subfolder. Ie:
    Code:
    connectString = "Provider = Microsoft.Jet.OLEDB.4.0;" _
    & "Data Source=" & AddSlash(App.Path) & "DataBase\sample.mdb"
    ...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