Can you help convert IF Statement to a Select Case Statement? 
Author Message
 Can you help convert IF Statement to a Select Case Statement?

Thanks! Been trying but my stings are alwasy empty. No errors, just no data.

Dim cCDORecips As Recipients
Dim oCDORecip As Recipient
Dim sRecipTo As String
Dim sRecipCc As String
Dim sRecipBcc As String

For Each oCDORecip In cCDORecips
    If oCDORecip.Type = 1 Then
       sRecipTo = sRecipTo & "; " & oCDORecip.Name
    ElseIf oCDORecip.Type = 2 Then
       sRecipCc = sRecipCc & "; " & oCDORecip.Name
    ElseIf oCDORecip.Type = 3 Then
       sRecipBcc = sRecipBcc & "; " & oCDORecip.Name
    End If
Next



Thu, 12 Aug 2004 05:21:24 GMT  
 Can you help convert IF Statement to a Select Case Statement?
You use Select Case like this:

Select Case MyVariable
    Case 1
        ' Do something when variable is 1
    Case 2
        ' Do something when variable is 2
    Case Else
        ' Do something when variable is neither 1, nor 2
End Select

Jason Bouzane

Quote:

> Thanks! Been trying but my stings are alwasy empty. No errors, just no data.

> Dim cCDORecips As Recipients
> Dim oCDORecip As Recipient
> Dim sRecipTo As String
> Dim sRecipCc As String
> Dim sRecipBcc As String

> For Each oCDORecip In cCDORecips
>     If oCDORecip.Type = 1 Then
>        sRecipTo = sRecipTo & "; " & oCDORecip.Name
>     ElseIf oCDORecip.Type = 2 Then
>        sRecipCc = sRecipCc & "; " & oCDORecip.Name
>     ElseIf oCDORecip.Type = 3 Then
>        sRecipBcc = sRecipBcc & "; " & oCDORecip.Name
>     End If
> Next



Thu, 12 Aug 2004 10:09:44 GMT  
 Can you help convert IF Statement to a Select Case Statement?

Quote:

> Thanks! Been trying but my stings are alwasy empty. No errors, just no data.

> Dim cCDORecips As Recipients
> Dim oCDORecip As Recipient
> Dim sRecipTo As String
> Dim sRecipCc As String
> Dim sRecipBcc As String

> For Each oCDORecip In cCDORecips
>     If oCDORecip.Type = 1 Then
>        sRecipTo = sRecipTo & "; " & oCDORecip.Name
>     ElseIf oCDORecip.Type = 2 Then
>        sRecipCc = sRecipCc & "; " & oCDORecip.Name
>     ElseIf oCDORecip.Type = 3 Then
>        sRecipBcc = sRecipBcc & "; " & oCDORecip.Name
>     End If
> Next

Hy, try this...not tested...

Select Case oCDORecip.Type  
   Case 1
........code
   Case 2
........code
................
End Select
'where
Type oCDORecip
    Type As Integer ' long or byte
    ......
End Type



Thu, 12 Aug 2004 10:48:40 GMT  
 Can you help convert IF Statement to a Select Case Statement?
I forget...
you need to assign a value at run-time to oCDORecip.Type, before you
make a selection ...
ex.: oCDORecip.Type =1

Quote:
>......... code ...........
> For Each oCDORecip In cCDORecips
>     If oCDORecip.Type = 1 Then
>        sRecipTo = sRecipTo & "; " & oCDORecip.Name
>     ElseIf oCDORecip.Type = 2 Then
>        sRecipCc = sRecipCc & "; " & oCDORecip.Name
>     ElseIf oCDORecip.Type = 3 Then
>        sRecipBcc = sRecipBcc & "; " & oCDORecip.Name
>     End If
> Next

Djinn


Thu, 12 Aug 2004 11:22:00 GMT  
 Can you help convert IF Statement to a Select Case Statement?
How about this alternative that removes the need for both the If Then and
Select Case statements ...

You have a collection of names, and each name has a corresponding numeric
value of 1, 2 or 3 that represents the recipient type.

If you used an array instead of individual strings for sRecipTo, sRecipCC
and sRecipBCC, you could assign the values of cCDORecips.name directly to
the correct string in this array by using oCDORecip.Type as the index.  For
example ...

