Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Is there a test for an empty string that returns a boolean?

Hi

I'm passing the ElevCptn parameter as Optional:

Private Sub SetOptBut(FontBack As Boolean, PlnCptn As String, Optional
ElevCptn As String)

I want to test to see if the parameter has been included & Enable a
button if it is, & Disable it if is isn't, so I'd like it to return a
boolean.

Something like this:
CboElevation.Enabled = IsEmpty(ElevCptn)

Except that doesn't work because "" isn't an empty string.

Any help is appreciated

Ta
Dave F.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Is there a test for an empty string that returns a boolean?

On Fri, 09 May 2008 19:01:36 +0100, "Dave F." wrote:

Hi

I'm passing the ElevCptn parameter as Optional:

Private Sub SetOptBut(FontBack As Boolean, PlnCptn As String, Optional
ElevCptn As String)

I want to test to see if the parameter has been included & Enable a
button if it is, & Disable it if is isn't, so I'd like it to return a
boolean.

Something like this:
CboElevation.Enabled = IsEmpty(ElevCptn)

Except that doesn't work because "" isn't an empty string.

Any help is appreciated

Ta
Dave F.


how about Len(ElevCptn) = 0

--ron
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Is there a test for an empty string that returns a boolean?

You might look at IsMissing, however with your optional string I'd do it
like this

Private Sub SetOptBut(FontBack As Boolean, PlnCptn As String, _
Optional ElevCptn As String = "dummy")

bMissing = (ElevCptn = "dummy")

change "dummy" to something you know will never be passed

Regards,
Peter T


"Dave F." wrote in message
...
Hi

I'm passing the ElevCptn parameter as Optional:

Private Sub SetOptBut(FontBack As Boolean, PlnCptn As String, Optional
ElevCptn As String)

I want to test to see if the parameter has been included & Enable a
button if it is, & Disable it if is isn't, so I'd like it to return a
boolean.

Something like this:
CboElevation.Enabled = IsEmpty(ElevCptn)

Except that doesn't work because "" isn't an empty string.

Any help is appreciated

Ta
Dave F.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default Is there a test for an empty string that returns a boolean?

"Dave F." wrote in message
...

CboElevation.Enabled = IsEmpty(ElevCptn)


IsEmpty() isn't for strings, it's for Variants that haven't been assigned a
value. IsMissing(), as Peter suggested, is only for Variants. Optional
parameters typed as anything besides a Variant that aren't given a default
value in the procedure declaration get the default for the data type, so
numbers get 0 and strings get empty strings. Therefore you should simply be
able to test ElevCptn against "". Did you ever try that?

Ron's Len() suggestion is also valid.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Is there a test for an empty string that returns a boolean?

CboElevation.Enabled = IsEmpty(ElevCptn)

IsEmpty() isn't for strings, it's for Variants that haven't been assigned
a value. IsMissing(), as Peter suggested, is only for Variants. Optional
parameters typed as anything besides a Variant that aren't given a default
value in the procedure declaration get the default for the data type, so
numbers get 0 and strings get empty strings. Therefore you should simply
be able to test ElevCptn against "". Did you ever try that?

Ron's Len() suggestion is also valid.


And to follow up on Jeff's comment, while this may look "simpler"...

CboElevation.Enabled = (ElevCptn = "")

due to the way VB stores Strings, this is marginally faster...

CboElevation.Enabled = (Len(ElevCptn) = 0)

It wouldn't matter in a single or few usage situation; but, inside a very
large loop, it could make a difference.

Rick



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Is there a test for an empty string that returns a boolean?

I guess it depends on the OP's overall objective "I want to test to see if
the parameter has been included" and whether "included" means passed or
empty. The way I see it Len(arg) merely tests if the argument contains any
characters, or indeed is an empty string. It doesn't prove either way if the
optional argument was passed or 'is missing'.

Sub test()
Dim a As String, b As String
Call foo(a, b)
MsgBox a & vbCr & b, , "neither arg was missing"

End Sub

Function foo(Optional arg1 As String, Optional arg2 As String = "dummy")

If Len(s1) = 0 Then
arg1 = "arg1 was missing"
Else
arg1 = "return string 1"
End If

If s2 = "dummy" Then
arg2 = "arg2 was missing"
Else
arg2 = "return string 2"
End If

End Function

Regards,
Peter T

"Rick Rothstein (MVP - VB)" wrote in
message ...
CboElevation.Enabled = IsEmpty(ElevCptn)


IsEmpty() isn't for strings, it's for Variants that haven't been

assigned
a value. IsMissing(), as Peter suggested, is only for Variants. Optional
parameters typed as anything besides a Variant that aren't given a

default
value in the procedure declaration get the default for the data type, so
numbers get 0 and strings get empty strings. Therefore you should simply
be able to test ElevCptn against "". Did you ever try that?

Ron's Len() suggestion is also valid.


And to follow up on Jeff's comment, while this may look "simpler"...

CboElevation.Enabled = (ElevCptn = "")

due to the way VB stores Strings, this is marginally faster...

CboElevation.Enabled = (Len(ElevCptn) = 0)

It wouldn't matter in a single or few usage situation; but, inside a very
large loop, it could make a difference.

Rick



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Is there a test for an empty string that returns a boolean?

Peter T wrote:
I guess it depends on the OP's overall objective "I want to test to see if
the parameter has been included" and whether "included" means passed or
empty. The way I see it Len(arg) merely tests if the argument contains any
characters, or indeed is an empty string. It doesn't prove either way if the
optional argument was passed or 'is missing'.


Hi Peter

You're tight. It appears that even if it isn't passed, the variable is stored as an empty string ("")
For my routine Len(arg) is what I want. I assumed its results could be used within an If Then statement.
I've learnt something new today.

Thank You to all who replied.

Ta
Dave F.
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
sumproduct with boolean criteria returns unexpected 0 goss[_2_] Excel Discussion (Misc queries) 6 October 28th 07 04:33 PM
Test for empty row MahrYon Excel Programming 2 October 23rd 05 02:16 PM
When I enter a number in an empty cel, de cel returns the value d. Paul KdN Excel Discussion (Misc queries) 2 January 13th 05 09:23 AM
Empty cell and a the empty String JE McGimpsey Excel Programming 0 September 13th 04 04:12 PM
Is Array Empty | Boolean Answer verizon Excel Programming 2 May 4th 04 02:53 PM


All times are GMT +1. The time now is 11:31 PM.

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"