Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 致电Get&;在类外设置方法_C# - Fatal编程技术网

C# 致电Get&;在类外设置方法

C# 致电Get&;在类外设置方法,c#,C#,如何从另一个类调用crypt方法并插入setmethod或getmethod?密码正在从文本框加载 public partial class LogUser : Form { private string userPassword; // textbox where user insert password public string GetUserPassword() { return userPassword; } public void

如何从另一个类调用crypt方法并插入setmethod或getmethod?密码正在从文本框加载

public partial class LogUser : Form
{

   private string userPassword;
   // textbox where user insert password 
   public string GetUserPassword()
   {
      return userPassword;
   }

   public void SetUserPassword(string userPassword)
   {
      this.userPassword = userPassword;
   }

   private void loginUser_Click(object sender, EventArgs e)
   {
          MyFunctions.Crypt(GetUserPassword());
          // compilator error.
   }      // can't convert from 'string' to 'Project.LogUser'
}

class MyFunctions
{



   public static string Crypt(string cryptPassword)
   {
      Random ra = new Random();
      int key = Rando.Rand(ra, 22, 4);
      LogUser cryptingPw = new LogUser();
      string crypt = Encrypting(cryptPassword, key);
      return crypt;
   }
}
我已经更新了我的问题。因此,加密方法将一个对象…

替换此代码:

MyFunctions.Crypt(GetUserPassword());
LogUser objLogUser = new LogUser();
MyFunctions.Crypt(objLogUser);
编写以下代码:

MyFunctions.Crypt(GetUserPassword());
LogUser objLogUser = new LogUser();
MyFunctions.Crypt(objLogUser);

您正在将方法GetUserPassword()的返回值传递给Crypt方法。 该方法需要一个LogUser作为输入。这不是字符串


我认为您需要修改Crypt方法的签名,以接受字符串作为输入,以消除编译错误。

您可以使用
MyFunctions.Crypt
调用它,如果您的作用域允许的话。请查看参数的类型。这是你的问题
GetUserPassword
提供一个字符串,但您的
Crypt
函数需要一个
LogUser
。该函数可以消除编译错误,但不会提供所需的结果,因为新创建的
LogUser
对象不包含用户输入的密码。OP可以简单地将
这个
作为参数传递,或者更好地重构
Crypt
方法,以字符串作为参数。