locked
copying a reference RRS feed

  • Question

  • so I have the following code:
    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace RefKoe
    {
        public partial class Form1 : Form
        {
            string gaay;
            public Form1()
            {
                InitializeComponent();
                gaay = "not yet changed";
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                joukko jouko = new joukko(ref gaay);
                MessageBox.Show(gaay);
            }
        }
        public class joukko
        {
            string str;
            public joukko(ref string ide)
            {
                str = ide;
                str = "changed now";
            }
        }
    }
    My goal ofcourse was to change the original string gaay but this did not succeed.
    I understand that if I had changed the value of ide, that would've got the job done but I cannot do that in my project.
    So how to "store" the reference so one can later change it ??
    Saturday, June 13, 2009 3:53 PM

Answers

  • Hi denarced,

    We can create a property named "ParentForm" in monthCalendar form, which associate with Form1, then we can change form1 variable in monthCalendar via this property. Like the code:

    Form1:
            public string DataString;

            private void Form1_Load(object sender, EventArgs e)
            {
                MonthForm frm = new MonthForm();
                frm.ParentForm = this;
                frm.Show();
            }

    monthcalendar Form:
         public partial class MonthForm : Form
        {
            private Form1 _parentForm;
            public Form1 ParentForm
            {
                get
                {
                    return _parentForm;
                }
                set
                {
                    _parentForm = value;
                }
            }

            private void button1_Click(object sender, EventArgs e)
            {
                _parentForm.DataString = "select value";
            }
        }


    Best regards,
    Guang-Ming Bian - MSFT


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    • Marked as answer by denarced Tuesday, June 16, 2009 7:36 AM
    Tuesday, June 16, 2009 7:21 AM

All replies

  • Hi,

    What you could do is make your gaay variable static so that you could change it in your joukko constructor as below :-

    Form1.gaay = "changed now";

    Hope this helps.

    Paul Diston


    http://www.smartmobiledevice.co.uk/
    Saturday, June 13, 2009 4:26 PM
  • that would solve the problem .. yes
    thank you for the solution

    I'd of course like to things in object oriented fashion.
    In this case it would mean that the classes are as independent as possible.
    Passing a reference would've achieved that.
    This one doesn't quite, but hey .. ugly solution's better than none :D
    Saturday, June 13, 2009 4:55 PM
  • Hi,

    Maybe you can explain a little more about what you are trying to achieve? From the code you provided it seems you could just have two individual methods returning individual strings.

    Thanks

    Paul Diston


    http://www.smartmobiledevice.co.uk/
    Saturday, June 13, 2009 4:57 PM
  • So I have this application for mobile phones.
    In one of the forms there's a date.
    There was some bad method for changing it so I thought I'd create another form in which there's the monthcalendar.
    The user would then choose the date, push a button and then he's back in the original form.
    So the original form has a datetime-member variable.
    I thought I'd slip a reference to that variable as an argument to the constructor of monthcalendar-form.
    and that's what my original question was about.
    How to save that reference for later manipulation.
    Sunday, June 14, 2009 4:17 PM
  • Hi denarced,

    We can create a property named "ParentForm" in monthCalendar form, which associate with Form1, then we can change form1 variable in monthCalendar via this property. Like the code:

    Form1:
            public string DataString;

            private void Form1_Load(object sender, EventArgs e)
            {
                MonthForm frm = new MonthForm();
                frm.ParentForm = this;
                frm.Show();
            }

    monthcalendar Form:
         public partial class MonthForm : Form
        {
            private Form1 _parentForm;
            public Form1 ParentForm
            {
                get
                {
                    return _parentForm;
                }
                set
                {
                    _parentForm = value;
                }
            }

            private void button1_Click(object sender, EventArgs e)
            {
                _parentForm.DataString = "select value";
            }
        }


    Best regards,
    Guang-Ming Bian - MSFT


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    • Marked as answer by denarced Tuesday, June 16, 2009 7:36 AM
    Tuesday, June 16, 2009 7:21 AM
  • this works .. thank you!
    Tuesday, June 16, 2009 7:36 AM