Public Int showing 0 when being used in different Class

how to pass value from one method to another in java
encapsulation
polymorphism
when writing a method call, the object that will perform the method is called the:
instance variable
what does an object look like in java
how to instantiate an object from another class in java
how to call a variable from another method in java

I'm trying to set a public int using a button click from a class then using create a new instance and use the value in another class to then be inserted into a DB.

I'm using webforms and C# to produce my system. Any help with this is much appreciated.

CLASS 1 --------------------------------

 public partial class Index : System.Web.UI.Page
    {

        public int ID1;

        public void bLogin_Click(object sender, EventArgs e)
        {
            string username = tbUsername.Text;
            string password = tbPassword.Text;

            SqlCommand cmd2 = new SqlCommand("select UsersID from [dbo].[Users] where Username=@uname and Password=@password");
            cmd2.Connection = con;
            cmd2.Parameters.AddWithValue("@uname", tbUsername.Text);
            cmd2.Parameters.AddWithValue("@password", tbPassword.Text);

            int Result2 = (int)cmd2.ExecuteScalar();
            ID1 = Result2;

            if (Result > 0)
            {
                MessageBox.Show("Welcome");
                MessageBox.Show(ID1.ToString());
                Response.Redirect("Dashboard.aspx");
                con.Close();

            }
            else
            {
                MessageBox.Show("Incorrect");
            }

        }

       }
}

CLASS 2 -----------------------------------

protected void bSubmit_Click(object sender, EventArgs e)
        {

            SQLDatabase.DatabaseTable module_table = new SQLDatabase.DatabaseTable("Boards");   // Need to load the table we're going to insert into.

            SQLDatabase.DatabaseRow new_row = module_table.NewRow();    // Create a new based on the format of the rows in this table.


            Index index = new Index();
            string new_id = module_table.GetNextIDBoards().ToString();    // Use this to create a new ID number for this module. This new ID follows on from the last row's ID number.
            string title = tbTitle.Text;
            string body = tbContent.Text;
            DateTime now = DateTime.Now;
            string date = now.GetDateTimeFormats('d')[0];
            string time = now.GetDateTimeFormats('t')[0];

            new_row["BoardsID"] = new_id;                                 // Add some data to the row (using the columns names in the table).
            new_row["Title"] = title;
            new_row["Body"] = body;  // Module name.
            new_row["UsersID"] = index.ID1.ToString();
            new_row["DateCreated"] = date;
            new_row["TimeCreated"] = time;

            module_table.Insert(new_row);                           // Execute the insert - add this new row into the database.

            MessageBox.Show("New Board Created");
            Response.Redirect("Dashboard.aspx");


        }

You can see im trying to transfer the ID across classes.

The database insert is fine but its inseting "0" for the ID.

You can use static field in Index class if you want to keep previous value

public static int ID1;

And this is how you can set its value outside class

Index.ID1 = 23;

