CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2007
    Posts
    448

    How to returns the value as true and false?

    Hi guys,

    I am writing the code in the class to create the strings. I want the value to returns as true if the strings in the class are matched with two numbers or more, but if the if the strings in the class are not matched with first two numbers or not then returns the value as false.

    Here it is the code:

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim c As New TestStrings.Strings
    
            If Not c.Strings(Textbox1.Text) Then
                MessageBox.Show("strings not found")
            Else
                Dim a() As String = Textbox1.Text.Split(".", "."c)
                If a.Length < 6 OrElse String.IsNullOrEmpty(a(2)) Then
                    MessageBox.Show("strings found!")
                End If
            End If
        End Sub

    And the strings in the class.

    Code:
     
        Function Strings(ByVal s As String) As Boolean
            Const Strings1 As String = "6."
            Const Strings2 As String = "7."
            Const Strings3 As String = "11."
            Const Strings4 As String = "142.15"
    
            If s.IndexOf(Strings1) >= 0 Then
                Return True
            ElseIf s.IndexOf(Strings2) >= 0 Then
                Return True
            ElseIf s.IndexOf(Strings3) >= 0 Then
                Return True
            ElseIf s.IndexOf(Strings4) >= 0 Then
                Return True
            End If
            Return False
        End Function
    What I am trying to do: input the strings in the textbox "6.11.11.11" and check the strings in the class, if the strings is matched in the class by "6." then displaying the messagebox that says the strings is found. Input the different strings in the textbox like "143.11.11.11" and check the strings in the class, if the strings in the class is not found by the first few two numbers or more, then displaying the messagebox that says the strings is not found.


    When I clicked the button by input the strings in the textbox "6.11.11.11", it returns the value as true, but when I input the strings in the textbox "143.11.11.11" which it does not exist in the class, it returns the value as true which it should returns as false.


    Please can someone help me?


    Thanks,
    Mark

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: How to returns the value as true and false?

    because 11 is after the 143?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to returns the value as true and false?

    sounds like you need to test all of them before you return a true value and retunr false if any fail.

    In each if else you should check to see if the value is not there and if so then return false then at the bottom return true which will only execute when all of the strings are found.

    You could also create a boolean var defaulted to True at the top and set it to False if any item is not found then at the bottom return the boolean var which would give you just one exit point rather than the 5 you now have.


    btw the way it is currently coded 67537342623478611.234786542 would be true as it contains 11.
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: How to returns the value as true and false?

    because 11 is after the 143?
    Once again...


    Input the strings in the textbox "6.11.11.11", it returns the value as true, but when I input the strings in the textbox "143.11.11.11"
    doesn't matter what the first number is.
    Last edited by dglienna; September 20th, 2010 at 01:05 AM.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to returns the value as true and false?

    Yep upon looking again testing to see if all of them were there would not work as your samples contain 3 sets of 11 and not all of the numbers there.

    Best bet is to split the string on the . and check each element of the resulting array to see if the value is valid according to your needs. Then only if ALL elements are valid should you return true otherwise you would return false.
    Always use [code][/code] tags when posting code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured