Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9086793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:34:26+00:00 2026-06-16T21:34:26+00:00

I can not pass one value from one class to another, when i try,

  • 0

I can not pass one value from one class to another, when i try, the value pass to 0 instead the value from the first class.

I have this: an method that will check if the person is in the database, and the variable IDautor will gain the id is on the database, it will enter in the if clauses then the array id2 in the position 1 will save the value from IDautor… I pretend to use that value in the class Artigo, but the value came always 0 and i don’t no why
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; }
    }

and in trying to save the value in array id2 and use that value in another class

Class 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";           
   }

the au.id2[1] become 0 instead the value on the other class.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:34:28+00:00Added an answer on June 16, 2026 at 9:34 pm

    The best way would be not to use Autor au = new Autor(); but rather to change the constructor of the Artigo to accept an Autor and save it or some properties as members of Artigo or to change the signature of AddArtigo to accept an Autor.

    So, either:

    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?
    

    or

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I pass a value from one class to another. eg Class one
i am trying to pass the value t from one class to another, but
I want to pass some variables from one class to another, but the code
I am using XML parser.In that i have to pass one id from one
I have this little problem, that I cannot figure out which arguments to pass
I would like to pass a variable from one Controller Action to another and
I have a rather silly question, I need to pass a parameter from one
I'm trying to pass a null value from a RenderAction to another view. But
I would like to pass a new value for an integer from one Activity
Because shells other than ksh do not support pass-by-reference, how can multiple arrays be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.