-2

So i try to get around this issue. I know for a fact that this will be initialized before am calling it but c# can not know this i guess. I cannot do xUser user = new xUser; One work around i was thinking about was using an List and store the values in list and then create the xuser after the while loop but it seems very messy. And i really wanna learn how to avoid this kind of errors.

        noUSer iuser = new noUSer();
        xUser user;
        string sql = "select * from accounts where id='" +id +"'";
        SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
        SQLiteDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {

            iuser.user_name = reader["login"].ToString();
            iuser.password = reader["password"].ToString();
            iuser.cookie_file = @"c:\cookies";

            user = iusers.create(iguser);


        }



        m_dbConnection.Close();
        if (tab_mode.SelectedTab.Text == "User")
        {
           dothiscall(user); //Error on "user" local variable might be not initialized before accessing

        }
5
  • 1
    Initialize it to null. However, you need to be aware that if loop doesn't execute, it will remain null. Commented Jul 3, 2016 at 2:08
  • Worked! Yes am aware of that. Thank you so much make it as an answer if you want. Commented Jul 3, 2016 at 2:12
  • Do you have a single user or multiple users? if so, with current implementation, you will call "dothiscall" only for the last one. Commented Jul 3, 2016 at 2:16
  • Its single user, thanks for the heads up Commented Jul 3, 2016 at 2:23
  • Possible duplicate of Why C# local variables must be initialized? Commented Nov 12, 2019 at 8:15

3 Answers 3

4

You should initialize the variable to null, however, be aware that if the code inside while loop doesn't execute, it will remain null.

Sign up to request clarification or add additional context in comments.

Comments

0

Yes better way is create a list iusers and add new iuser inside while loop. and your whileloop to user you are adding iguser which can not find. It should be iuser

   xUser user = new xUser();
   string sql = "select * from accounts where id='" +id +"'";
   SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
   SQLiteDataReader reader = command.ExecuteReader();

    while (reader.Read())
    {
        noUSer iuser = new noUSer();
        iuser.user_name = reader["login"].ToString();
        iuser.password = reader["password"].ToString();
        iuser.cookie_file = @"c:\cookies";

        user = iusers.Add(iuser);

    }

Comments

0

When you do:

xUser user;

You are merely creating a pointer.meaning if u had an object of type xUser later on and wanted a pointer to it you could do for example something like :

user =(xUser)DataGridView1.Rows[0].cells[myxUserObjectInCell.index].Value;

That way making changes to user will make changes to the object in your grid. However if you're planning on creating an object of type xUser,then you need to do:

xUser user = new xUser();

The new keyword makes it initialize.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.