使用Get Set的二进制转换器

时间:2011-12-20 20:58:08

标签: c#

使用Get and 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 0s或8 1s

1 个答案:

答案 0 :(得分:0)

在此之前:

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

添加以下行:

    numconvert = value;