Click here to Skip to main content
15,889,116 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: A textbox with squiggly lines in different colours Pin
dan!sh 11-Mar-09 22:53
professional dan!sh 11-Mar-09 22:53 
GeneralRe: A textbox with squiggly lines in different colours Pin
Expert Coming11-Mar-09 23:03
Expert Coming11-Mar-09 23:03 
AnswerRe: A textbox with squiggly lines in different colours Pin
Expert Coming11-Mar-09 22:34
Expert Coming11-Mar-09 22:34 
QuestionCompare data of the last day with the past 4 day’s data Pin
Dobrobit11-Mar-09 18:14
Dobrobit11-Mar-09 18:14 
AnswerRe: Compare data of the last day with the past 4 day’s data Pin
Jumping Jupiter12-Mar-09 7:05
Jumping Jupiter12-Mar-09 7:05 
GeneralRe: Compare data of the last day with the past 4 day’s data Pin
Dobrobit12-Mar-09 11:16
Dobrobit12-Mar-09 11:16 
GeneralRe: Compare data of the last day with the past 4 day’s data Pin
Dobrobit15-Mar-09 21:36
Dobrobit15-Mar-09 21:36 
GeneralRe: Compare data of the last day with the past 4 day’s data Pin
Jumping Jupiter16-Mar-09 1:43
Jumping Jupiter16-Mar-09 1:43 
I assume that your are quite new to VB.net but I may be wrong. I have therfore written the attached code using an array as you asked. I would not say it is the best method but if you are learning they I thought it better.

Create a new project with a form1 and two listboxes (ListBox1 and ListBox2)
Replace the code from your form with the attached code.

It automatically generates test data to show you it working.

It may not be exactly what you want but its a start.


Public Class Form1


  Const NumberOfDaystocountBack = 4 'This is a constant for the number of dats you will count back
  Dim OriginalDataArray(15, 5) As String 'This is a sample array with 15 records each with 5 pieces of data
  Dim ResultsArray(1, 1) As String 'This is a sample array that will store the data, it is resized with the code

  Private Sub Main()
    'Generate test data
    Dim X As Integer
    Dim Y As Integer
    Dim TempString As String
    For X = 15 To 0 Step -1
      TempString = ""
      For Y = 0 To 4
        OriginalDataArray(X, Y) = Int(Rnd() * 10)
        TempString = TempString & OriginalDataArray(X, Y).ToString & ", "
      Next
      ListBox1.Items.Add(TempString)
    Next

    GenerateResults()

    For X = 15 To 0 Step -1
      TempString = ""
      For Y = 0 To 3

        TempString = TempString & ResultsArray(X, Y).ToString & ", "
      Next
      ListBox2.Items.Add(TempString)
    Next


  End Sub

  Private Sub GenerateResults()
    Dim MaxDayCount As Long
    Dim LoopCounter As Integer
    Dim RecordLoopCounter As Long
    Dim DataLoopCounter As Long
    Dim DataCount As Integer 'count of the number of data elements

    Dim TempArray1() As Integer
    Dim TempArray2() As Integer

    DataCount = OriginalDataArray.GetUpperBound(1) 'This will be '5' for the sample array given
    MaxDayCount = OriginalDataArray.GetUpperBound(0)  'This would be 50 for the sample array given

    ReDim ResultsArray(MaxDayCount, NumberOfDaystocountBack) 'Resize the results array to accept data


    For RecordLoopCounter = MaxDayCount To 0 Step -1 'Steps back through records of data

      'Pass the two days data to the comparison function
      For DataLoopCounter = 1 To NumberOfDaystocountBack

        ReDim TempArray1(DataCount) 'Resize array to the number of data elements and clears data
        ReDim TempArray2(DataCount) 'Resize array to the number of data elements and clears data
        If RecordLoopCounter - DataLoopCounter >= 0 Then
          For LoopCounter = 0 To DataCount - 1
            'Creates two temporary arrays that hold the data for the two days in question
            TempArray1(LoopCounter) = OriginalDataArray(RecordLoopCounter, LoopCounter)
            TempArray2(LoopCounter) = OriginalDataArray(RecordLoopCounter - DataLoopCounter, LoopCounter)
          Next

          ResultsArray(RecordLoopCounter, DataLoopCounter - 1) = CompareTwoDays(TempArray1, TempArray2)

        Else
          ResultsArray(RecordLoopCounter, DataLoopCounter - 1) = "-"

        End If

      Next

    Next

  End Sub




  Private Function CompareTwoDays(ByVal TodayArray() As Integer, ByVal CompareDayArray() As Integer) As Integer
    Dim TodayArraySize As Integer
    Dim LoopCounter As Integer
    Dim TempCount As Integer

    If TodayArray.GetUpperBound(0) <> CompareDayArray.GetUpperBound(0) Then
      'Put your error handling here
      MsgBox("Cannot compare arrays of different sizes.", MsgBoxStyle.Critical, "Error")
      End
    End If


    TodayArraySize = TodayArray.GetUpperBound(0)
    'Loop through array to compare values
    For LoopCounter = 0 To TodayArraySize - 1
      If TodayArray(LoopCounter) = CompareDayArray(LoopCounter) Then TempCount = TempCount + 1 'or you can use TempCount+=
    Next

    Return TempCount

  End Function



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


