469,271 Members | 852 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Control Array

dfg
Hello. I have a control Array of option buttons. I'd like to use them
in a Case statement, but I can't get it to work. I can only seem to get
it working if I use an if-then-else structure, like so:

If optFinishType(0) Then
TotalAccess = TotalAccess + mcurStandard
ElseIf optFinishType(1) Then
TotalAccess = TotalAccess + mcurPearl
ElseIf optFinishType(2) Then
TotalAccess = TotalAccess + mcurDetailing
End If

This structure is currently in the click-event of a command button, but
I was wondering if I can place it in the click event of the option
button. Do you think that would be better?

Jul 17 '05 #1
9 4925
> Hello. I have a control Array of option buttons. I'd like to use them
in a Case statement, but I can't get it to work. I can only seem to get
it working if I use an if-then-else structure, like so:

If optFinishType(0) Then
TotalAccess = TotalAccess + mcurStandard
ElseIf optFinishType(1) Then
TotalAccess = TotalAccess + mcurPearl
ElseIf optFinishType(2) Then
TotalAccess = TotalAccess + mcurDetailing
End If


If you have it working with an If-Then structure, why do you feel you must
change it to a Select Case structure? Anyway, this should work

Select Case True
Case optFinishType(0).Value
TotalAccess = TotalAccess + mcurStandard
Case optFinishType(1).Value
TotalAccess = TotalAccess + mcurPearl
Case optFinishType(2).Value
TotalAccess = TotalAccess + mcurDetailing
End Select

I would suggest that you not rely on VB supplying the default property of
the OptionButton (or any other control, for that matter)... six months from
now, you control's name **might** look like the name of an ordinary
variable. You will find it easier to read your code later on if you specify
all properties for your controls.

Rick - MVP
Jul 17 '05 #2
"dfg" <df*@dfg.net> wrote in message
news:YL7jb.103882$pl3.101496@pd7tw3no...
Hello. I have a control Array of option buttons. I'd like to use them
in a Case statement, but I can't get it to work. I can only seem to get
it working if I use an if-then-else structure, like so:

If optFinishType(0) Then
TotalAccess = TotalAccess + mcurStandard
ElseIf optFinishType(1) Then
TotalAccess = TotalAccess + mcurPearl
ElseIf optFinishType(2) Then
TotalAccess = TotalAccess + mcurDetailing
End If

This structure is currently in the click-event of a command button, but
I was wondering if I can place it in the click event of the option
button. Do you think that would be better?


Given you have an array of option buttons,
I would use this code in the optFinishType_Click() event:

Private Sub optFinishType_Click(Index)
Select Case Index
Case = 0
TotalAccess=TotalAccess+mcurStandard
Case = 1
TotalAccess=TotalAccess+mcurPearl
Case = 2
TotalAccess=TotalAccess+mcurDetailing
End Select

End Sub
Jul 17 '05 #3
On Wed, 15 Oct 2003 05:10:08 -0400, "Rick Rothstein"
<ri************@NOSPAMcomcast.net> wrote:

<snip>

If you have it working with an If-Then structure, why do you feel you must
change it to a Select Case structure? Anyway, this should work

Select Case True
Case optFinishType(0).Value
TotalAccess = TotalAccess + mcurStandard
Case optFinishType(1).Value
TotalAccess = TotalAccess + mcurPearl
Case optFinishType(2).Value
TotalAccess = TotalAccess + mcurDetailing
End Select


I rather suspect that these options are mutually exclusive
Jul 17 '05 #4
> <snip>

If you have it working with an If-Then structure, why do you feel you mustchange it to a Select Case structure? Anyway, this should work

Select Case True
Case optFinishType(0).Value
TotalAccess = TotalAccess + mcurStandard
Case optFinishType(1).Value
TotalAccess = TotalAccess + mcurPearl
Case optFinishType(2).Value
TotalAccess = TotalAccess + mcurDetailing
End Select


I rather suspect that these options are mutually exclusive


I'm not sure I understand your comment... did you intend to post more?

Rick - MVP
Jul 17 '05 #5
On Wed, 15 Oct 2003 08:34:40 -0400, "Rick Rothstein"
<ri************@NOSPAMcomcast.net> wrote:
<snip>
>
>If you have it working with an If-Then structure, why do you feel youmust >change it to a Select Case structure? Anyway, this should work
>
>Select Case True
> Case optFinishType(0).Value
> TotalAccess = TotalAccess + mcurStandard
> Case optFinishType(1).Value
> TotalAccess = TotalAccess + mcurPearl
> Case optFinishType(2).Value
> TotalAccess = TotalAccess + mcurDetailing
>End Select