ESSENTIAL JAVA FOR SCIENTISTS AND ENGINEERS, One of the places where this is useful is when you are setting data members in a r as formal parameters, we have used the full variable names height and rate. shows a sample class TestScope containing variables with different scope: 0 public c1a55 TestScope { Scope of data I member num . . v~—~1IP private int  This is because the Test class extends class Addition and the protected modifier allows the access of protected members in subclasses (in any packages). Addition.java. package abcpackage; public class Addition { protected int addTwoNumbers(int a, int b){ return a+b; } } Test.java

I can't see this with the code you have provided but it sounds to me based on your description, that you are executing the bLogin_Click method and then creating a new instance of Index.

This new instance will have ID1 have its value default to 0 (unless you set it in a constructor that I can't see). So you need to create the instance first, then execute bLogin_Click on that instance.

Also looks like you assign a value to Result2, but then perform validation on Result.

Also, not really related but many would consider it bad practice for a class to contain a public field, you should consider using a property instead, even a basic one like this:

public int ID1 { get; private set; }

Java Network Programming: Developing Networked Applications, When a socket is closed, it may not immediately release the local port, It might be wise to price the different classes of service differentially so that we'd all just use FedEx overnight, which would quickly become congested and overwhelmed. these two methods: public int getTrafficClass() throws SocketException public  Use the image scaling algorithm embodied in the ReplicateScaleFilter class. The Image object is free to substitute a different filter that performs the same algorithm yet integrates more efficiently into the imaging infrastructure supplied by the toolkit.

Default Constructor will fires whenever creating a new instance.A constructor without any parameters is called a default constructor; in other words this type of constructor does not take parameters. The drawback of a default constructor is that every instance of the class will be initialized to the same values and it is not possible to initialize each instance of the class to different values.

The default constructor initializes:

All numeric fields in the class to zero. All string and object fields to null.

If you want to maintain the state of the type & value to all consumer/client or across multiple instances, just set it as static type.

Internet and Web Technologies, (b) Second way is to code a separate class UnivMarksheetSpl and let the class Figure 6.17 shows the members in super and subclasses. public class into account */ 17. public int findTotal MaxMarks Spl ( ) {int total Marks = 0; for (int  class Point { public int x; public int y; } unsafe private static void ModifyFixedStorage() { // Variable pt is a managed variable, subject to garbage collection. Point pt = new Point(); // Using fixed allows the address of pt members to be taken, // and "pins" pt so that it is not relocated.

Declaring Member Variables (The Java™ Tutorials > Learning the , The Java Tutorials have been written for JDK 8. public int cadence; public int gear; public int speed; Zero or more modifiers, such as public or private . The first (left-most) modifier used lets you control what other classes have access to a​  Constructors (C++) 12/27/2019; 17 minutes to read +7; In this article. To customize how class members are initialized, or to invoke functions when an object of your class is created, define a constructor.

4. Methods Use Instance Variables: How Objects Behave, In other words, methods use instance variable values. Every method is declared with a return type, but until now we've made all of our Number primitives (including char) get 0, booleans get false, and object reference variables get null. class AddThing { int a; int b = 12; public int add() { int total = a + b; return total; } }. priority - a value between 0 and 1, inclusive, where higher values indicate more importance for acceleration. A value of 0 means that this Image should never be accelerated. Other values are used simply to determine acceleration priority relative to other Images. Throws: IllegalArgumentException - if priority is less than zero or greater than 1.

Creating Objects, Instantiation: The new keyword is a Java operator that creates the object. This notifies the compiler that you will use name to refer to data whose type is type. public class Point { public int x = 0; public int y = 0; //A constructor! public Point(int object; an object can have multiple references to it, as shown in the next figure:​  Declares a class (i.e., a type) called Rectangle and an object (i.e., a variable) of this class, called rect.This class contains four members: two data members of type int (member width and member height) with private access (because private is the default access level) and two member functions with public access: the functions set_values and area, of which for now we have only included their

Comments
  • You understand that each object instance gets a new set of fields, right? The bSubmit_Click method creates a brand new instance of Index but never calls bLogin_Click on it. So it never sets the ID1 field to anything. The uninitialized value comes back as 0.
  • Never store passwords as plain text - they must be salted and hashed. Also, do not use AddWithValue
  • Look into storing the ID in the session or somewhere else specific to the user. Static fields exist across all requests, for all users (assuming it's a single server / worker process).
  • @early237 Try having multiple users at the same time. This field can only have one value. All logged in users will be using the value of the last logged in user. The new board will belong to the last user that logged in, not the user who pressed the button (unless they're the same user).
  • @early237 Please note previous comment by madreflection, static field's value changes for all instances of class
  • So how do i make it so more than one user can login/make boards etc? Sessions?? @madreflection
  • One way is to store the ID in the session. Session["ID"] = Result2; in Index.aspx.cs. Elsewhere, use int id = (int)Session["ID"]; to retrieve it. But until you assign it, that cast will fail. There's more to it and there are good resources out there, which is why I'm not making it an answer. See Page's Session property to start, and read up as much as possible on session management in ASP.NET WebForms.