二进制转换器使用Get Set

本文关键字:Get Set 转换器 二进制 | 更新日期: 2023-09-27 18:18:31

我正在使用Get和Set方法在windows窗体中制作二进制转换器

执行转换的代码
class SubnetConvert
{
private int numconvert;
private string OctetToBinary;
public int OctetConvert
{
get 
{
    return numconvert;
}
    set
    {
        List<int> Subnet = new List<int>(new int[] { 128, 64, 32, 16, 8, 4, 2, 1 });
        foreach (int num in Subnet)
        {
            if (num <= numconvert)
            {
                numconvert -= num;
                OctetToBinary += "1";
            }
            else
            {
                OctetToBinary += "0";
            }
        }
    }
}
public string SendBinary
{
    set
    {
        OctetToBinary = value;
    }
    get
    {
        return OctetToBinary;
    }
}

应用于转换按钮的代码

 private void btnSubnetting_Click(object sender, EventArgs e)
    {
        SubnetConvert SubnetOctet = new SubnetConvert();
        lblOctet1.Text = "";
        int Octet1 = int.Parse(txtOctet1.Text);
        SubnetOctet.OctetConvert = Octet1;
        lblOctet1.Text = SubnetOctet.SendBinary;
    }

目前唯一返回的值是8个0或8个1

二进制转换器使用Get Set

行前:

    List<int> Subnet = new List<int>(new int[] { 128, 64, 32, 16, 8, 4, 2, 1 });

添加这一行:

    numconvert = value;