locked
Conditions RRS feed

  • Question

  • Is there a way to make visual basic check simple if conditions?

    What I mean is: any normal programming language would know that

    if 'xyz' returns true because 'xyz' is neither undefined nor an empty string, but VB is dumb enough to try to convert a string to boolean and fails to test the condition. Same thing with integers. Is there a way to make it test conditions normally?

    Wednesday, October 11, 2017 12:52 PM

All replies

  • Is there a way to make visual basic check simple if conditions?

    What I mean is: any normal programming language would know that

    if 'xyz' returns true because 'xyz' is neither undefined nor an empty string, but VB is dumb enough to try to convert a string to boolean and fails to test the condition. Same thing with integers. Is there a way to make it test conditions normally?

    Hi

    Rambling on like that really isn't likely to get a decent answer.

    Simple 'If' conditions - show code to illustrate what you mean/want.

    Normal programming language - which ones are they?

    Your last paragraph makes no sense at all! Post code examples to support.



    Regards Les, Livingston, Scotland



    • Edited by leshay Wednesday, October 11, 2017 1:07 PM
    Wednesday, October 11, 2017 1:05 PM
  • What kind of VB. 

    The ones from Microsoft don't do what you write. 

    In my perception you use JavaScript. 

    If 'xyz' is not accepted in VB.

    Everything after the ' is seen as a comment


    Success
    Cor

    Wednesday, October 11, 2017 1:05 PM
  • Hello,

    One thing you should do, under project properties, compile tab set Option String On, Option Infer Off to start with then using assertion to work with variables e.g. If Not String.IsEmptyOrWhiteSpace(somevariable), on other variables work with TryParse such as when testing if a string can be converted to a Date for instance. You can check if a variable is null/nothing, use TypeOf for checking type and much more.

    VB is only, as you put it dumb if you allow it to be along with using poor programming practices.

    Personally I have Option Strict On, Option Infer On (only recommend this once you understand the pros and cons) for defaults under the IDE options dialog.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, October 11, 2017 1:37 PM
  • Hi Superdevel,

    According to your description, I am not sure what problem you have, if you want to know if convertint a string to Boolean, I suggest you to use Boolean.TryParse Method. If I missing your mean, please give more info about your issue.

    https://msdn.microsoft.com/en-us/library/system.boolean.tryparse(v=vs.110).aspx

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 12, 2017 8:49 AM
  • VB has a certain way of getting things done. As a practice, type safety is encouraged when coding in VB. However, I can't understand what type of error you are having when converting an Integer to a Boolean. VB treats 0 as a false value and any none zero value as true.

    Option Explicit On
    
    Module Module1
    
        Sub Main()
            Dim a1 As Integer = 10
            Dim a2 As Boolean
    
            a2 = a1
            Console.WriteLine(a2.ToString)
    
    
            If a1 Then
                MsgBox("An integer can be converted to a boolean.")
    
            End If
    
        End Sub
    
    End Module





    Thursday, October 12, 2017 9:35 AM
  • Hi Superdevil,

    It would be high time for you to provide your code, if you want to solve the issue.

    Regards,


    Ashidacchi

    Thursday, October 12, 2017 11:16 AM
  • Is there a way to make visual basic check simple if conditions?

    What I mean is: any normal programming language would know that

    if 'xyz' returns true because 'xyz' is neither undefined nor an empty string, but VB is dumb enough to try to convert a string to boolean and fails to test the condition. Same thing with integers. Is there a way to make it test conditions normally?


    Friday, October 13, 2017 3:03 AM