Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Providing static variable to parent class

Tags:

c#

database

I want to make class with tools for managing DB tables. But I need to somehow send back table name to the tools class from data class. I managed to make this work in non-static environment, but I need to make this work also in static functions.

I did some Googling, but I found nothing helpful.

Example usage:

Caller

User.Delete(1);

Tools class

public class DBTools
{
    public static string table_name = "NULL"; 

    public static void Delete(int id)
    {
        Console.WriteLine(table_name);
    }
    ...
}

Data class

public class User : DBTools
{
    public new static string table_name = "users";
    ...
}
like image 770
PSSGCSim Avatar asked Feb 16 '26 12:02

PSSGCSim


1 Answers

One option is to create a Delete() method in any class derived from DBTools, and have it simply call the base class's Delete() method (which could still do all the heavy lifting) and pass it the correct table name.

public class DBTools
{
    public string table_name = "NULL";

    public void Delete(string table_name, int id)
    {
        Console.WriteLine(table_name);

        // whatever work is required for the given table name
    }
}

public class User : DBTools
{
    public string table_name = "users";

    public void Delete(int id)
    {
        Delete(table_name, id);
    }
}
like image 128
Grant Winney Avatar answered Feb 18 '26 00:02

Grant Winney



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!