?
Solved

Need a method to insert 8 values into some sort of array, hashtable, etc....

Posted on 2006-03-20
9
Medium Priority
?
262 Views
Last Modified: 2010-08-05
Friends,

Background:

I am looking to fill out a spreadsheet (created through vb.net) with values I have stored in a table.

Here is the method for getting the values I want to put in the Excel Spreadsheet:

Public Shared IRLPrizeMoneyArray As New ArrayList

    Public Sub BuildIRLPrizeMoneyArray(ByVal dsnPoints)
        Try
            Dim data As New DataSet
            Dim r As DataRow
            Dim con As New SqlClient.SqlConnection(dsnPoints)
            data = New DataSet
            Dim dr As SqlClient.SqlDataReader
            con = New SqlClient.SqlConnection(dsnPoints)
            con.Open()
            Dim c As New SqlClient.SqlCommand("SELECT" & vbCrLf _
            & "FinishPosition," & vbCrLf _
            & "CarNumber, " & vbCrLf _
            & "Entrant, " & vbCrLf _
            & "IRLBasic, " & vbCrLf _
            & "IRLQual, " & vbCrLf _
            & "IRLTotal, " & vbCrLf _
            & "IRLPointsFund, " & vbCrLf _
            & "Total, " & vbCrLf _
            & "FROMPrizeMoney" & vbCrLf _
            & "ORDER BY FinishPosition", con)
            dr = c.ExecuteReader(CommandBehavior.SingleResult)
            Dim i As Integer = 0

            Dim Position As Integer
            Dim CarNumber As String
            Dim Entrant As String
            Dim IRLBasic As Decimal
            Dim IRLQual As Decimal
            Dim IRLTotal As Decimal
            Dim IRLPointsFund As Decimal
            Dim Total As Decimal

            'loop through the records
            If dr.HasRows Then
                While dr.Read()
                    ' fill array
                    Position = dr.Item("FinishPosition")
                    CarNumber = dr.Item("FinishPosition")
                    Entrant = dr.Item("FinishPosition")
                    IRLBasic = dr.Item("FinishPosition")
                    IRLQual = dr.Item("FinishPosition")
                    IRLTotal = dr.Item("FinishPosition")
                    IRLPointsFund = dr.Item("FinishPosition")
                    Total = dr.Item("FinishPosition")

                    IRLPrizeMoneyArray.Add(Position, CarNumber, Entrant, IRLBasic, IRLQual, IRLTotal, IRLPointsFund, Total)

                End While
            End If

            con.Close()
            con = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Sub: BuildIRLPrizeMoneyArray")
        End Try
    End Sub

Well, I'd like to say it works, but apparently, you can't put that many values into an array list.  Any suggestions?
0
Comment
Question by:indy500fan
  • 5
  • 4
9 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16236430
You could create a class (or struct if thats allowed in VB) to hold the values, then add that to the ArrayList.

Here's a very simplistic example:

  Public Class Form1

      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

          Dim arr As New ArrayList()
          Dim info As MyInfo

          For i As Integer = 1 To 10
              info = New MyInfo()
              info.Position = i
              info.CarNumber = i
              info.Entrant = i
              info.IRLBasic = i
              info.IRLQual = i
              info.IRLTotal = i
              info.IRLPointsFund = i
              info.Total = i

              arr.Add(info)
          Next

      End Sub

  End Class

  Public Structure MyInfo

      Public Position As Integer
      Public CarNumber As Integer
      Public Entrant As Integer
      Public IRLBasic As Integer
      Public IRLQual As Integer
      Public IRLTotal As Integer
      Public IRLPointsFund As Integer
      Public Total As Integer

  End Structure


Hope this helps.
0
 

Author Comment

by:indy500fan
ID: 16236483
????

No, I don't think this is what I am looking for..

With regards to an array, I am drawing a blank...

Can't I just define an array to have 8 elements by 30 records long???

Wouldn't it be something like

Dim myArray(8,100)???
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 16236571

        Dim arr(8, 30) As Integer
        Dim Count As Integer = 0

        If dr.HasRows Then
            While dr.Read()
                ' fill array
                arr(0, Count) = dr.Item("FinishPosition")
                arr(1, Count) = dr.Item("FinishPosition")
                arr(2, Count) = dr.Item("FinishPosition")
                arr(3, Count) = dr.Item("FinishPosition")
                arr(4, Count) = dr.Item("FinishPosition")
                arr(5, Count) = dr.Item("FinishPosition")
                arr(6, Count) = dr.Item("FinishPosition")
                arr(7, Count) = dr.Item("FinishPosition")
                Count += 1
            End While
        End If


Kinda defeats the purpose of OOP, but its your app.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:indy500fan
ID: 16236624
Thanks carl.

That's what I was looking for.
0
 

Author Comment

by:indy500fan
ID: 16236911
Wait, what if every value isn't an integer?  As you can see there are decimals, strings and integers..

I screwed up with the dr.Read, it should be:

            Dim Position As Integer
            Dim CarNumber As String
            Dim Entrant As String
            Dim IRLBasic As Decimal
            Dim IRLQual As Decimal
            Dim IRLTotal As Decimal
            Dim IRLPointsFund As Decimal
            Dim Total As Decimal

            'loop through the records
            If dr.HasRows Then
                While dr.Read()
                    ' fill array
                    Position = dr.Item("FinishPosition")
                    CarNumber = dr.Item("CarNumber")
                    Entrant = dr.Item("Entrant")
                    IRLBasic = dr.Item("IRLBasic")
                    IRLQual = dr.Item("IRLQual")
                    IRLTotal = dr.Item("IRLTotal")
                    IRLPointsFund = dr.Item("IRLPointsFund")
                    Total = dr.Item("Total")
                End While
            End If
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16236934
The you'll have to declare the array as Object and do a cast on every value when you want to retrieve it from the array.
0
 

Author Comment

by:indy500fan
ID: 16236972
But everything else is the same?

So the syntax to add to the array would be:

       Dim arr(8, 30) As Object
        Dim Count As Integer = 0

        If dr.HasRows Then
            While dr.Read()
                ' fill array
                arr(0, Count) = dr.Item("FinishPosition")
                arr(1, Count) = dr.Item("CarNumber")
                arr(2, Count) = dr.Item("Entrant")
                arr(3, Count) = dr.Item("IRLBasic")
                arr(4, Count) = dr.Item("IRLQual")
                arr(5, Count) = dr.Item("IRLTotal")
                arr(6, Count) = dr.Item("IRLPointsFund")
                arr(7, Count) = dr.Item("Total")
                Count += 1
            End While
        End If

Now to retrieve the finish position for example, what would the syntax be with the cast?

0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16237025
Dim val As Integer
val = Convert.Int32(arr(0, 0))
0
 

Author Comment

by:indy500fan
ID: 16237345
Carl,

Thanks for your help!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Screencast - Getting to Know the Pipeline

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question