Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 280
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 493
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 280
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
requiring column entries Candace Excel Discussion (Misc queries) 1 January 10th 10 05:34 AM
If Statement requiring explanation CP Excel Discussion (Misc queries) 4 February 11th 09 02:56 PM
Sum Problem requiring help NoodNutt Excel Worksheet Functions 6 May 2nd 08 05:23 PM
Requiring data in certain cells jerminski73 Excel Worksheet Functions 2 November 21st 07 02:03 PM
requiring a field cherrynich Excel Worksheet Functions 1 July 10th 06 10:00 PM


All times are GMT +1. The time now is 07:32 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"