MainForm, Usercontrol and a Class...I´m a Newbie

Hello,

I really don’t know how to make it. But let me axplain:

I have a MainForm, a UserControl and a Class. In the MainForm I will create an Objekt from my Class. But then I’m not able to access this Object ! My Class will create a Connection to a DB ,makes SQL statementsand and can close the connection. I will use this Class everywhere but how will I do this I’ve learned Delphi before and there it was possibel to use an Object that was created in Form1 in Form2 by this code:

form1.MyClass.Connect(path);

How will I do something like this in c# Or will I have to Destroy my Object in MainForm and Create a new one in my UserControl

Hope you can get me some help, please

With kind regards

twickl



Answer this question

MainForm, Usercontrol and a Class...I´m a Newbie

  • Mary Chipman

    Delphi is cool...but I think C# is also really cool. That’s the reason why I want to learn it. but I’ll never give up Delphi! :)

    So, I added in my Class the following lines:

    public sealed class Singleton

    {

    private static readonly Singleton instance = new Singleton();

    private Singleton() { }

    public static Singleton Instance

    {

    get

    {

    return instance;

    }

    }

    }

    And in my MainForm I create the object

    DBClass MyDBClass = new DBClass();

    But how can I now access this object from my UserControl !

    twickl


  • R. Dobson

    Delphi Rox!!! doesn't it

  • Radim Hampel

    Ohhhh.....I to dumb to read! The Example in the msdn is a class. So I have to make my class looking like the example, or
  • Santiago88

    its the equivalent of a global variable.

    all objects in the scope of the class can refer to it via Singleton.Instance.

    you need to get these books. . . yesterday:

    Design Patterns: Elements of Reusable Object-Oriented Software

    Refactoring: Improving the Design of Existing Code



  • Mark Lowenstein

    you had to make MyClass either a public property or a public field (ill-advised) in delphi, didn't you

    Got to do the same in C#.

    You might want to take a look implementing a singleton pattern



  • MainForm, Usercontrol and a Class...I´m a Newbie