Good luck
QuestionInputbox triggering form_closed event Pin
Sonhospa11-Mar-09 13:48
Sonhospa11-Mar-09 13:48 
AnswerRe: Inputbox triggering form_closed event Pin
Luc Pattyn11-Mar-09 14:16
sitebuilderLuc Pattyn11-Mar-09 14:16 
GeneralRe: Inputbox triggering form_closed event Pin
Sonhospa12-Mar-09 9:26
Sonhospa12-Mar-09 9:26 
GeneralRe: Inputbox triggering form_closed event Pin
Luc Pattyn12-Mar-09 10:33
sitebuilderLuc Pattyn12-Mar-09 10:33 
GeneralRe: Inputbox triggering form_closed event Pin
Sonhospa13-Mar-09 12:21
Sonhospa13-Mar-09 12:21 
GeneralRe: Inputbox triggering form_closed event Pin
Luc Pattyn13-Mar-09 12:30
sitebuilderLuc Pattyn13-Mar-09 12:30 
AnswerRe: Inputbox triggering form_closed event Pin
Dave Kreskowiak12-Mar-09 3:47
mveDave Kreskowiak12-Mar-09 3:47 
GeneralRe: Inputbox triggering form_closed event Pin
Sonhospa12-Mar-09 9:29
Sonhospa12-Mar-09 9:29 
Questionerror propagation [modified] Pin
captainmogo11-Mar-09 9:58
captainmogo11-Mar-09 9:58 
AnswerRe: error propagation Pin
Dave Kreskowiak12-Mar-09 3:46
mveDave Kreskowiak12-Mar-09 3:46 
GeneralRe: error propagation Pin
captainmogo12-Mar-09 4:15
captainmogo12-Mar-09 4:15 
GeneralRe: error propagation Pin
Luc Pattyn12-Mar-09 4:54
sitebuilderLuc Pattyn12-Mar-09 4:54 
GeneralRe: error propagation Pin
captainmogo12-Mar-09 5:03
captainmogo12-Mar-09 5:03 
GeneralRe: error propagation Pin
Luc Pattyn12-Mar-09 5:23
sitebuilderLuc Pattyn12-Mar-09 5:23 
GeneralRe: error propagation Pin
captainmogo12-Mar-09 8:14
captainmogo12-Mar-09 8:14 
QuestionHow to export to word using VB 2008 Pin
Manfred ramirez11-Mar-09 8:45
Manfred ramirez11-Mar-09 8:45 
AnswerRe: How to export to word using VB 2008 Pin
Luc Pattyn11-Mar-09 9:41
sitebuilderLuc Pattyn11-Mar-09 9:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.