switch that uses radio button selection

T

Tim923

For an exercise, I must use a switch statement to assign values to
variables based on the 1 or 4 radio buttons selected. Can I do this
without using an extra if-else statement? Some code below:

if (makeOverRadioButton.Checked) {intServiceChoiceLocal = 1;}
else if (hairStylingRadioButton.Checked) {intServiceChoiceLocal = 2;}
else if (manicureRadioButton.Checked) {intServiceChoiceLocal = 3;}
else if (permanentMakeupRadioButton.Checked) {intServiceChoiceLocal
=4;}

switch (intServiceChoiceLocal)
{
case 1:
strServiceNameLocal = "Make Over";
decServiceCostLocal = decMAKEOVER_PRICE;
break;

(code snip, etc.)

-
http://mysite.verizon.net/vze8adrh/news.html (profile) --Tim923 My email is valid.
 
P

Peter Jausovec

Hi Tim,

You can put the radiobuttons in groupbox and use it like this :)
for (int i = 0; i < groupBox1.Controls.Count; i ++)
{
RadioButton current = (RadioButton)groupBox1.Controls ;

switch (current.Checked)
{
case true:
// your code here
break;
}
}

Regards,
Peter Jausovec
(http://blog.jausovec.net)
 
J

James Curran

Use standard HTML (non Server control) radiobuttons (<input type="radio"
.....>)

Assigned each the same ID and provide VALUE attributes (1,2,3,4)
<input type="Radio" id="Hair" Value="1">Make Over</input> etc

Read the value of that ID from the Request.Form collection.
int choice = Convert.ToInt32(Request.Form["Hair"]);

Use those values as the cases of the switch.


Presumably there is a way to to this use a server control, but I do see
it.

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com


Tim923 said:
For an exercise, I must use a switch statement to assign values to
variables based on the 1 or 4 radio buttons selected. Can I do this
without using an extra if-else statement? Some code below:

if (makeOverRadioButton.Checked) {intServiceChoiceLocal = 1;}
else if (hairStylingRadioButton.Checked) {intServiceChoiceLocal = 2;}
else if (manicureRadioButton.Checked) {intServiceChoiceLocal = 3;}
else if (permanentMakeupRadioButton.Checked) {intServiceChoiceLocal
=4;}

switch (intServiceChoiceLocal)
{
case 1:
strServiceNameLocal = "Make Over";
decServiceCostLocal = decMAKEOVER_PRICE;
break;

(code snip, etc.)

-
http://mysite.verizon.net/vze8adrh/news.html (profile) --Tim923 My email
is valid.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top