-
August 7th, 2003, 12:38 PM
#1
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
-
August 7th, 2003, 01:21 PM
#2
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
-
August 7th, 2003, 01:34 PM
#3
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
-
August 7th, 2003, 01:39 PM
#4
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 ...
-
August 7th, 2003, 01:52 PM
#5
Hi,
I changed
Test("Hello") To
Dim t As String
t = "Hello"
Test(t)
It still compiles.
-
August 7th, 2003, 02:11 PM
#6
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
-
August 7th, 2003, 02:15 PM
#7
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.
-
August 7th, 2003, 02:19 PM
#8
You have option explicit turned on?
-
August 7th, 2003, 02:22 PM
#9
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?
-
August 7th, 2003, 02:34 PM
#10
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.
-----------------
-
August 7th, 2003, 02:50 PM
#11
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
-
August 7th, 2003, 02:57 PM
#12
I wonder then what is the point of using enums in the
arguments of a function
-
August 7th, 2003, 03:56 PM
#13
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...
-
August 7th, 2003, 03:59 PM
#14
Ooops i jumped the gun, didn't see your last replys...
-
August 8th, 2003, 04:34 AM
#15
Oops!
Mine works
I mean it doesnt compile on my computer
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|