I rather suspect that these options are mutually exclusive


I'm not sure I understand your comment... did you intend to post more?


No, the nature of the 'mcur' Properties and the fact that it is an
array of Option Buttons suggests to me that only one result is
required.

I would use a small UserControl ....
Jul 17 '05 #6
> >> <snip>
>
>If you have it working with an If-Then structure, why do you feel you

must
>change it to a Select Case structure? Anyway, this should work
>
>Select Case True
> Case optFinishType(0).Value
> TotalAccess = TotalAccess + mcurStandard
> Case optFinishType(1).Value
> TotalAccess = TotalAccess + mcurPearl
> Case optFinishType(2).Value
> TotalAccess = TotalAccess + mcurDetailing
>End Select

I rather suspect that these options are mutually exclusive


I'm not sure I understand your comment... did you intend to post more?


No, the nature of the 'mcur' Properties and the fact that it is an
array of Option Buttons suggests to me that only one result is
required.

I would use a small UserControl ....


Perhaps... but if I had to guess from the original question, I'd say the OP
is not ready to start playing around with UserControls just yet. In any
event, the OP's question was pretty specific... where does my original reply
not respond to it? Or am I still missing the point why you posted your
original comment against my original response?

Rick - MVP
Jul 17 '05 #7
On Wed, 15 Oct 2003 10:00:50 -0400, "Rick Rothstein"
<ri************@NOSPAMcomcast.net> wrote:

<snip>

I would use a small UserControl ....


Perhaps... but if I had to guess from the original question, I'd say the OP
is not ready to start playing around with UserControls just yet. In any
event, the OP's question was pretty specific... where does my original reply
not respond to it? Or am I still missing the point why you posted your
original comment against my original response?


'mutually exclusive' was the bit I sat up and looked at

That sort of situation does not need a case statement
- unless it does something

.... perhaps an Enum

Realistically a spot of 'encapsulation' would help the OP
- and frankly I'm not up to that right now
Jul 17 '05 #8
If you're using option buttons, and want to detect which is selected from
inside a command button's click event, you have two options ... iterate
through the control array examining the value property (using a for-next
loop) until you determine the index selected, and use that index in your
select case statement to execute the appropriate code, or you can create a
private function that will return the selected option item index with which
you execute the select case. As 'air code' ...

'------using a loop------

dim cnt as long
dim selectedindex as long

for cnt = 0 to 2 'since you indicate 3 options
if option1(cnt).value = true then
selectedindex = cnt
exit for
end if
next cnt

select case selectedindex
case 0
case 1
etc ...
end select
'------using a function------
select case GetSelectedOptionIndex()
case 0
case 1
etc ...
end select

Private Function GetSelectedOptionIndex() As Long

'returns the selected item index from
'an option button array. Use in place
'of multiple If...Then statements!
'If your array contains more elements,
'just append them to the test condition,
'setting the multiplier to the button's
'negative -index.
GetSelectedOptionIndex = Option1(0).Value * 0 Or _
Option1(1).Value * -1 Or _
Option1(2).Value * -2
End Function
--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.
"dfg" <df*@dfg.net> wrote in message
news:YL7jb.103882$pl3.101496@pd7tw3no...
: Hello. I have a control Array of option buttons. I'd like to use them
: in a Case statement, but I can't get it to work. I can only seem to get
: it working if I use an if-then-else structure, like so:
:
: If optFinishType(0) Then
: TotalAccess = TotalAccess + mcurStandard
: ElseIf optFinishType(1) Then
: TotalAccess = TotalAccess + mcurPearl
: ElseIf optFinishType(2) Then
: TotalAccess = TotalAccess + mcurDetailing
: End If
:
: This structure is currently in the click-event of a command button, but
: I was wondering if I can place it in the click event of the option
: button. Do you think that would be better?
:
Jul 17 '05 #9
"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message
news:oO********************@comcast.com
Select Case True


heathen

Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by The Mess | last post: by
10 posts views Thread by James McGivney | last post: by
3 posts views Thread by Mark Broadbent | last post: by
6 posts views Thread by chxant | last post: by
2 posts views Thread by George | last post: by
14 posts views Thread by Jim Burns | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.