0

I am trying to use inheritance to pass a string from one form to another using a ref.class. Im looking at it and to be honest I think this should work. But when debugging nothing comes up in my settings textBox...

Here is my reference Class:

    class Ref
{

    private String _url;

    public String Sett
    {

        set { _url = value; }
    }
    public String Gett
    {
        get { return _url; }
    }

and I am setting it from the main form and getting it from a settings form

Here is my main Form:

    private void webBrowser1_DocumentCompleted(object sender,WebBrowserDocumentCompletedEventArgs e)
        {Ref reff = new Ref();
        this.Text = webBrowser1.Document.Title + " - NHS Cerner Booking Dash";
        textBox1.Text = webBrowser1.Document.Url.ToString();
        reff.Sett = webBrowser1.Document.Url.ToString();

and finally this is my Settings form

    public void load()
    {
        Ref reff = new Ref();
        textBox3.Text = reff.Gett;
    }

Im sure this looks a bit complicated. So to clarify I want to take the URL from the main form and set it to my ref.class and then get it from the Ref.class to set it to a textBox in my Settings form. Thank you for looking.

Marshal
  • 1,177
  • 4
  • 18
  • 30
  • 2
    Why have you got two properties, one with a getter and one with a setter, rather than a single "Value" property with both a getter and a setter? Not that that would fix the problem... – Jon Skeet Feb 07 '12 at 14:17

8 Answers8

5

To start with, this has nothing to do with inheritance.

No, it definitely shouldn't work. You're creating two separate instances of Ref - so setting the value in one of them won't make it available in the other. You would need to create one instance of Ref and make both forms aware of it - so that they can use it as a shared communication channel.

Personally I don't think that's the best way of communicating between the forms anyway - you should consider using events, for example - but that's why it's not working at the moment.

As noted in a comment, it's very strange to have two separate properties. If I really wanted this behaviour, I would have written that class as:

public class MutableWrapper<T>
{
    public T Value { get; set; }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

Because you create new object when write "new"

public void load()
{
    Ref reff = new Ref();
    textBox3.Text = reff.Gett;
}

İf you debug you can see reff is empty..

this should help you

Community
  • 1
  • 1
Farid Movsumov
  • 12,350
  • 8
  • 71
  • 97
2

You are creating a new Ref object in each place so the value you set in webBrowser1_DocumentCompleted is on a different object to that retrieved in load.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
1

Why don't you just create a public property in the form that will be opened. This way you can set it just before you open it and use it further on.

Abbas
  • 14,186
  • 6
  • 41
  • 72
1

Puh, there to start.

At first i suggest you simplyfy your class.

public class Ref
{
    public string Url { get; set; }
}

But this is not the problem.

The Problem is that you have two different instances of your Ref class.

So you can do two things.

  1. If this is a setting form, you should save the value of Ref.Url to Application Settings. OR
  2. You must pass the instance of Ref to your Settings form.
dknaack
  • 60,192
  • 27
  • 155
  • 202
0

You don't need any Ref class for this. In your second form create a public property and while creating that form access this property and assign the value of URL.

Click here to see a post which does similar thing.

In Form2

 public string URL {get; set;}

In Form1

 Form2 theNewForm = new Form2();
 theNewForm.URL = "your url value";
 theNewForm.Show();

Hope this works for you.

Amar Palsapure
  • 9,590
  • 1
  • 27
  • 46
0

Ok, this is ugly, do this instead:

class Ref
{

    private String _url;

    public String URL
    {

        set { _url = value; }
        get { return _url; }
    }
}

Then you need to create a Ref instance inside your main class and pass it to your Settings class when you create it. Then you can fill it up inside your Settings class like so:

myref.URL = "whatevergoeshere";

When your Settings dialog is finished, you'll be able to look inside your Ref object and see what the URL is.

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
  • The annoying thing is I did this was Java in the past and it was really simple at the time. Was clean and now doing it with C# completely forgot everything. Thank you for your help as I will use this as a reference in the future when using a few objects. – Marshal Feb 07 '12 at 14:58
0

Okay you're really not using inheritance... and the issue you're having is the Ref's that you're declaring in two different places are not the same.

        private void webBrowser1_DocumentCompleted(object sender,WebBrowserDocumentCompletedEventArgs e)
        {
            Ref reff = new Ref();
            //not the same "Ref" as below
        }

        public void load()
        {
            Ref reff = new Ref();
            //not the same "Ref" as above
        }


        //consider using properties instead of members... you get the get/set for free
        public class Ref
        {
            public string URL { get; set; }
            public void Load()
            {
                //do your load set
                this.URL = "What you want";
            }
            //...
        }

        //you can also declare the constructor on the fly if you want.. or call the Load() method
        Ref myRef = new Ref()
        {
            URL = "What you want"
        };
bizah
  • 227
  • 1
  • 2
  • 9