ref或out参数必须是可赋值变量 [英] A ref or out argument must be an assignable variable

查看:1013
本文介绍了ref或out参数必须是可赋值变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用存储过程来更改密码。但它给了我错误。



我尝试过:



和按钮单击我的功能是: -



 受保护 < span class =code-keyword> void  BtnSave_Click( object  sender,EventArgs e)
{
using (SampleDataContext dbContext = new SampleDataContext())
{
System.Nullable< char> outpt = null ;
System.Nullable< long> id = Convert.ToInt64(Session [ USERID]);
dbContext.passwrdchange(id,old.Text.ToString()。Trim(),newpass.Text.ToString()。Trim(), ref outpt );

if (outpt.ToString()== T
{
ClientScript.RegisterStartupScript( this .GetType(), SuccessMsg < script> alert('密码已成功更改')< / script>);
}

else
{
ClientScript.RegisterStartupScript( this .GetType(), ErrorMsg < script> alert('旧密码与记录不匹配。')< / script>);
}
}
}





我的存储过程是: -



  ALTER   proc  [dbo ]。[passwrdchange] 
@ ID bigint
@ oldpass varchar (MAX),
@newpass varchar (MAX),
@ Out char 输出
作为
开始
设置 @ Out = ' F'

if 选择密码来自 User_Infor mation 其中 ID = @ID)= @ oldpass
开始
更新 User_Information 设置密码= @newpass 其中 ID = @ ID
设置 @ Out = ' T'
结束

结束





我的Linq .cs文件函数用于调用存储过程是: -



< pre lang =C#> private void UpdateUser_Information(User_Information obj)
{
this .passwrdchange(((System.Nullable< long>)(obj.ID)), default string ),默认 string ), ref default (System.Nullable<炭>));
}





但是这给了我一个错误

 ref或out参数必须是一个可分配的变量



请帮助!!

解决方案

请参阅我对这个问题的评论。有什么不清楚的吗?



问题在于:

  this  .passwrdchange((System.Nullable< long>)(obj.ID)),默认 string ),默认 string ), ref  默认(System.Nullable< char>); 



这里,你的 ref 参数不是可赋值变量。这是由类型定义的常量。您需要通过引用了解传递参数。调用方法应该填充内存中的某个位置。你的电话中没有这样的东西。这将编译:

 System.Nullable< char> someVariable =  //  某个值,null或不 
this .passwrdchange((System.Nullable< long>)(obj.ID)),默认 string ),默认 string ), ref someVariable);
// 但现在:
// 此调用表明您可以使用值
// 通话后someVariable的



您必须了解这一点:

ref(C#参考) [ ^ ],

out(C#参考) [ ^ ],

参考(计算机科学) - 维基百科,免费的百科全书 [ ^ ],

传递参数(C#编程指南) [ ^ ]。



-SA


I'm Calling a stored procedure for password change. But it is giving me Error.

What I have tried:

And On Button Click My Function is:-

protected void BtnSave_Click(object sender, EventArgs e)
  {
      using (SampleDataContext dbContext = new SampleDataContext())
      {
          System.Nullable<char> outpt = null;
          System.Nullable<long> id = Convert.ToInt64(Session["USERID"]);
          dbContext.passwrdchange(id, old.Text.ToString().Trim(), newpass.Text.ToString().Trim(),ref outpt);

          if(outpt.ToString()=="T")
          {
              ClientScript.RegisterStartupScript(this.GetType(), "SuccessMsg", "<script>alert('Password Changed Successfully')</script>");
          }

          else
          {
              ClientScript.RegisterStartupScript(this.GetType(), "ErrorMsg", "<script>alert('Old Password Does Not Match with Records.')</script>");
          }
      }
  }



My stored procedure is:-

ALTER proc [dbo].[passwrdchange]
@ID bigint,
@oldpass varchar(MAX),
@newpass varchar(MAX),
@Out char output
As
Begin
Set @Out='F'

	if(Select Password from User_Information where ID=@ID)=@oldpass
		Begin
			Update User_Information Set Password=@newpass where ID=@ID
			Set @Out='T'
		End
	
End



And My Linq .cs file function for Calling stored procedure is:-

private void UpdateUser_Information(User_Information obj)
	{
		this.passwrdchange(((System.Nullable<long>)(obj.ID)), default(string), default(string),ref default(System.Nullable<char>));
	}



But this is giving me an error

A ref or out argument must be an assignable variable


Please HELP!!

解决方案

Please see my comment to the question. Is anything unclear?

The problem is here:

this.passwrdchange((System.Nullable<long>)(obj.ID)), default(string), default(string), ref default(System.Nullable<char>);


Here, your ref parameter is not assignable variable. This is the constant defined by the type. You need to understand passing parameters by reference. The calling method is supposed to fill in some place in memory. There is no such thing in your call. This would compile:

System.Nullable<char> someVariable = // some value, null or not
this.passwrdchange((System.Nullable<long>)(obj.ID)), default(string), default(string), ref someVariable);
// but now:
// this call suggests that you can use the value
// of someVariable after the call


You have to learn this:
ref (C# Reference)[^],
out (C# Reference)[^],
Reference (computer science) - Wikipedia, the free encyclopedia[^],
Passing Parameters (C# Programming Guide)[^].

—SA


这篇关于ref或out参数必须是可赋值变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