'1: define constants representing the recipient types
'    to make your code more understandable

const recipTO as long = 1
const recipCC as long = 2
const recipBCC as long = 3

'2: define a string array to hold the names
'    assigned as To, CC or BCC. i.e. ...
'    sRecip(recipTO)  holds the To names
'    sRecip(recipCC )  holds the CC names
'    sRecip(recipBCC)  holds the BCC names

Dim sRecip(recipTO To recipCC) As String
'same as dimming sRecip(1 to 3)

'3: Now, in the For Each loop, drop the if then statement
'    and assign each name directly to the appropriate
'   array string ...

For Each oCDORecip In cCDORecips

   sRecip( oCDORecip.Type) = sRecip( oCDORecip.Type) & oCDORecip.Name  & ";
"

Next

That's it. Later in your code, when assigning the string to the outgoing To,
CC and BCC fields of the email, just assign the correct array member ...

  ... to = sRecip(recipTO)
  ... cc = sRecip(recipCC)
  ... bcc = sRecip(recipBCC)

Where you have no members for a particular output field (ie no BCC members),
the string will be "" and can be assigned without requiring any additional
If Then checks to see whether the string contains information.

--

Randy Birch
MVP Visual Basic

http://www.mvps.org/vbnet/

Please respond only to the newsgroups so all can benefit.

*** If you call the Sleep API in Bill's latest, will it have .NET dreams?
***


Quote:
> Thanks! Been trying but my stings are alwasy empty. No errors, just no
data.

> Dim cCDORecips As Recipients
> Dim oCDORecip As Recipient
> Dim sRecipTo As String
> Dim sRecipCc As String
> Dim sRecipBcc As String

> For Each oCDORecip In cCDORecips
>     If oCDORecip.Type = 1 Then
>        sRecipTo = sRecipTo & "; " & oCDORecip.Name
>     ElseIf oCDORecip.Type = 2 Then
>        sRecipCc = sRecipCc & "; " & oCDORecip.Name
>     ElseIf oCDORecip.Type = 3 Then
>        sRecipBcc = sRecipBcc & "; " & oCDORecip.Name
>     End If
> Next



Fri, 13 Aug 2004 00:20:29 GMT  
 Can you help convert IF Statement to a Select Case Statement?
Thanks for the help, I must admit that I am a newbie and need some
more help.
I don't think I understand when you say assign a value at run-time,
the cSafeCDORecips collection can contain multiple oSafeCDORecip, of
which they can be type 1,2 or 3.  They can be only type 3 for example.

Anyway this is my latest attempt, which of course dosen't work.  Will
keep hacking at it in the mean time though.
Dim cSafeCDORecips As Redemption.SafeRecipients
Dim oSafeCDORecip As Redemption.SafeRecipient

For Each oSafeCDORecip In cSafeCDORecips
        Select Case cSafeCDORecips.Type
            Case 1
                sRecipTo = sRecipTo & "; " & oSafeCDORecip.Name
            Case 2
                sRecipCc = sRecipCc & "; " & oSafeCDORecip.Name
            Case 3
                sRecipBcc = sRecipBcc & "; " & oSafeCDORecip.Name
        End Select
        Next

Original Code:
For Each oSafeCDORecip In cSafeCDORecips
    If oSafeCDORecip.Type = 1 Then
       sRecipTo = sRecipTo & "; " & oSafeCDORecip.Name
    ElseIf oSafeCDORecip.Type = 2 Then
       sRecipCc = sRecipCc & "; " & oSafeCDORecip.Name
    ElseIf oSafeCDORecip.Type = 3 Then
       sRecipBcc = sRecipBcc & "; " & oSafeCDORecip.Name
    End If
    Next

    AddRecipsViaCDO = sRecipTo & vbNewLine & _
                      sRecipCc & vbNewLine & _
                      sRecipBcc

End Of Post:
*****************************************************************************

Quote:

> I forget...
> you need to assign a value at run-time to oCDORecip.Type, before you
> make a selection ...
> ex.: oCDORecip.Type =1
> >......... code ...........
> > For Each oCDORecip In cCDORecips
> >     If oCDORecip.Type = 1 Then
> >        sRecipTo = sRecipTo & "; " & oCDORecip.Name
> >     ElseIf oCDORecip.Type = 2 Then
> >        sRecipCc = sRecipCc & "; " & oCDORecip.Name
> >     ElseIf oCDORecip.Type = 3 Then
> >        sRecipBcc = sRecipBcc & "; " & oCDORecip.Name
> >     End If
> > Next

> Djinn



Fri, 13 Aug 2004 02:02:07 GMT  
 Can you help convert IF Statement to a Select Case Statement?
Randy, thanks again for your paitence and an in depth explanation.
It all works with one small problem.  It always adds a ";" to the end
of each string in the array.  I tried the following code, it got an
error code #9, Subscript out of range.

If Len(sRecip(1 - 3)) > 0 Then
   sRecip(1 - 3) = Left$(sRecip(1 - 3), Len(sRecip(1 - 3)) - 1)
End If

Quote:

> How about this alternative that removes the need for both the If Then and
> Select Case statements ...

> You have a collection of names, and each name has a corresponding numeric
> value of 1, 2 or 3 that represents the recipient type.

> If you used an array instead of individual strings for sRecipTo, sRecipCC
> and sRecipBCC, you could assign the values of cCDORecips.name directly to
> the correct string in this array by using oCDORecip.Type as the index.  For
> example ...

> '1: define constants representing the recipient types
> '    to make your code more understandable

> const recipTO as long = 1
> const recipCC as long = 2
> const recipBCC as long = 3

> '2: define a string array to hold the names
> '    assigned as To, CC or BCC. i.e. ...
> '    sRecip(recipTO)  holds the To names
> '    sRecip(recipCC )  holds the CC names
> '    sRecip(recipBCC)  holds the BCC names

> Dim sRecip(recipTO To recipCC) As String
> 'same as dimming sRecip(1 to 3)

> '3: Now, in the For Each loop, drop the if then statement
> '    and assign each name directly to the appropriate
> '   array string ...

> For Each oCDORecip In cCDORecips

>    sRecip( oCDORecip.Type) = sRecip( oCDORecip.Type) & oCDORecip.Name  & ";
> "

> Next

> That's it. Later in your code, when assigning the string to the outgoing To,
> CC and BCC fields of the email, just assign the correct array member ...

>   ... to = sRecip(recipTO)
>   ... cc = sRecip(recipCC)
>   ... bcc = sRecip(recipBCC)

> Where you have no members for a particular output field (ie no BCC members),
> the string will be "" and can be assigned without requiring any additional
> If Then checks to see whether the string contains information.

> --

> Randy Birch
> MVP Visual Basic

> http://www.mvps.org/vbnet/

> Please respond only to the newsgroups so all can benefit.

> *** If you call the Sleep API in Bill's latest, will it have .NET dreams?
> ***



> > Thanks! Been trying but my stings are alwasy empty. No errors, just no
>  data.

> > Dim cCDORecips As Recipients
> > Dim oCDORecip As Recipient
> > Dim sRecipTo As String
> > Dim sRecipCc As String
> > Dim sRecipBcc As String

> > For Each oCDORecip In cCDORecips
> >     If oCDORecip.Type = 1 Then
> >        sRecipTo = sRecipTo & "; " & oCDORecip.Name
> >     ElseIf oCDORecip.Type = 2 Then
> >        sRecipCc = sRecipCc & "; " & oCDORecip.Name
> >     ElseIf oCDORecip.Type = 3 Then
> >        sRecipBcc = sRecipBcc & "; " & oCDORecip.Name
> >     End If
> > Next



Fri, 13 Aug 2004 07:32:41 GMT  
 Can you help convert IF Statement to a Select Case Statement?
If you don't want any semicolons, remove the

  & ";"

from the end of the code that builds the string. If you only want to not
have a semicolon after the last item, you have to remove the rightmost
character individually ...

for x = 1 to 3

If Len(sRecip(x)) > 0 then
  sRecip(x) = Left$(sRecip(x), Len(sRecip(x)) - 1)
End If

However, Outlook itself doesn't complain if the email addresses entered all
have terminating semi-colons, so this extra removal step may be unnecessary.

--

Randy Birch
MVP Visual Basic

http://www.mvps.org/vbnet/

