locked
Checking how many rows my DataTable.[Select] returns RRS feed

  • Question

  • User507956310 posted

    Hi guys, I have a select...case statement that contains If..then statements.
    This particular case checks if my DataTable contains value1 or value2 and value3
    The problem is that the IF statement can return 2 or three rows, depending value1 and value2 and value 3were
    foud or just value1 and value3, or value2 and value3.
    So I need to write another if statement that
    checks how many rows the MembershipInfoDataTable.[Select] returned so I can declare the right number of Numbers(1-2) or (1-3).
    Do you guys know how to check for how many rows the MembershipInfoDataTable.[Select] returned? I know there is no .rows.count and that is the problem.

     

     

    1    Case Is = "AND" 'Operator 2 is AND
    2    
    3    If MembershipInfoDataTable.Rows.Contains(Rulerow.Cells(3).Text) Or MembershipInfoDataTable.Rows.Contains(Rulerow.Cells(6).Text) And MembershipInfoDataTable.Rows.Contains(Rulerow.Cells(9).Text) Then
    4    
    5    
    6    'since true, now create a row array and dertmine the mix/max from values
    7    Dim rowsFound As Data.DataRow() = MembershipInfoDataTable.[Select]("MembershipName='" + Rulerow.Cells(3).Text + "' Or MembershipName='" + Rulerow.Cells(6).Text + "' Or MembershipName='" + Rulerow.Cells(9).Text + "'")
    8    
    9    Dim Number1 As Integer = rowsFound(0)("MembershipValue").ToString
    10   Dim Number2 As Integer = rowsFound(1)("MembershipValue").ToString
    11   Dim Number3 As Integer = rowsFound(2)("MembershipValue").ToStirng
    12   Dim SemiOverallResult As Integer = Math.Min(Number2, Number3)
    13   Dim OverallResult As String = Math.Max(SemiOverallResult, Number1).ToString
    14   
    15   Else
    16   Result = "FALSE"
    17   End If
    
      

     

    Thanks,

    polynaux 

    Sunday, May 4, 2008 1:41 PM

