I got only 1 form, which use to retrieve text from a text file. After that I need to do convertion(convertion of the text file) and display(display the converted text) as well.

I manage to have 4 class which are Form1.cs, Convertion.cs, Display.cs and Database.cs. So Form1, Convertion and Display must sharing the same instant of Database class. But i don't know how to do in c#. Because what I'm able to do is create different instant on the 3 class (Form1, Convertion and Display). Then they are not sharing the same database anymore. Is it all I need to do is 3 of these classes inherit Database.cs?

Another problem is these 3 classes are running simulataneously. It might overwriting the database.

What I did is (which currently I don't have Database.cs), I keep using get,set function to update the data when everytime I need to update or read the data. So these 3 classes has the same variables with different name. But I found that this is not a good way.

Thanks for any suggestion. Sorry because I don't really study C# before.

Recommended Answers

All 2 Replies

You could use Polymorphism in your project. This means you will use your Database class as your base class, and other 2 (Conversation and Display) classes will be deirived from it - this means they both can access to base class.
But this still wont salve the problem of having the same instance of a class inside both other classes. You will still need to create an instance of a base class (database) and use the reference of it for all the other 2 classes, if you want to keep the same data passes to it.

Here is a simple example (its made in console) but almost the same can be done win forms:

        class Program
        {
            static void Main(string[] args)
            {
                DataBase db = new DataBase();
                Conversion co = new Conversion(db);
                Display di = new Display(db);
                co.Method_C1();
                di.Method_D1();

                Console.ReadLine();
            }
        }

        class Conversion
        {
            DataBase db1;
            public Conversion(DataBase param)
            {
                this.db1 = param;
            }

            public void Method_C1()
            {
                Console.WriteLine(db1.A);
            }
        }

        class Display
        {
            DataBase db2;
            public Display(DataBase param)
            {
                this.db2 = param;
            }

            public void Method_D1()
            {
                Console.WriteLine(db2.B);
            }
        }

        class DataBase
        {
            public int A { get; private set; }
            public string B { get; private set; }
            public DataBase()
            {
                A = 2;
                B = "b";
            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.