Thursday, March 29, 2012

Loop ? or some other way

I have the following code setting my row = to an itemindex of a grid:

If dgPlantResults.EditItemIndex = 1Then
row = DsGoalsResults1.Tables("Results").Rows(e.Item.ItemIndex - 1)
ElseIf dgPlantResults.EditItemIndex = 3Then
row = DsGoalsResults1.Tables("Results").Rows(e.Item.ItemIndex - 2)
ElseIf dgPlantResults.EditItemIndex = 5Then
row = DsGoalsResults1.Tables("Results").Rows(e.Item.ItemIndex - 3)
EndIf
My problem is I need to do this for an infinite amount of #'s. How can I set up a loop(what type would be best also). To test until row <> Nothing or row = one of the lines?
Also the #'s 1, 3, 5 need to increment by 2. As well the # I subtract must increase by 1. I'm just having issues figuring out how to do this. Please someone help, thank you.

onewisehobbit--
Regarding this...

onewisehobbit wrote:

I have the following code setting my row
My problem is I need to do this for an infinite amount of #'s. How can I set up a loop(what type would be best also). To test until row <> Nothing or row = one of the lines?
Also the #'s 1, 3, 5 need to increment by 2. As well the # I subtract must increase by 1. I'm just having issues figuring out how to do this. Please someone help, thank you.


...it looks like you might need to dynamically derived the index and then call a helper method...
...it is not 100% clear to me what you are doing, but I think that I get the gist of it...
...as such, something like this may give you some ideas...

Public WithEvents dgPlantResults As DataGrid

Private Sub Test( _
ByVal sender As System.Object, _
ByVal e As Web.UI.WebControls.DataGridCommandEventArgs _
) Handles dgPlantResults.EditCommand

Dim DsGoalsResults1 As DataSet = Nothing
Dim row As DataRow = Nothing

'add code here to make sure DsGoalsResults1 is instantiated...

'add code here to make sure dgPlantResults is instantiatiated properly...

Dim myItemIndex As Integer = dgPlantResults.EditItemIndex - dgPlantResults.EditItemIndex
Dim myDataRow As DataRow = GetRow(myItemIndex, DsGoalsResults1)

'add code here to work with the row...
End Sub

Private Function GetRow( _
ByVal targetIndex As Integer, _
ByVal targetDataSet As DataSet _
) As DataRow

Dim myRow As DataRow = Nothing

myRow = targetDataSet.Tables("Results").Rows(targetIndex)

Return myRow
End Function


This is one of the interesting cases where you need to see the correlation between the numbers.
You don't need a loop at all. You know you're interested in the oddEditItemIndex numbers. So you need one if statement and only oneassignment.
Notice that dividing the itemindex by 2 and subtracting 0.5 will give you your new row index.
In C#:
// check whether the number is an odd number
if (dgPlantResults.EditItemIndex % 2 == 1)
{
row = DsGoalsResults1.Tables["Results"].Rows[e.Item.ItemIndex+((float)dgPlantResults.EditItemIndex / -2)-.5];
}
I'm sure you can figure out the VB.NET equivalent.
Hope that helped you out.
Wim

I ended up doing it a different way--it works so that's all I needed but thanks for the advice.

Dim itemIndex, subIndex, itemCount, dsRowCountAs Int32

itemIndex = e.Item.ItemIndex

dsRowCount = 0

itemCount = 0

subIndex = 0

For dsRowCount = 0To itemIndexStep 2

subIndex += 1

Next

row = DsGoalsResults1.Tables("Results").Rows(e.Item.ItemIndex - subIndex)


I still don't understand why you would need the loop.
You could just optimize it by writing something along the lines of the following instead:
subIndex = itemIndex-(itemIndex/2)-0.5
Regards,
Wim

Yes thank you--I tried it out this morning and it does work--No looping is necessary--Thanks for the help!
onewisehobbit--
Regarding this...

onewisehobbit wrote:

...No looping is necessary...


...in case it has not been mentioned...
...FWIW...
...in general, whenever there is a choice between a looping option and not-looping-option, one should choose the not-looping-option...
...in general, looping is slow, especially when the amount of data is large...
...this is general impetus for primary keys in database tables, indexes of an array, hash table keys, and a host of other constructs...
...these are general principles but they are held widely in algorithm analysis, for example in searching...
...and so on...
I just thought I should add some food for thought.
HTH.
Thank you.

0 comments:

Post a Comment