开发者

Setting the currently selected item of a radio button to a value for use in a object constructor

开发者 https://www.devze.com 2023-02-21 17:58 出处:网络
Hey there, I have a Dog class, which has a constructor which takes, int Id, string Name, and SizeType sizeType values.

Hey there, I have a Dog class, which has a constructor which takes, int Id, string Name, and SizeType sizeType values.

The user can specify all these values from the user interface by typing into the text box for id and name, however for sizetype they must select a radio button. The size type is an enumeration type public enum SizeType { small, medium, large }; (this is declared on a code file), and this is declared where the instance variables are declared for the class, private SizeType sizeType_;. Now when trying to create a dog object based on the开发者_开发知识库 users input, how do I set the value of the SizeType enum to the currently selected radio button on the form??

Here is all the related code:

public Dog (int idNumber, string dogName, string breed, SizeType sizeType, NatureType natureType)
{
  dogId_ = idNumber;
  dogName_ = dogName;
  breed_ = breed;
  sizeType_ = sizeType;
  natureType_ = natureType;
}

public enum SizeType { small, medium, large };
private SizeType sizeType_;


what technology are we talking?

Enum.Parse function will take a string value (the value of your radio button) and parse it to the enum value if thats what your trying to do. But depending on what we're talking (asp.net mvc, winforms, etc) this may or may not be necessary.

http://msdn.microsoft.com/en-us/library/system.enum.parse.aspx


Making some assumptions on the technology you're using; you could respond to the CheckedChanged event;

    public enum SizeType { small, medium, large };
    private SizeType size;

    private void rbSmall_CheckedChanged(object sender, EventArgs e)
    {
        size = SizeType.small;
    }
    private void rbMedium_CheckedChanged(object sender, EventArgs e)
    {
        size = SizeType.medium;
    }
    public void CreateDog()
    {
        new Dog(10, "Ben", "Poodle", size, ...);
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号