none
use a class in another class RRS feed

  • Question

  • Hello Guys.

    I created a class in my program to the name of Specifications 

    namespace Student_demo
    {
        public class Specifications
        {
            public event nonevalue onnonevaluename;
            public event nonevalue onnonevaluefamily;
            public event nonevalue onnonevaluefather;
            //these evants are for clean text of validation labeles if have value
            public event nonevalue onvaluename;
            public event nonevalue onvaluefamily;
            public event nonevalue onvaluefather;
            //private fields
            private string _name;
            private string _family;
            private string _father;
            private string _errormessage;
            private int _errornumber = 0;
            //public properties
            public string name
            {
                get
                {
                    return _name;
                }
                set
                {
                    if (value.Length == 0)
                    {
                        // send thread to the handle event
                        onnonevaluename();
                        //onnonevalue += new nonevalue(student_onnonevalue);
                    }
                    else
                    {
                        _name = value;
                        // send thread to the handle event
                        onvaluename();
                    }
    
                }
            }
            public string Family
            {
                get
                {
                    return _family;
                }
                set
                {
                    if (value.Length == 0)
                    {
                        // send thread to the handle event
                        onnonevaluefamily();
                        //onnonevalue += new nonevalue(student_onnonevalue);
                    }
                    else
                    {
                        _family = value;
                        // send thread to the handle event
                        onvaluefamily();
                    }
    
                }
            }
            public string Father
            {
                get
                {
                    return _father;
                }
                set
                {
                    if (value.Length == 0)
                    {
                        // send thread to the handle event
                        onnonevaluefather();
                        //onnonevalue += new nonevalue(student_onnonevalue);
                    }
                    else
                    {
                        // send thread to the handle event
                        _father = value;
                        onvaluefather();
                    }
                }
            }
            public string Errormessage
            {
                get
                {
                    return _errormessage;
                }
                set 
                {
                    _errormessage = value;
                }
            }
            public int Errornumber
            {
                get
                {
                    return _errornumber;
                }
                set 
                {
                    _errornumber = value;
                }
            }
            //send a new student to the database
            
        }
    }


    And  i use the class in another class that is in following code:

    namespace Student_demo
    {
        
        public class student
        {
            public Specifications StudentSpecifications
            {
                get;
                set;
            }
            //send a new student to the database
            public bool add(/* out int errornumber,out string errormessage */) 
            {
                bool result = true;
                if (StudentSpecifications.name == null || StudentSpecifications.Family == null || StudentSpecifications.Father == null)
                {
                    result = false;
                    return result;
                }
                else 
                {
                    SqlCommand objcommand = new SqlCommand();
                    objcommand.Connection = Constr.getconnection();
                    objcommand.CommandText = "USP_INS_STUDENT";
                    objcommand.CommandType = CommandType.StoredProcedure;
                    objcommand.Parameters.Add("@NAME", SqlDbType.NVarChar, 50);
                    objcommand.Parameters.Add("@FAMILY", SqlDbType.NVarChar, 50);
                    objcommand.Parameters.Add("@FATHER", SqlDbType.NVarChar, 50);
                    objcommand.Parameters.Add("@ERRORNUMBER", SqlDbType.Int);
                    objcommand.Parameters.Add("@ERRORMESSAGE", SqlDbType.NVarChar, 50);
                    objcommand.Parameters["@NAME"].Value = StudentSpecifications.name;
                    objcommand.Parameters["@FAMILY"].Value = StudentSpecifications.Family;
                    objcommand.Parameters["@FATHER"].Value = StudentSpecifications.Father;
                    objcommand.Parameters["@ERRORNUMBER"].Direction = ParameterDirection.ReturnValue;
                    objcommand.Parameters["@ERRORMESSAGE"].Direction = ParameterDirection.Output;
                    objcommand.Connection.Open();
                    try
                    {
                        objcommand.ExecuteNonQuery();
                        if (Convert.ToInt32(objcommand.Parameters["@ERRORNUMBER"].Value) != 0)
                            throw new Exception();
                    }
                    catch (Exception)
                    {
                        result = false;
                    }
                    finally
                    {
                        objcommand.Connection.Close();
                    }
                    //errornumber = Convert.ToInt32(objcommand.Parameters["@ERRORNUMBER"].Value);
                    //errormessage = objcommand.Parameters["@ERRORMESSAGE"].Value.ToString();
                    StudentSpecifications.Errornumber = Convert.ToInt32(objcommand.Parameters["@ERRORNUMBER"].Value);
                    StudentSpecifications.Errormessage = objcommand.Parameters["@ERRORMESSAGE"].Value.ToString();
                    return result;
                }            
            }
            //void student_onnonevalue()
            //{
            //    _errormessage = "Invalid Value"; 
            //}
            ////void student_onnonevalue(string nonevalue)
            //{
            //    throw new NotImplementedException();
            //}
        }
    }
    


    When i use classes in my main program in following list

    namespace Student_demo
    {
        public partial class RegisterForm : Form
        {
            public RegisterForm()
            {
                InitializeComponent();
            }
            //public event argumenttransfer onargumenttransfer;
    
            private void btncancel_Click(object sender, EventArgs e)
            {
                Close();
            }
    
            private void btnclear_Click(object sender, EventArgs e)
            {
                txtname.Text = txtfather.Text = txtfamily.Text = txtcourse.Text = "";
                txtname.Focus();
            }
    
            private void btnsave_Click(object sender, EventArgs e)
            {
                //onargumenttransfer(txtname.Text,txtfamily.Text,txtfather.Text);
                student std = new student();
                std.StudentSpecifications.onnonevaluename += new nonevalue(StudentSpecifications_onnonevaluename);
                std.StudentSpecifications.onnonevaluefamily += new nonevalue(StudentSpecifications_onnonevaluefamily);
                std.StudentSpecifications.onnonevaluefather += new nonevalue(StudentSpecifications_onnonevaluefather);
                std.StudentSpecifications.onvaluename += new nonevalue(StudentSpecifications_onvaluename);
                std.StudentSpecifications.onvaluefamily += new nonevalue(StudentSpecifications_onvaluefamily);
                std.StudentSpecifications.onvaluefather += new nonevalue(StudentSpecifications_onvaluefather);
                std.StudentSpecifications.name = txtname.Text;
                std.StudentSpecifications.Family = txtfamily.Text;
                std.StudentSpecifications.Father = txtfather.Text;
                if (std.add())
                {
                    MessageBox.Show("عملیات با موفقیت انجام شد", "ثبت دانشجو", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
                    validationfields();
                }
                else
                    validationfields();
            }
    
            void StudentSpecifications_onvaluefather()
            {
                lblnonevaluefather.Text = "";
            }
    
            void StudentSpecifications_onvaluefamily()
            {
                lblnonevaluefamily.Text = "";
            }
    
            void StudentSpecifications_onvaluename()
            {
                lblnonevaluename.Text = "";
            }
    
            void StudentSpecifications_onnonevaluefather()
            {
                lblnonevaluefather.Text = "*";
            }
    
            void StudentSpecifications_onnonevaluefamily()
            {
                lblnonevaluefamily.Text = "*";
            }
    
            void StudentSpecifications_onnonevaluename()
            {
                lblnonevaluename.Text = "*";
            }
    
            
            void validationfields() 
            {
                if (lblnonevaluename.Text == "*" || lblnonevaluefamily.Text == "*" || lblnonevaluefather.Text == "*")
                    lblresult.Text = "خطا! تعدادی از فیلد ها بدون مقدار هستند";
                else
                    lblresult.Text = "";
            }
        }
    }

    And i run it i have the following code:

    null refrence exception was unhandled

    please Help me:

    all my source projects

    Monday, February 11, 2013 4:02 PM

Answers

  • You never actually create your StudentSpecifications. 

    I would reocmmend adding this to your student constructor:

     public class student
        {
           // Add a constructor
            public student()
            {
               // Construct this object
               this.StudentSpecifications = new Specifications();
            }
    
            public Specifications StudentSpecifications
            {
                get;
                private set; // You can make this private
            }


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Monday, February 11, 2013 4:38 PM
    Moderator

All replies

  • And i run it i have the following code:

    null refrence exception was unhandled

    please Help me:

    all my source projects

    Where ? At which part of your code ?

    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Monday, February 11, 2013 4:06 PM
  • You never actually create your StudentSpecifications. 

    I would reocmmend adding this to your student constructor:

     public class student
        {
           // Add a constructor
            public student()
            {
               // Construct this object
               this.StudentSpecifications = new Specifications();
            }
    
            public Specifications StudentSpecifications
            {
                get;
                private set; // You can make this private
            }


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Monday, February 11, 2013 4:38 PM
    Moderator
  • thank u so much.

    your guess is really true;

    i dont initialize student's constuctor.

    you help me very well.

    Monday, February 11, 2013 6:27 PM