Hi,
I created a class name -InterestedCompany.cs

namespace TestingProject
{
    class InterestedCompany
    {
        string interests;

        public string Interests
        {
            get { return interests; }
            set { interests = value; }
        }
    }
}

I have one more class name - Student.cs

namespace TestingProject 
{
    class Student :Person
    {
        InterestedCompany x;

        internal InterestedCompany X
        {
            get { return x; }
            set { x = value; }
        }
     }
}

in the main form I try to do

InitializeComponent();
            Student v = new Student();
            v.X.Interests = "x";

but i get run time error why ?
how can i access into insertcompany ?

Recommended Answers

All 4 Replies

as InterestedCompany is a class in the student class you have to create the instance of it... preferably create constructor for student class:

public Student()
{
   this.x = new InterestedCompany();
}

or create an instance like that:

InterestedCompany x = new InterestedCompany();

inside of Student class...
I'm not sure which solution is better... I personally usually for such things create constructor...

.....
 internal InterestedCompany X
        {
            get { 
                    if(x==null)
                        x=new InterestedCompany();
                    return x; 
                  }
            set { x = value; }
        }
....

thank's it is working!

You're welcome. I'm glad you got it working. Please mark this thread as solved if you have found an answer to your question and good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.