【问题标题】:Error in my Decryptor/Encryptor Error CS0236我的解密器/加密器错误 CS0236
【发布时间】:2017-05-29 05:18:20
【问题描述】:

错误 CS0236 字段初始值设定项无法引用非静态字段、方法或属性

我一直在尝试修复它,但我无法解决 这是我正在制作的加密器/解密器程序

string hash = (materialSingleLineTextField4.Text);

private void materialRaisedButton1_Click(object sender, EventArgs e)
{
    //Encrypt Function
    byte[] data = UTF8Encoding.UTF8.GetBytes(materialSingleLineTextField1.Text);
    using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
    {
        byte[] keys = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hash));
        using (TripleDESCryptoServiceProvider tripDes = new TripleDESCryptoServiceProvider() { Key = keys, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 })
        {
            ICryptoTransform transform = tripDes.CreateEncryptor();
            byte[] results = transform.TransformFinalBlock(data, 0, data.Length);
            materialSingleLineTextField2.Text = Convert.ToBase64String(results, 0, results.Length);
        }
    }
}

我的错误在这里string hash = (materialSingleLineTextField4.Text); 第一行,它用红色下划线materialSingleLineTestField4 有人可以帮帮我吗?我遇到了困难,谢谢 c;

【问题讨论】:

    标签: c#


    【解决方案1】:

    您试图在类初始化级别在任何方法或事件的上下文之外设置此值:

    string hash = (materialSingleLineTextField4.Text);
    

    但是当类第一次被初始化时,这没有任何意义。 materialSingleLineTextField4.Text 没有任何价值,甚至不能保证有任何有效的 materialSingleLineTextField4 实例。

    如果要在materialRaisedButton1_Click 方法中设置和使用该值,则将该行代码放入该方法中。如果需要,变量 仍然可以在类级别范围内。但至少,必须在方法或构造函数中将其设置为 materialSingleLineTextField4.Text 的值。

    【讨论】:

    • 或构造函数:)
    • @niceman:好点,我已经更新了答案。我一直认为这两者在上下文上足够相似,但这肯定是一个重要的区别。
    • 谢谢 c;这对我真的很有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 2011-02-24
    • 2020-11-03
    相关资源
    最近更新 更多