Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
requiring parameters to be of a certain type
Can I force a Function to take only a String or other data type as an
argument? In Java one had to do this . . . I'm worried if I don't specify, I might get weird errors if I accidentally pass the wrong type of information to a Function or called Sub procedure. |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
requiring parameters to be of a certain type
Just declare the argument type in the function declaration.
Function foo(f as String) As String Function moo(m as Long) As Integer Function noo(n as Single, m as Double) As Collection Function poo(p as Variant) As Workbook If you leave off the type, the type defaults to Variant, which will accept anything, although at a some cost of overhead and type checking. You should understand that VB coerces numerics types to strings and strings that appear numeric to numeric types according to the context (argument type or operator). In other words foo(5) would work as well as foo("5") as far as VB is concerned. If you do or do not want numeric types, you can use IsNumeric to differentiate. You can also check variant arguments for their subtype (VarType). See help regarding the keywords/functions/operators Is, IsNumeric(), IsObject(), TypeName, TypeOf, VarType. -- Bob Kilmer "Jamie Martin" wrote in message ... Can I force a Function to take only a String or other data type as an argument? In Java one had to do this . . . I'm worried if I don't specify, I might get weird errors if I accidentally pass the wrong type of information to a Function or called Sub procedure. |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
requiring parameters to be of a certain type
Awesome. Why do I need to say "As String" twice?
"Bob Kilmer" wrote in message ... Just declare the argument type in the function declaration. Function foo(f as String) As String Function moo(m as Long) As Integer Function noo(n as Single, m as Double) As Collection Function poo(p as Variant) As Workbook If you leave off the type, the type defaults to Variant, which will accept anything, although at a some cost of overhead and type checking. You should understand that VB coerces numerics types to strings and strings that appear numeric to numeric types according to the context (argument type or operator). In other words foo(5) would work as well as foo("5") as far as VB is concerned. If you do or do not want numeric types, you can use IsNumeric to differentiate. You can also check variant arguments for their subtype (VarType). See help regarding the keywords/functions/operators Is, IsNumeric(), IsObject(), TypeName, TypeOf, VarType. -- Bob Kilmer "Jamie Martin" wrote in message ... Can I force a Function to take only a String or other data type as an argument? In Java one had to do this . . . I'm worried if I don't specify, I might get weird errors if I accidentally pass the wrong type of information to a Function or called Sub procedure. |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
requiring parameters to be of a certain type
The first declares the argument (f) data type, the next declares the
data type returned by the Function (default is Variant) In article , "Jamie Martin" wrote: Awesome. Why do I need to say "As String" twice? "Bob Kilmer" wrote in message ... Just declare the argument type in the function declaration. Function foo(f as String) As String |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
requiring parameters to be of a certain type
In the case of a Sub (which does not itself return a value) the second As
<type is not used and would be a syntax error. (Subs can modify arguments passed ByRef (the default) and result in a modified argument value for the caller but they are not allowed to return values that can be assigned like a function.) -- Bob Kilmer "J.E. McGimpsey" wrote in message ... The first declares the argument (f) data type, the next declares the data type returned by the Function (default is Variant) In article , "Jamie Martin" wrote: Awesome. Why do I need to say "As String" twice? "Bob Kilmer" wrote in message ... Just declare the argument type in the function declaration. Function foo(f as String) As String |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
requiring column entries | Excel Discussion (Misc queries) | |||
If Statement requiring explanation | Excel Discussion (Misc queries) | |||
Sum Problem requiring help | Excel Worksheet Functions | |||
Requiring data in certain cells | Excel Worksheet Functions | |||
requiring a field | Excel Worksheet Functions |