Karlis Gribulis

Karlis Gribulis

  • NA
  • 18
  • 633

Question about class objects, static, new initialization

Jul 5 2020 3:31 PM

I'm new to programming and for last couple of weeks I'm working a project. In this project I stumbled on a problem and questions related to it, because i couldn't find the right answer (most of them were close to it and so on). In this program you can search for a movie, save it to MySQL database, view its info and so on. So my problems and questions are:

  1. Initial problem was class objects. At the start user logins in the program and I give the username to DBConnection class in which it determines what userID it has and saves to a variable (it is needed to correctly save, delete the new movie for that specific user), but from courses and so on I learned and everyone showed that in new classes or forms in order to access other classes you have to create a new instance of it, but because of that a new problem occurred - every time I made a new instance then the saved userID would be 0 [ 1.1) I assume its because new instance creates the same class but without new variable, so if I create new class object, with it change values then create new instance, then the values will be the default ones, not like the ones created from previous instance? -> Did I understand the new class initializing correctly or does it work differently? ] to ecape that problem (if I understood it correctly) I made the variable userID static and from basic tests I understood that it saved and had the new value in it even if later on in program I made new instance of that class in different UserControl. (It should work like that, correct? Or did I not understand it..
  2. second question is related to the previous. Because of this problem I made some methods static (later I realized I don't need it, but this raised some misunderstandings too) so I could access like DBConnection.MyMethod but does this mean that with this type of access I never initialize constructor, correct? This question is important because in the constructor I have the basic info and set the values to variable with which I have necessary data to connect to my MySQL database. So for example, if I do the DBConnection.MyMethod in a different class and within that method I need the information which is set in Constructor does that mean that the method won't work because the values are not set or it will if the class is initialized sometime previously within the program but from different class?
  3. When does one would want to use class object and initializing it as a new instead of just using the example of DBConnection.MyMethod and using static? Like at what point would I really want/need class objects? Whenever I have anything in the constructor within there are needed values for some methods? Or other aspects (I know it is a broad question and have seen some abstract answers, but maybe you could give some examples)

I know the post is long, but as I said these are more of clarification questions more than anything so I could finally wrap my head around, because for the last hours, even a day, I tried to solve this problem and had these questions.

P.S. I know I didn't post any code, because it's really long and messy in my program, but if anyone wants to see it to understand more it's available at https://github.com/KarlisAG/Movie-Project/tree/master/Project_Movie/Project_Movie (the following DBConnection is in Database folder in the said .cs). Only when I finish the program and it works like I want it, then I will make it a lot shorter and hopefully cleaner (atm it repeats a lot and many parts are probably un-needed, but this is a first big project that's why it is really messy).

P.S.S. Additional question to a problem I'm trying to solve now. I have a listview and when user clicks on columns it sorts it, but the problem is it only does it for the title and not other parameters if the user clicks on different columns ( ) and whenever user opens different UserControl and returns to the one with listview then the table is still sorted (by title) but the other columns are shown as blank spaces -> how and why does this happen?

  1. private void SortColumn(object sender, ColumnClickEventArgs e)   
  2. {   
  3. if (asc) {                   
  4. listView1.Sorting = SortOrder.Descending;                  
  5.  asc = !asc;   
  6. }   
  7. else   
  8. {                   
  9. listView1.Sorting = SortOrder.Ascending;  
  10.                  asc = !asc;   
  11. } 

(asc is a bool with which the program knows if it is in ascending order at that time or now)

Thank you to anyone in advance!


Answers (2)