locked
Create New Instance of Class RRS feed

  • Question

  • I have a program with a mysqldatabase class that I use to access mysqldatabases. I also have a timer in my program that uses this class every couple seconds. My problem is that when I call on the mysqldatabase from another area of the program, I get an error that basically says a variable from the mysqldatabase class is already in use (in use by the timer).

    I thought one way to get around the issue is to create a new instance of the mysqldatabase using this:

    mysqldatabase remindertimerdatabase = new mysqldatabase();

    however I get member 'mysqldatabse.checktableField(string)' cannot be accessed with an instance reference; qualify it with a type name instead. Thing is I can't do that because of the issue I described above. Any ideas on how to get around this?

    Thanks.

    Saturday, September 5, 2015 7:16 PM

Answers

  • It is a static class, which is part of my problem, but I require it to be static.

    I found a way around and just created a second class identical to the mysqldatabase class except changed the class name to a different name effectively creating an instance of the same thing. Not a perfect solution but will work for my purposes as I expect to only need two instances of the class.

    thanks for the replies.

    The second you are having two instances, you should not use a static class.

    Just make the class non-static (as it should have been all along). Instead put the reference into a static variable. Or make a static reference holder class.

    Putting this kind of stuff into static is always going to bite you. Just stop.

    • Proposed as answer by Kristin Xie Tuesday, September 8, 2015 9:40 AM
    • Marked as answer by Kristin Xie Tuesday, September 15, 2015 9:13 AM
    Saturday, September 5, 2015 10:04 PM

All replies

  • You are doing something wrong, and it isn't clear based on your description.

    Show your code. What variable "is already in use"? What is its type and access modifier?

    Saturday, September 5, 2015 7:35 PM
  • "My problem is that when I call on the mysqldatabase from another area of the program, I get an error that basically says a variable from the mysqldatabase class is already in use (in use by the timer)."
    That message makes no sense without context. What is the actuall message? What line throws the message?
    A cross thread exception? Those should only happen if the class goes out of it's way to detect them.

    "'mysqldatabse.checktableField(string)' cannot be accessed with an instance reference"

    The message says clearly that this is NOT an instance function. It is a static function. You access it via [classname].checktableField(string). You try to access it via [the instance I created].checktableField(string).

    We need a lot more info about this class. At least the documentation or full code of mysqldatabase. Afterwards we might be able to understand the most basic stuff.

    Saturday, September 5, 2015 7:38 PM
  • Hi, I am not sure but I think that you can use the lock Statement. You may get rid of your error but I think you will get into a dead lock. 
    I would propose you to debug carefully your project to understand what happens!
    Saturday, September 5, 2015 8:49 PM
  • It is a static class, which is part of my problem, but I require it to be static.

    I found a way around and just created a second class identical to the mysqldatabase class except changed the class name to a different name effectively creating an instance of the same thing. Not a perfect solution but will work for my purposes as I expect to only need two instances of the class.

    thanks for the replies.

    Saturday, September 5, 2015 9:18 PM
  • It is a static class, which is part of my problem, but I require it to be static.

    I found a way around and just created a second class identical to the mysqldatabase class except changed the class name to a different name effectively creating an instance of the same thing. Not a perfect solution but will work for my purposes as I expect to only need two instances of the class.

    thanks for the replies.

    The second you are having two instances, you should not use a static class.

    Just make the class non-static (as it should have been all along). Instead put the reference into a static variable. Or make a static reference holder class.

    Putting this kind of stuff into static is always going to bite you. Just stop.

    • Proposed as answer by Kristin Xie Tuesday, September 8, 2015 9:40 AM
    • Marked as answer by Kristin Xie Tuesday, September 15, 2015 9:13 AM
    Saturday, September 5, 2015 10:04 PM