using for loop and if statement in vb.net

Hi
here is code of my function.
in the code i have called another function (retrieve)from class2,which returns array into  my present class1.
i am using a column 'state' from that array.function should loop thru the column in table and compare the consecutive values.if there is a change in values then first value should be sent to 'old data' column and second value into 'newdata' column in another table .and if there is no change,then value should be stored in 'olddata'column&newdata column should be empty.

example:
table1
state(column name)
tx
il
nc
nc
md
va
va

Table2
old data        newdata
tx                     il
il                      nc
nc                   -
nc                   md
md                   va
va                    -

but with the code below,output is like this

Table2
old data        newdata
tx               tx    
il               il    
nc               nc    
nc               nc    
md               md    
va               va  
wht is the change which should be made in order to get the correct output.
Thank you.
Public Function xyz(ByVal ID As Integer, ByVal no As Integer) As class1()
 
	Dim obj1 As New class1
        Dim i As Integer
        Dim obj2 As New class2
        Dim arclass1() As class1
	Dim arclass2() As class2
	Dim b as boolean
 
        arclass2= obj2.Retrieve(ID,No)
	b=false
        i = 0
        For Each obj2 In arclass2
	obj1.Olddata = obj2.state
	if i=0 then
	ReDim arclass1(i)
	obj1.Newdata=""
	arclass1(i) = obj1
	i=i+1
	else
	if arclass1(i-1).olddata=obj1.oldata then
	b=true
	else
	if b then
	ReDim Preserve arclass1(i)
	arclass1(i).Olddata=arclass1(i-1).Olddata
	i=i+1
	end if
	arclass1(i).Newdata=obj1.olddata
	ReDim Preserve arclass1(i)
	obj1.newdata=""
	arclass1(i)=obj1
	i=i+1
	 b = False
	end if
	end if
        Next
        Return arclass1
 
End Function

Open in new window

tracy_pAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Joel CoehoornDirector of Information TechnologyCommented:

Public Function xyz(ByVal ID As Integer, ByVal no As Integer) As class1()
    Dim obj1 As class1
    Dim obj2 As New class2
        
    Dim arclass2() As class2 = obj2.Retrieve(ID,No)
    Dim arclass1(arclass2.Length) As class1  
 
    For i as integer = 0 To arclass2.Length - 1
        obj2 = arclass2(i)
        obj1 = new class1()
 
        obj1.Olddata = obj2.state
        If i<arclass2.Length-1 AndAlso arclass2(i+1).state<>obj2.state Then
            obj1.Newdata=arclass2(i+1).state
        End If
        arclass1(i) = obj1
 
     
     Next i
     Return arclass1
 
End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ee_rleeCommented:
hi tracy_p

Sorry, haven't read your post about the code having a problem. i already unsubscribed from it.

i changed
     arclass1(i).Newdata=obj1.olddata
to
     arclass1(i-1).Newdata=obj1.olddata
in the code.

i already added some comments to explain how it works.
i can provide a more detailed explanation if you want
Public Function xyz(ByVal ID As Integer, ByVal no As Integer) As class1()
 
        Dim obj1 As New class1
        Dim i As Integer
        Dim obj2 As New class2
        Dim arclass1() As class1
        Dim arclass2() As class2
        Dim b as boolean
 
        arclass2= obj2.Retrieve(ID,No)
        b=false
        i = 0
        For Each obj2 In arclass2
                obj1.Olddata = obj2.state
                if i=0 then                    
'this is just for initialization
                        ReDim arclass1(i)
                        obj1.Newdata=""
                        arclass1(i) = obj1
                        i=i+1
                else
'this will check whether the next data is the same as the prev data
                        if arclass1(i-1).olddata=obj1.oldata then  
'if the data is the same, we do nothing but records that there was a repeat in data
                                b=true
                        else
'the data has changed
                                if b then
'if the data has repeated before changing, we add an item where the newdata is null
'before adding a new item with the olddata and the new data
                                        ReDim Preserve arclass1(i)
                                        arclass1(i).Olddata=arclass1(i-1).Olddata
                                        i=i+1
                                end if
'this will record the newdata
                                arclass1(i-1).Newdata=obj1.olddata
 
'this will create a new item with the new data as an old data
                                ReDim Preserve arclass1(i)
                                obj1.newdata=""
                                arclass1(i)=obj1
                                i=i+1
                                b = False
                        end if
                end if
        Next
        Return arclass1
 
End Function

Open in new window

0
tracy_pAuthor Commented:
Thanks jcoehoorn and ee_rlee
0
tracy_pAuthor Commented:
hi  jcoehoorn and ee_rlee.
the above code works if there are few columns.what if i have 100 columns.

Thank you
0
Joel CoehoornDirector of Information TechnologyCommented:
It should still work, just take a little longer, but you probably won't notice a difference.  If you have 100 columns in a database table, you need to normalize your database.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.