CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Jun 2003
    Posts
    41

    Why does this compile?

    Hello, If I have

    ----------------------------

    Public Sub Test(i As Integer)

    End Sub

    Load()

    Test("Hello")

    End Sub

    -----------------


    Why does the program compile with no errors? Shouldnt vb see that the argument passed to Test is not an integer?

    Thanks

  2. #2
    Join Date
    Aug 2001
    Posts
    1,447
    clearly the code as you posted it will not compile as you have two end subs with only one sub start. Post your actual code.
    phinds
    vs2008, 3.5SP1 Version 9.0.21022.8 RTM

  3. #3
    Join Date
    Jun 2003
    Posts
    41
    My question is, why does the code compile.
    Shouldn't the compiler see that you are passing a
    string as a parameter instead of an integer.


    Public Sub Test(i As Integer)

    End Sub

    Private Sub Form_Load()

    Test("Hello")

    End Sub

  4. #4
    Join Date
    Jun 2001
    Location
    Mi
    Posts
    1,249
    That applies only when you pass a variable (i.e. a variable of type string)... You are passing a pointer to a local stack and VB renders the value into an integer ...

  5. #5
    Join Date
    Jun 2003
    Posts
    41
    Hi,

    I changed



    Test("Hello") To

    Dim t As String
    t = "Hello"

    Test(t)


    It still compiles.

  6. #6
    Join Date
    Jul 2003
    Location
    I'm Here!
    Posts
    31
    I don't think the compiler cares what you are passing a function. All it cares about is IF you are passing one or not and whether or not it requires one.

    This is also true in your code.
    Try a numeric variable that your are trying to use as alpha and create a calculation with it but don't run it to get a runtime error. Instead, just compile it and you'll see what I mean.

    HTH

  7. #7
    Join Date
    Jun 2003
    Posts
    41
    Hi,

    I see that it doesnt care what type the argument is. I want to know if this is something that can be changed by choosing some sort of option in VB.

    For example, what is the point of having types in the arguments of a function if they are not used to help the coder avoid passing incorrect things to functions before it is run.

    Can someone give answer this.

  8. #8
    Join Date
    Jun 2001
    Location
    Mi
    Posts
    1,249
    You have option explicit turned on?

  9. #9
    Join Date
    Jul 2003
    Location
    I'm Here!
    Posts
    31
    You have to remember how a function works.
    Until you run it and pass it something then how is it going to know that what you are passing is valid or not? It's just code waiting for an action. There is no cross-referencing on arguments passed because on compile, you're not passing anything to it.

    I have to ask this though: Why are you compiling something you haven't tested to begin with?

  10. #10
    Join Date
    Jun 2003
    Posts
    41
    I have option exlicit.

    I come from a c++ background.

    The reason you want this to happen confus is because it makes more reliable and less buggy code.

    What if I had the following code.

    ------------------

    Public Enum Colour
    Red = 1
    Blue = 2
    End Enum

    Public Sub Test(col As Colour)


    End Sub

    Public Sub Form_Load()

    Test(4)

    End Sub

    ---------------

    The compiler should see that 4 is not of type, Colour and it
    should not compile. It makes the debugging easier.

    My simple question is, does anyone know if vb can do this type of checking.






    -----------------

  11. #11
    Join Date
    Jul 2003
    Location
    I'm Here!
    Posts
    31
    There's a price to pay for ease of programming when using VB and you just found one of many in comparison to C++

    Interpretive languages like VB, REXX, etc. have many flaws in comparison with more rigid languages like C++

    C++ is not as forgiving as VB. It's hard to crash a user's PC with VB code unless you really screwed up badly. No so with C++

    Not to mention the learning curb

  12. #12
    Join Date
    Jun 2003
    Posts
    41
    I wonder then what is the point of using enums in the
    arguments of a function

  13. #13
    Join Date
    Nov 2002
    Posts
    86
    Originally posted by duh
    My simple question is, does anyone know if vb can do this type of checking.

    The reason you want this to happen confus is because it makes more reliable and less buggy code.
    Confucius is right, VB can test this. Run the program? It would make no sense to write a program, then not test it in the IDE and expect to have no bugs...

  14. #14
    Join Date
    Nov 2002
    Posts
    86
    Ooops i jumped the gun, didn't see your last replys...

  15. #15
    Join Date
    Nov 2001
    Location
    London, UK
    Posts
    275
    Oops!
    Mine works
    I mean it doesnt compile on my computer

Page 1 of 2 12 LastLast

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