如何使用struct作为输入调用函数?

时间:2014-05-20 18:08:47

标签: asp.net c#-4.0 struct

这是我的结构定义,函数和调用函数的部分。

public struct NewSigningRequest
{
    public string SerialNumber;
    //public string Requestor;
    public string Sponsor;
    public string Approver;
    public string BusinessJustification;
    public byte[] DebugFile;
    public string DebugFileName;
    public string FirmwareVersion;
    public string FirmwareDescription;
    public byte[] SmokeTestResult;
    public string ProductName;
    public string SigningType;
}
    static public bool CreateSigningRequest(NewSigningRequest Request)
    {
        Request = new NewSigningRequest();
        bool succeeded = false;
        string sqlcmdString = sqlQueryNewSigningRequest;
        SqlConnection con = new SqlConnection(connection);
        SqlCommand Insertcmd = new SqlCommand(sqlcmdString, con);

        SqlParameter SerialNumber = new SqlParameter("@SerialNumber", SqlDbType.NVarChar, 50);
        SerialNumber.Value = Request.SerialNumber;
        Insertcmd.Parameters.Add(SerialNumber);

        SqlParameter Requestor = new SqlParameter("@Requestor", SqlDbType.NVarChar, 20);
        Requestor.Value = GetRequestor.GetRequestorAlias();
        Insertcmd.Parameters.Add(Requestor);

        SqlParameter Sponsor = new SqlParameter("@Sponsor", SqlDbType.NVarChar, 20);
        Sponsor.Value = Request.Sponsor;
        Insertcmd.Parameters.Add(Sponsor);

        SqlParameter BusinessJustification = new SqlParameter("@BusinessJustification", SqlDbType.NVarChar, 2000);
        BusinessJustification.Value = Request.BusinessJustification;
        Insertcmd.Parameters.Add(BusinessJustification);

        SqlParameter DebugFile = new SqlParameter("@DebugFile", SqlDbType.VarBinary, 8000);
        DebugFile.Value = Request.DebugFile;
        Insertcmd.Parameters.Add(DebugFile);

        SqlParameter DebugFileName = new SqlParameter("@DebugFileName", SqlDbType.NVarChar, 100);
        DebugFileName.Value = Request.DebugFileName;
        Insertcmd.Parameters.Add(DebugFileName);

        SqlParameter ProductName = new SqlParameter("@ProductName", SqlDbType.NVarChar, 20);
        ProductName.Value = Request.ProductName;
        Insertcmd.Parameters.Add(ProductName);

        SqlParameter SigningType = new SqlParameter("@SigningType", SqlDbType.NVarChar, 10);
        SigningType.Value = Request.SigningType;
        Insertcmd.Parameters.Add(SigningType);

        SqlParameter FirmwareVersion = new SqlParameter("@FirmwareVersion", SqlDbType.NVarChar, 50);
        FirmwareVersion.Value = Request.FirmwareVersion;
        Insertcmd.Parameters.Add(FirmwareVersion);

        SqlParameter FirmwareDescription = new SqlParameter("@FirmwareDescription", SqlDbType.NVarChar, 250);
        FirmwareDescription.Value = Request.FirmwareDescription;
        Insertcmd.Parameters.Add(FirmwareDescription);

        SqlParameter SmokeTestResult = new SqlParameter("@SmokeTestResult", SqlDbType.VarBinary, 8000);
        SmokeTestResult.Value = Request.SmokeTestResult;
        Insertcmd.Parameters.Add(SmokeTestResult);

        return succeeded;
    }

这就是我试图称之为

的方式
                NewSigningRequest Request;
                byte[] bin;
                string FileName;
                if (FileUpload.HasFile)
                {
                    bin = new byte[FileUpload.PostedFile.ContentLength];
                    HttpPostedFile mybin = FileUpload.PostedFile;
                    FileName = mybin.FileName;
                    mybin.InputStream.Read(bin, 0, FileUpload.PostedFile.ContentLength);
                }
                else
                {
                    bin = null;
                    FileName = "";
                }
                string NameAlias = @HttpContext.Current.User.Identity.Name;
                int index = NameAlias.IndexOf("\\") + 1;
                string sAlias = NameAlias.Substring(index);

                byte[] SmokeTest;

                if (FileUploadSTR.HasFile)
                {
                    SmokeTest = new byte[FileUploadSTR.PostedFile.ContentLength];
                    HttpPostedFile mySmokeTest = FileUploadSTR.PostedFile;
                    mySmokeTest.InputStream.Read(SmokeTest, 0, FileUploadSTR.PostedFile.ContentLength);
                }
                else
                {
                    SmokeTest = null;
                }
                Request.SerialNumber = TextBoxSerialNumber.Text;
                Request.Sponsor = TextBoxSponsor.Text;
                Request.BusinessJustification = TextBoxBJ.Text;
                if (bin == null)
                {
                    Request.DebugFile = new byte[0];
                }
                else
                {
                    Request.DebugFile = bin;
                }
                Request.DebugFileName = FileName;
                Request.FirmwareVersion = TextBoxFV.Text;
                Request.FirmwareDescription = TextBoxFD.Text;
                if (TextBoxSigningType.Text == string.Empty)
                {
                    Request.SigningType = drp2.SelectedValue.ToString();
                }
                else
                {
                    Request.SigningType = TextBoxSigningType.Text.ToUpper();
                }
                if (SmokeTest == null)
                {
                    Request.SmokeTestResult = new byte[0];
                }
                else
                {
                    Request.SmokeTestResult = SmokeTest;
                }
                SurfaceSignDBAccess.CreateSigningRequest(Request);

在调用此函数时,会出错并说明"使用未分配的本地变量'请求'。"

调用此功能的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

NewSigningRequest请求; - 在调用之前,从未在方法中初始化         SurfaceSignDBAccess.CreateSigningRequest(请求);

试试这个: var request = new NewSigningRequest(); //在第二个代码片段的第一行代码中。 并在静态方法中,删除以下代码。         Request = new NewSigningRequest();