PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] If statement not validating-VBForums
Results 1 to 6 of 6

Thread: [RESOLVED] If statement not validating

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    879

    Resolved [RESOLVED] If statement not validating

    The following code use to work before but no longer works anymore because of the last IF STATEMENT in the code. For some reason, it wants me to write it as:
    If lStart Then If lEnd Then 'CODE GOES HERE.

    I refuse to do that because it's retarded. Why won't the AND validate? Can someone please test this code :S.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.   MsgBox GetTextBetween("THIS IS MY TEXT", "THIS ", " MY")
    5. End Sub
    6.  
    7. Public Function GetTextBetween(Source As String, Before As String, After As String, Optional ByRef Start As Long = 1, Optional ByRef LastPosition As Long) As String
    8.   Dim lStart As Long
    9.   Dim lEnd As Long
    10.  
    11.   On Error GoTo GetTextBetween_Error
    12.  
    13.   lStart = InStr(Start, Source, Before) + Len(Before)
    14.   If lStart Then lEnd = InStr(lStart, Source, After)
    15.  
    16.   LastPosition = lEnd
    17.  
    18.   If lStart And lEnd Then GetTextBetween = Mid$(Source, lStart, lEnd - lStart)
    19.  
    20.   Exit Function
    21.  
    22. GetTextBetween_Error:
    23.   MsgBox "Error #: " & Err.Number & vbCrLf & "Procedure [GetTextBetween] of Module [mHTMLFunctions]" & vbCrLf & "Description: " & Err.Description, vbOKOnly, "Error Handling"
    24. End Function
    Visual Basic 6.0
    Visual C++ 5
    Delphi 5


  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    879

    Re: If statement not validating

    I gave up on this and decided to use the following:

    If lStart > 0 And lEnd > 0 Then 'Code goes here.

    Damn inconsistancies in VB are starting to piss me off.
    Visual Basic 6.0
    Visual C++ 5
    Delphi 5


  3. #3
    March 2015 Winner kfcSmitty's Avatar
    Join Date
    May 2005
    Location
    Kingston, Ontario
    Posts
    2,153

    Re: If statement not validating

    I thought the only variable type you could do

    If variable then

    was with boolean?

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,381

    Re: If statement not validating

    Not necessarily.... integers(longs too) will sometimes work. Since False = 0 and True = Not False.... and numerical variable which has a value equal to exactly 0 will be treated as false. If it is any value other than 0, it is considered True.

    Inconsistancies or not, If lStart > 0 And lEnd > 0 is the "better" and safer way to write it any ways. Just because you can do something, doesn't mean you should. Based on you earlier statement, through, this might have worked as well: If (lStart And lEnd)

    But it's better practice to explicitly check the value > 0.

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    879

    Re: If statement not validating

    No not necessairly. There's many different characteristics that allow for T/F validation. 0 is false, but everything else is true.

    Look at the following code:

    VB Code:
    1. If 0 Then MsgBox "validates"
    2.   If 1 Then MsgBox "validates"
    3.   If 666 Then MsgBox "validates"
    4.   If -1 Then MsgBox "validates"
    Visual Basic 6.0
    Visual C++ 5
    Delphi 5


  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    879

    Re: If statement not validating

    LOL you beat me to it.


    Thx for the input. Yeah the brackets, it all makes sense now. RESOLVED!
    Visual Basic 6.0
    Visual C++ 5
    Delphi 5


Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width