Please respond only to the newsgroups so all can benefit.

*** If you call the Sleep API in Bill's latest, will it have .NET dreams?
***


Quote:
> Randy, thanks again for your paitence and an in depth explanation.
> It all works with one small problem.  It always adds a ";" to the end
> of each string in the array.  I tried the following code, it got an
> error code #9, Subscript out of range.

> If Len(sRecip(1 - 3)) > 0 Then
>    sRecip(1 - 3) = Left$(sRecip(1 - 3), Len(sRecip(1 - 3)) - 1)
> End If




Quote:
> > How about this alternative that removes the need for both the If Then
and
> > Select Case statements ...

> > You have a collection of names, and each name has a corresponding
numeric
> > value of 1, 2 or 3 that represents the recipient type.

> > If you used an array instead of individual strings for sRecipTo,
sRecipCC
> > and sRecipBCC, you could assign the values of cCDORecips.name directly
to
> > the correct string in this array by using oCDORecip.Type as the index.
For
> > example ...

> > '1: define constants representing the recipient types
> > '    to make your code more understandable

> > const recipTO as long = 1
> > const recipCC as long = 2
> > const recipBCC as long = 3

> > '2: define a string array to hold the names
> > '    assigned as To, CC or BCC. i.e. ...
> > '    sRecip(recipTO)  holds the To names
> > '    sRecip(recipCC )  holds the CC names
> > '    sRecip(recipBCC)  holds the BCC names

> > Dim sRecip(recipTO To recipCC) As String
> > 'same as dimming sRecip(1 to 3)

> > '3: Now, in the For Each loop, drop the if then statement
> > '    and assign each name directly to the appropriate
> > '   array string ...

> > For Each oCDORecip In cCDORecips

> >    sRecip( oCDORecip.Type) = sRecip( oCDORecip.Type) & oCDORecip.Name  &
";
> > "

> > Next

> > That's it. Later in your code, when assigning the string to the outgoing
To,
> > CC and BCC fields of the email, just assign the correct array member ...

> >   ... to = sRecip(recipTO)
> >   ... cc = sRecip(recipCC)
> >   ... bcc = sRecip(recipBCC)

> > Where you have no members for a particular output field (ie no BCC
members),
> > the string will be "" and can be assigned without requiring any
additional
> > If Then checks to see whether the string contains information.

> > --

> > Randy Birch
> > MVP Visual Basic

> > http://www.mvps.org/vbnet/

> > Please respond only to the newsgroups so all can benefit.

> > *** If you call the Sleep API in Bill's latest, will it have .NET
dreams?
> > ***



> > > Thanks! Been trying but my stings are alwasy empty. No errors, just no
> >  data.

> > > Dim cCDORecips As Recipients
> > > Dim oCDORecip As Recipient
> > > Dim sRecipTo As String
> > > Dim sRecipCc As String
> > > Dim sRecipBcc As String

> > > For Each oCDORecip In cCDORecips
> > >     If oCDORecip.Type = 1 Then
> > >        sRecipTo = sRecipTo & "; " & oCDORecip.Name
> > >     ElseIf oCDORecip.Type = 2 Then
> > >        sRecipCc = sRecipCc & "; " & oCDORecip.Name
> > >     ElseIf oCDORecip.Type = 3 Then
> > >        sRecipBcc = sRecipBcc & "; " & oCDORecip.Name
> > >     End If
> > > Next



Fri, 13 Aug 2004 07:41:13 GMT  
 Can you help convert IF Statement to a Select Case Statement?
Thanks Randy! I was also thinking the same thing, for this reason. If
I leave the semicolon there then if the user adds more addresses via
the address book they'll be seperated correctly.

Or

Remove the semicolan for asthetics and clear the recipients text boxes
everytime they add more.  So gonna leave it!

Quote:

> If you don't want any semicolons, remove the

>   & ";"

> from the end of the code that builds the string. If you only want to not
> have a semicolon after the last item, you have to remove the rightmost
> character individually ...

> for x = 1 to 3

> If Len(sRecip(x)) > 0 then
>   sRecip(x) = Left$(sRecip(x), Len(sRecip(x)) - 1)
> End If

> However, Outlook itself doesn't complain if the email addresses entered all
> have terminating semi-colons, so this extra removal step may be unnecessary.

