-1

我不能将一个值从一个类传递给另一个类,当我尝试时,该值传递给 0 而不是第一个类的值。

我有这个:一个方法将检查人员是否在数据库中,并且变量 IDautor 将获得 id 在数据库中,它将进入 if 子句,然后位置 1 中的数组 id2 将保存值IDautor ...我假装在Artigo类中使用该值,但该值始终为0,我不知道为什么Class Autor

    public bool Login(TextBox txtUser, TextBox txtPassword)

    {

        string utl, pass;

        try
        {
            utl = txtUser.Text;
            pass = txtPassword.Text;

            _Sql = "Select Count(IDAutor) From Autor Where uUser = @uUser and Pass = @Pass";

            SqlCommand cmd = new SqlCommand(_Sql, Conn);

            cmd.Parameters.Add("@uUser", SqlDbType.VarChar).Value = utl;
            cmd.Parameters.Add("@Pass", SqlDbType.VarChar).Value = pass;

            Conn.Open();

            IDautor = (int)cmd.ExecuteScalar();                
            if (IDautor > 0)
            {
                MessageBox.Show("Bem Vindo");                        
                Conn.Close();
                id2[1] = IDautor;
                return true;
            }
            else
            {
                MessageBox.Show("Tera que tentar de novo");
                Conn.Close();
                return false;
            }                
        }
        catch(Exception ex)
        {
            MessageBox.Show("ERRO Message: "+ ex);
            Conn.Close();
            return false;
        }
    }



public int[] id2 
    {
        get { return this.id2; }
    }

并尝试将值保存在数组 id2 中并在另一个类中使用该值

类Artigo

public string AddArtigo(string newArtigo)
   {
       if (newArtigo == null)
       {
           return "Nao selecionou o PDF desejado. Tente de novo";               
       }
       if (newArtigo != null)
       {
           using (FileStream st = new FileStream(newArtigo, FileMode.Open, FileAccess.Read))
           {
               SqlConnection Conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Convidado1\Desktop\Aplicaçao C#\Aplicaçao\ITArtiConf\ITArtiConf\Database1.mdf;Integrated Security=True;User Instance=True");

               Autor au = new Autor();

               byte[] buffer = new byte[st.Length];
               st.Read(buffer, 0, (int)st.Length);
               st.Close();

               Conn.Open();
               SqlCommand cmd = Conn.CreateCommand();
               cmd.CommandText = "insert into Artigo (idAutor) values('" + au.id2[1] + "')";
               cmd.ExecuteNonQuery();
               Conn.Close();


               SqlCommand cmd1 = new SqlCommand("UPDATE SomeTable SET Artigo=@Artigo WHERE idAutor =" + au.id2[1], Conn);
               cmd1.Parameters.AddWithValue("@Artigo", buffer);
               Conn.Open();
               int i = cmd1.ExecuteNonQuery();
               Conn.Close();                   
           }
           return "Guardado";
       }

       return "Error";           
   }

au.id2[1] 变为 0 而不是其他类的值。

4

3 回答 3

1
Autor au = new Autor();

这行代码创建了一个新Author对象,该对象的所有属性都设置为默认值。在尝试获取值之前,您不会对该对象再调用任何内容id,因此它的值为 0。

您应该考虑制作该属性static,什么会使它的价值持久。

于 2012-12-29T14:04:42.873 回答
0

将Author对象传递给另一个类。使用此 Author 对象,您可以访问值。

作者 _author = 新作者();

int 值 = _author.id[1];

于 2012-12-29T14:51:45.133 回答
0

最好的方法是不使用Autor au = new Autor();,而是更改 的构造函数Artigo以接受 anAutor并将其或某些属性保存为成员Artigo或更改签名AddArtigo以接受Autor.

所以,要么:

Autor au = new Autor();    
au.Login(user, pass); // this should really accept strings and not TextBoxes...
Artigo art = new Artigo(au); // this saves id2[1] as a member somewhere
art.AddArtigo(newArtigo); // why does the Artigo class have an AddArtigo method?

或者

Autor au = new Autor();    
au.Login(user, pass);    
Artigo art = new Artigo();
art.AddArtigo(newArtigo, au);

Either way, there are multiple little issues with your solution (from having an array id2, to a Login method accepting TextBoxes and an Artigo that adds itself, most likely that code would be better suited as part of the Artigo constructor).

于 2012-12-29T15:01:50.680 回答