accessing custom entity objects throughtout the project

Hi,

When we define a ProductEntity class for example.

If I want to use the class object myProduct in a form1 and form2 and access the properties from different forms.

Let s say, first I load an mdi form  mymdiForm, then I click a button that loads form1 and in form1 i click a button that loads form2 and so on..

My concern:

1- where do I instatiate myProduct object. In the mdi, or in form1 or in form2 knowing that myProduct  properties are assigned from controls located in the form form2.

Also I want to make sure that I can access those properties in the mdi and form1 forms and all the project

Do I declare myProduct  as public or friend or what

Also how can I access the object myProduct  and its properties

2- Do u know any document related to this issue since I get many errors, can t access object and so on

Thanks a lot.

 

I instantiate an object




Answer this question

accessing custom entity objects throughtout the project

  • app developer

    What does :

    public Form1(Product myProduct)

    {

    //set the object on this form to the parameter

    this.myProduct = myProduct;

    }

    correspond to in VB.net . is it the New or it it a method that I should add myself

    Thanks.



  • Bart Goltstein

    That is only a constructor. The format of it in vb.net is...after doing a quick google seach is

    Public Sub New(ByVal myProduct As Product)
    me.myProduct = myProduct

    End Sub



  • John Broomfield

    hey

    it is a solution, but it may not be a best practice ;)

    look at design patterns - especially the model view controller pattern for best practices regarding gui - business logic separation

    http://www.dofactory.com/



  • idos

    I don't have any experience with mdi forms, but you can still overload the constructors.

    Public Sub New(ByVal myProduct As Product)
    me.myProduct = myProduct


    End Sub

    default constructor

    public Sub New()

    End Sub

    In order to get multiple instances of your object.

    public Sub New(ByVal whatever the syntax for an array is)

    EndSub

    So depending on what you want your form to hold, pass to it different parameters.



  • ceedee

    That perfectly solved my problem ryan. Is this a best practice to pass Custom Entity objects between forms and classes.

    Also what s the right place in this forum to ask questions about Object oriented design questions and best practices in C# or VB.net

    Thanks.



  • InquiringMinds

    I guess this is the best place for C#. Not sure about Vb.net. As for object oriented design, look for a book in a library. or http://en.wikipedia.org/wiki/Object-oriented_programming



  • wolf777

    If I use :

    public Form1(Product myProduct)

    {

    //set the object on this form to the parameter

    this.myProduct = myProduct;

    }

    That means that I link product entity to the forms form1 and form2. but that s not the case. Because when I load Form1 or Form2 it doesn t mean that I always have access to a product entity. may I won t need it in some cases at all. Also, may I will need to access several instances of the product entity at the same time.

    So is there an other way than passing the product instance in the form constructor

    Thanks.



  • qpsk

    I would use the constructor to pass the Product object between forms. Where do you get the data and set its properties If you get its properties in mymdForm, then in your code to create the Form1, pass the Product to the Form1 as a parameter. ex.

    Code for button click event in mymdForm:

    Product myProduct = new Product();

    set its properties

    Form1 form1 = new Form1(myProduct);

    Code in Form1

    // first initialize a Product object so your whole form can know it

    Product myProduct;

    public Form1(Product myProduct)

    {

    //set the object on this form to the parameter

    this.myProduct = myProduct;

    }

    //then later on for the button click event to load Form2, do the same thing as you did in mymdForm

    Code in Form2

    // first initialize a Product object so your whole form can know it

    Product myProduct;

    public Form2(Product myProduct)

    {

    //set the object on this form to the parameter

    this.myProduct = myProduct;

    }

    hope this helps



  • accessing custom entity objects throughtout the project