> --

> Randy Birch
> MVP Visual Basic

> http://www.mvps.org/vbnet/

> Please respond only to the newsgroups so all can benefit.

> *** If you call the Sleep API in Bill's latest, will it have .NET dreams?
> ***



> > Randy, thanks again for your paitence and an in depth explanation.
> > It all works with one small problem.  It always adds a ";" to the end
> > of each string in the array.  I tried the following code, it got an
> > error code #9, Subscript out of range.

> > If Len(sRecip(1 - 3)) > 0 Then
> >    sRecip(1 - 3) = Left$(sRecip(1 - 3), Len(sRecip(1 - 3)) - 1)
> > End If



> > > How about this alternative that removes the need for both the If Then
>  and
> > > Select Case statements ...

> > > You have a collection of names, and each name has a corresponding
>  numeric
> > > value of 1, 2 or 3 that represents the recipient type.

> > > If you used an array instead of individual strings for sRecipTo,
>  sRecipCC
> > > and sRecipBCC, you could assign the values of cCDORecips.name directly
>  to
> > > the correct string in this array by using oCDORecip.Type as the index.
>  For
> > > example ...

> > > '1: define constants representing the recipient types
> > > '    to make your code more understandable

> > > const recipTO as long = 1
> > > const recipCC as long = 2
> > > const recipBCC as long = 3

> > > '2: define a string array to hold the names
> > > '    assigned as To, CC or BCC. i.e. ...
> > > '    sRecip(recipTO)  holds the To names
> > > '    sRecip(recipCC )  holds the CC names
> > > '    sRecip(recipBCC)  holds the BCC names

> > > Dim sRecip(recipTO To recipCC) As String
> > > 'same as dimming sRecip(1 to 3)

> > > '3: Now, in the For Each loop, drop the if then statement
> > > '    and assign each name directly to the appropriate
> > > '   array string ...

> > > For Each oCDORecip In cCDORecips

> > >    sRecip( oCDORecip.Type) = sRecip( oCDORecip.Type) & oCDORecip.Name  &
>  ";
> > > "

> > > Next

> > > That's it. Later in your code, when assigning the string to the outgoing
>  To,
> > > CC and BCC fields of the email, just assign the correct array member ...

> > >   ... to = sRecip(recipTO)
> > >   ... cc = sRecip(recipCC)
> > >   ... bcc = sRecip(recipBCC)

> > > Where you have no members for a particular output field (ie no BCC
>  members),
> > > the string will be "" and can be assigned without requiring any
>  additional
> > > If Then checks to see whether the string contains information.

> > > --

> > > Randy Birch
> > > MVP Visual Basic

> > > http://www.mvps.org/vbnet/

> > > Please respond only to the newsgroups so all can benefit.

> > > *** If you call the Sleep API in Bill's latest, will it have .NET
>  dreams?
> > > ***



> > > > Thanks! Been trying but my stings are alwasy empty. No errors, just no
>  data.

> > > > Dim cCDORecips As Recipients
> > > > Dim oCDORecip As Recipient
> > > > Dim sRecipTo As String
> > > > Dim sRecipCc As String
> > > > Dim sRecipBcc As String

> > > > For Each oCDORecip In cCDORecips
> > > >     If oCDORecip.Type = 1 Then
> > > >        sRecipTo = sRecipTo & "; " & oCDORecip.Name
> > > >     ElseIf oCDORecip.Type = 2 Then
> > > >        sRecipCc = sRecipCc & "; " & oCDORecip.Name
> > > >     ElseIf oCDORecip.Type = 3 Then
> > > >        sRecipBcc = sRecipBcc & "; " & oCDORecip.Name
> > > >     End If
> > > > Next



Fri, 13 Aug 2004 11:02:38 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Select Case Statement Help

2. Can this be converted to a CASE Statement?

3. Select Case Statement

4. Criteria for select case statements

5. Embedded or nested Select Case statements

6. Using a wildcard string comparison in a select case statement

7. Select Case statement is driving me mad!

8. Can you nest Select Case statements?

9. If...Then...Else or Select Case Statement

10. Select Case Statement

11. If statement in select case problem

12. Select Case Statement

 

 
Powered by phpBB® Forum Software