Marshal
Marshal

Reputation: 1247

Inheritance with C#

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.

Upvotes: 0

Views: 343

Answers (8)

bizah
bizah

Reputation: 247

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"
        };

Upvotes: 0

Tony The Lion
Tony The Lion

Reputation: 63310

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.

Upvotes: 0

dknaack
dknaack

Reputation: 60556

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.

Upvotes: 1

Abbas
Abbas

Reputation: 14432

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.

Upvotes: 1

Farid Movsumov
Farid Movsumov

Reputation: 12735

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

Upvotes: 3

ChrisF
ChrisF

Reputation: 137188

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.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1503729

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; }
}

Upvotes: 5

Amar Palsapure
Amar Palsapure

Reputation: 9680

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.

Upvotes: 0

Related Questions