none
"Reuse the Object"

    Question

  • Hi Frnz,

    In c# i want to use the class1 object in class2, class3.but i dont want to create class1 object more than once.How can i achive this other than single ton.Plz suggest me.Suggest me with some piece of code. i am very new to c#.

    senario:

     i want to enter the values in form1. these values has to set in Class1. and display in forms.again i hav to enter some values in form to textbox.these values has to display in form1.when i going to close any one of the forms it should close properly.

    in below code u can observe i created the object of same class.and there is cyclic dependency is between form1 and form2.

    plz correct me..

    you can see my code also.

    //form1

    using

     

    System;

    using

     

    System.Collections.Generic;

    using

     

    System.ComponentModel;

    using

     

    System.Data;

    using

     

    System.Drawing;

    using

     

    System.Linq;

    using

     

    System.Text;

    using

     

    System.Windows.Forms;

    namespace

     

    Communication

     

    public partial class Form1 : Form

    {

     

    public storeData spd;

     

    private Form2 f2;

     

    public Form1()

     

    public Form1(storeData x)

     

    private void OK_button_Click(object sender, EventArgs e)

     

    new storeData();

     

    new Form2(spd);

     

    this.Hide();

     

    private void Form1_Load(object sender, EventArgs e)

     

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)

     

    this.Close();

     

    Thanks in advance.

    Srinu

     

     

    }

     

    }

    }

    //form 2

    using

     

    System;

    using

     

    System.Collections.Generic;

    using

     

    System.ComponentModel;

    using

     

    System.Data;

    using

     

    System.Drawing;

    using

     

    System.Linq;

    using

     

    System.Text;

    using

     

    System.Windows.Forms;

    namespace

     

    Communication

     

    public partial class Form2 : Form

    {

     

    private storeData spd;

     

    private Form1 f1;

     

    public Form2(storeData x)

     

    private void btnOK_Click(object sender, EventArgs e)

     

    new storeData();

     

    new Form1(spd);

     

    this.Hide();

     

    private void Form2_FormClosing(object sender, FormClosingEventArgs e)

     

     

    {

     

    }

     

     

    }

    }

    //class which i want use between above 2forms

    using

     

    System;

    using

     

    System.Collections.Generic;

    using

     

    System.Linq;

    using

     

    System.Text;

    namespace

     

    Communication

     

    public struct SerialPortData

    {

     

    public string m_port;

     

    public string m_baudRate;

     

    public string m_dataBit;

     

    public string m_parity;

     

    public string m_stopBit;

     

    public string m_flowControl;

     

    public class storeData

    {

     

     

    public SerialPortData test;

     

     

     

     

     

     

    }

    }

     

    };

     

     

     

     

     

     

     

    {

     

     

     

    }

     

     

    f1.Show();

     

     

    spd.test.m_port = frm2_textbox1.Text;

    spd.test.m_baudRate = frm2_textbox2.Text;

    spd.test.m_dataBit = frm2_textbox3.Text;

    spd.test.m_parity = frm2_textbox4.Text;

    spd.test.m_stopBit= frm2_textbox5.Text;

    spd.test.m_flowControl= frm2_textbox6.Text;

    f1 =

     

    {

    spd =

     

    {

    InitializeComponent();

    lbl1.Text = x.test.m_port;

    lbl2.Text = x.test.m_baudRate;

    lbl3.Text = x.test.m_dataBit;

    lbl4.Text = x.test.m_parity;

    lbl5.Text = x.test.m_stopBit;

    lbl6.Text = x.test.m_flowControl;

     

    }

     

     

     

     

     

     

     

     

     

     

    {

     

     

    {

     

     

     

    {

    }

     

     

    }

     

     

    f2.Show();

     

     

    spd.test.m_port = cb1_textbox.Text;

    spd.test.m_baudRate = cb2_textbox.Text;

    spd.test.m_dataBit = cb3_textbox.Text;

    spd.test.m_parity = cb4_textbox.Text;

    spd.test.m_stopBit= cb5_textbox.Text;

    spd.test.m_flowControl = cb6_textbox.Text;

     

     

    f2 =

     

    {

     

    spd =

     

    {

    InitializeComponent();

    cb1_textbox.Text = x.test.m_port;

    cb2_textbox.Text = x.test.m_baudRate;

    cb3_textbox.Text = x.test.m_dataBit;

    cb4_textbox.Text = x.test.m_parity;

    cb5_textbox.Text = x.test.m_stopBit;

    cb6_textbox.Text = x.test.m_flowControl;

     

    }

     

     

     

    {

    InitializeComponent();

     

    }

     

     

     

     

    {

     

     

     

     

     

     

     

     

     

    • Edited by Hemanth52 Wednesday, August 25, 2010 7:08 AM
    Wednesday, August 25, 2010 6:48 AM

Answers

  • hi Srinu,

    Try using a class that can warp that singleton. say class1 is singleton; class2 will wrap class1. since class2 is not singleton; you can decide its  'Scope' inside the application. its like sinlgeton acts as DB; your class2 acts as Context. use class2 to do CRUD.

    SanNeo

     

    Wednesday, August 25, 2010 1:47 PM

All replies

  • i can't understand

    why don't you want to use singleton. i don't see any reason to follow other way


    Manish Sati
    Wednesday, August 25, 2010 6:54 AM
  • Hi manish,

    thanks for ur reply..

    i am in learing process. i want to do in some other process. in singleton we can use this object through out application.suppose i want to use the object in 2classes only out of 5classes.i want to pass the instance of object in two different classes.

    Pls help me..

    thanks again

    Wednesday, August 25, 2010 7:13 AM
  • hi Srinu,

    Try using a class that can warp that singleton. say class1 is singleton; class2 will wrap class1. since class2 is not singleton; you can decide its  'Scope' inside the application. its like sinlgeton acts as DB; your class2 acts as Context. use class2 to do CRUD.

    SanNeo

     

    Wednesday, August 25, 2010 1:47 PM