Answers

  • User437720957 posted

    would True AND False result in False then ?

    Yes!

    True AND True = True

    True OR True = True

    True OR False = True

    True AND False = False

    False OR False = False

    False AND False = False

    True OR False AND True = True OR False = True

    (True OR False) AND True = True AND True = True

    The use of parenthesis says that the "grouped" evaluation should be evaluated separately, so in this case the True OR False is evaluated (to True) before the AND evaluation is performed.

    AndAlso and OrElse are keywords that only exists in VB.NET.  The reason is this:

    In C# (and most other languages) a boolean expression is usually written like this:

    if (somevalue == "value" && someothervalue > 5)

    It is often assumed that the same expression in VB.NET should be...

    If somevalue = "value" And someothervalue > 5 Then

    but it should not! The difference is that the "&&" operator is "shortcircuiting" the evaluation, since it knows that if the first expression (somevalue=="value") is False it doesn't matter whether the second expression (someothervalue > 5) is True or False. The net result will be False either way, so it doesn't bother with evaluating the second expression at all. The same thing with an OR expression in C#...

    if (somevalue == "value" ||  someothervalue > 5)

    Which is often written in VB as..

    If somevalue="value" Or someothervalue > 5 Then

    In this case C# will "shortcircuit" the evaluation if somevalue="value", since the second expression is meaningless to the outcome of the full expression.

    This may seem unimportant, but it's very important, especially in cases where you want to test if an instance exists and at the same time check if it holds a certain property value.

    C#:

    if (myObj!=null && myObj.FirstName=="somename")

    If you in VB.NET write...

    If myObj Is Not Nothing And myObj.FirstName="somename" Then

    ..you will run into problems. If myObj in fact Is Nothing, the second expression will be evaluated anyway, but since myObj is Nothing, it is impossible to check its FirstName property, and you will get an "Object reference not set" exception.

    Maybe it makes sense that they chose to implement And and Or in this way in Visual Basic (it has been like that from the beginning), but they also (unfortunately?) chose to keep it that way in VB.NET, to make it more compatible with VB6 etc. However, MS at least gave us the possibility to use shortcircuiting operators, namely AndAlso and OrElse, which corresponds to C#'s && and ||

    I suggest that you use AndAlso and OrElse as often as you can. (C# also has non-shortcircuiting operators, & and |, but they are rarely used in if-expressions)

     

     


    I also wonder why the array.length returned 1 for the above presented if..then statement.

    should the array.length not be equal to 0 since only one statement is true and hence only one item/row is returned? 

     

    The length of an array is the number of elements it contains. An array with one element has Length=1. The element is placed at position 0 in the array, since the index i zero based.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 4, 2008 7:05 PM

All replies

  • User437720957 posted

     It's an array, so there's a length property.

    rowsFound.Length

     

    Sunday, May 4, 2008 2:18 PM
  • User507956310 posted

     Hi gunteman,

    I used the array.length and it does what I want.

    A new problem emerged, my initial IF statement does not act as it should

    1    If MembershipInfoDataTable.Rows.Contains(high) Or MembershipInfoDataTable.Rows.Contains(medium) And MembershipInfoDataTable.Rows.Contains(low) Then
    When my datatable contains
    high, extrem
    it should be false, instead its true . How can that be, since the condition medium AND low does not hold true. Only the first thing is true and the whole if statement is true ?
    Sunday, May 4, 2008 5:42 PM
  • User507956310 posted

     I tried the "AndAlso" and "OrElse" operators instead of "AND" and "Or" but it did not change the issue

    Sunday, May 4, 2008 5:53 PM
  • User437720957 posted

    1 If MembershipInfoDataTable.Rows.Contains(high) Or MembershipInfoDataTable.Rows.Contains(medium) And MembershipInfoDataTable.Rows.Contains(low) Then
    When my datatable contains
    high, extrem
    it should be false, instead its true . How can that be, since the condition medium AND low does not hold true. Only the first thing is true and the whole if statement is true ?

    Yes, that's how OR and AND work (in all languages). AND is always evaluated first.

    In the case you mentioned (high,extreme) your statement is equal to:

    True Or False And False

    Which, after evaluating the And,  is equal to (since if any part of an And evaluation is False, the result is False)

    True Or False

    Which is equal to (since if any part of an Or evaluation is True, the result is True)

    True

    Sunday, May 4, 2008 6:00 PM
  • User507956310 posted

     thanks gunteman,

     that was a good explanation.

    I wonder if you can make the AND compulsory, or does this not make sense?

    As far as I see it if the AND condition is false the whole condition should be false. - or is it just me who sees it that way :-)

    would True AND False result in False then ?
     

     

    I also wonder why the array.length returned 1 for the above presented if..then statement.

    should the array.length not be equal to 0 since only one statement is true and hence only one item/row is returned? 

    Sunday, May 4, 2008 6:06 PM
  • User437720957 posted

    would True AND False result in False then ?

    Yes!

    True AND True = True

    True OR True = True

    True OR False = True

    True AND False = False

    False OR False = False

    False AND False = False

    True OR False AND True = True OR False = True

    (True OR False) AND True = True AND True = True

    The use of parenthesis says that the "grouped" evaluation should be evaluated separately, so in this case the True OR False is evaluated (to True) before the AND evaluation is performed.

    AndAlso and OrElse are keywords that only exists in VB.NET.  The reason is this:

    In C# (and most other languages) a boolean expression is usually written like this:

    if (somevalue == "value" && someothervalue > 5)

    It is often assumed that the same expression in VB.NET should be...

    If somevalue = "value" And someothervalue > 5 Then

    but it should not! The difference is that the "&&" operator is "shortcircuiting" the evaluation, since it knows that if the first expression (somevalue=="value") is False it doesn't matter whether the second expression (someothervalue > 5) is True or False. The net result will be False either way, so it doesn't bother with evaluating the second expression at all. The same thing with an OR expression in C#...

    if (somevalue == "value" ||  someothervalue > 5)

    Which is often written in VB as..

    If somevalue="value" Or someothervalue > 5 Then

    In this case C# will "shortcircuit" the evaluation if somevalue="value", since the second expression is meaningless to the outcome of the full expression.

    This may seem unimportant, but it's very important, especially in cases where you want to test if an instance exists and at the same time check if it holds a certain property value.

    C#:

    if (myObj!=null && myObj.FirstName=="somename")

    If you in VB.NET write...

    If myObj Is Not Nothing And myObj.FirstName="somename" Then

    ..you will run into problems. If myObj in fact Is Nothing, the second expression will be evaluated anyway, but since myObj is Nothing, it is impossible to check its FirstName property, and you will get an "Object reference not set" exception.

    Maybe it makes sense that they chose to implement And and Or in this way in Visual Basic (it has been like that from the beginning), but they also (unfortunately?) chose to keep it that way in VB.NET, to make it more compatible with VB6 etc. However, MS at least gave us the possibility to use shortcircuiting operators, namely AndAlso and OrElse, which corresponds to C#'s && and ||

    I suggest that you use AndAlso and OrElse as often as you can. (C# also has non-shortcircuiting operators, & and |, but they are rarely used in if-expressions)

     

     


    I also wonder why the array.length returned 1 for the above presented if..then statement.

    should the array.length not be equal to 0 since only one statement is true and hence only one item/row is returned? 

     

    The length of an array is the number of elements it contains. An array with one element has Length=1. The element is placed at position 0 in the array, since the index i zero based.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 4, 2008 7:05 PM