问题描述:

I am relatively new to C# and I am trying to create an app where a user can supply a lot of data about bonds. So I have a lot of textboxes for bonds names etc and I have 2 radio buttons inside a groupbox to define a coupon type. So, I have a radio button for Fixed and for Zero. What I want to happen is that when a user selects one of the radio buttons, that the selection will be added to a list.

I was trying to do a boolean and say:

if (rdbtn_Fixed.Checked)

{

string couponType = rdbtn_Fixed.Text;

}

else

{

if (rdbtn_Zero.Checked)

{

string couponType = rdbtn_Zero.Text;

}

}

Though I know it was a long shot because I didn't think you can define a variable that way. I have a method calling a different class as such:

Bond bonds = CreateBond(cusip, ticker, name, maturityNoTime, coupon, primaryBook, assignedBook, couponType, positionValue);

So I am having trouble defining the couponType as whichever radio button is selected.

网友答案:

You simply need to put the declaration of couponType outside the if:

string couponType = null;

if (rdbtn_Fixed.Checked)
    couponType = rdbtn_Fixed.Text;
else if (rdbtn_Zero.Checked)
    couponType = rdbtn_Zero.Text;
网友答案:
 string couponType = rdbtn_Fixed.Checked == true ? rdbtn_Fixed.Text : rdbtn_Zero.Text;
相关阅读:
Top