A class & B Class

patrick

Well-known member
Joined
Dec 5, 2021
Messages
302
Programming Experience
1-3
A Class:
namespace ABC
{
Public class claA
{
   Public DataTable TableA; // global
   TableA = new Table();

   Public Table Function()
  {
      TableA.Column.Add("Lable");
      TableA.Add("ABC");
      return TableA;
   }
 }
}


B Class:
namespace ABC
{
  public class B
  {
     public void FuncionB()
     {
        claA A = new claA();
        DataTable dt = A.TableA;
//<=== Error : value is null
     }
   }
}

In Class B,, claA A = claA();
DataTable dt = A.TableA; <=== Error: value is null

How to solve??
 
Last edited:
You need to provide more detail, or provide minimal code that compiles and runs. As it is now, your A Class code block will not compile.

When does the line: TableA = new Table(); get executed? If it any time after the other constructor for claA, then the error you are getting is completely expected. This is because your B Class line 7 creates a new instance of claA. Since it is a new instance, all of its fields are set to default values. As part of creating a new instance, the claA constructor is called. If that line for initializing the TableA field is not in the constructor, then the field will still continue to be null. So you get the error on line 8.
 
You need to provide more detail, or provide minimal code that compiles and runs. As it is now, your A Class code block will not compile.

When does the line: TableA = new Table(); get executed? If it any time after the other constructor for claA, then the error you are getting is completely expected. This is because your B Class line 7 creates a new instance of claA. Since it is a new instance, all of its fields are set to default values. As part of creating a new instance, the claA constructor is called. If that line for initializing the TableA field is not in the constructor, then the field will still continue to be null. So you get the error on line 8.
I can't code..

To put it in words, I don't know.
Could you please give me some example code?
 
You need to provide more detail, or provide minimal code that compiles and runs. As it is now, your A Class code block will not compile.

When does the line: TableA = new Table(); get executed? If it any time after the other constructor for claA, then the error you are getting is completely expected. This is because your B Class line 7 creates a new instance of claA. Since it is a new instance, all of its fields are set to default values. As part of creating a new instance, the claA constructor is called. If that line for initializing the TableA field is not in the constructor, then the field will still continue to be null. So you get the error on line 8.

Class claA:
namespace ABC
{
Public class claA
{
   Public DataTable TableA = new Table(); // global

   Public Table Function()
  {
      TableA.Column.Add("Lable");
      TableA.Add("ABC");
      return TableA;
   }

   Public Table Functuon1()
   {
        Function();
    }
 }
}

As Your Answer, I am modify code..

Class B:
namespace ABC
{
  public class B
  {
     public void FuncionB()
     {
        claA A = new claA();
        DataTable dt = A.TableA;
//<=== Error : value is null
     }
   }
}

But, claA = new claA();
DataTable dt = A.TableA;
//<=== Error: value is null

How to soulv?
Please Example Source....
 
Last edited:
But, claA = new claA();
DataTable dt = A.TableA;
//<=== Error: value is null
I find that highly unlikely.

Please show screenshot of the error.
 
I find that highly unlikely.

Please show screenshot of the error.

It is not Error. But value is null.
But, claA = new claA();
DataTable dt = A.TableA; // called null value
i desperately need help😭


The returned value should be "ABC"
Because, In Class claA,,
TableA.Column.Add("Lable");
TableA.Add("ABC");
 
Please show screenshot of error or provide minimal compilable code that demonstrates the error.
 
Please show screenshot of error or provide minimal compilable code that demonstrates the error.
Form.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            Class1 claA = new Class1();
            dt = claA.Table;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Class1 claA = new Class1();
            claA.FunctionA();
        }
    }
}


Class1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace WindowsFormsApplication5
{
    class Class1
    {
        private DataTable TableA;
        public DataTable Table
        {
            get
            {
                return TableA;
            }
            set
            {
                this.TableA = value;
            }
        }  
        public void FunctionA()
        {
            DataTable dt = new DataTable("Table");
            dt.Columns.Add("Col1", typeof(string));
            DataRow dr = dt.NewRow();
            dr["Col1"] = "ABC";
            Table = dt;
        }
    }
}


Data (ABC) has been entered into the DataTable.
q1.png



BUT, DatTable is null value

q2.png


I can't code..
 
As I said, by default the members of a class are initialized to their default values. In the case of reference types like a DataTable, the default value is null. So in your Class1, the TableA is set to null whenever you create a new instance.

So on line 22 of your Form1 you create a new instance. Therefore TableA for that instance is null.

The instance that you create on line 27 of Form1 is different the instance you create on line 22. Any work you do on lines 27-28 has no impact on any future instances.

If you want both button1_Click() and button2_Click() to share the same instance, just create one instance and store a reference to that instance in your constructor. In other words, delete line 27, then move line 22 to between lines 14 and 15. (E.g. Just like what line 5 on post #4 looks like.)
 
As I said, by default the members of a class are initialized to their default values. In the case of reference types like a DataTable, the default value is null. So in your Class1, the TableA is set to null whenever you create a new instance.

So on line 22 of your Form1 you create a new instance. Therefore TableA for that instance is null.

The instance that you create on line 27 of Form1 is different the instance you create on line 22. Any work you do on lines 27-28 has no impact on any future instances.

If you want both button1_Click() and button2_Click() to share the same instance, just create one instance and store a reference to that instance in your constructor. In other words, delete line 27, then move line 22 to between lines 14 and 15. (E.g. Just like what line 5 on post #4 looks like.)


If Class3 is added. and did
DataTable dt = new DataTable();
Class1 claA = new Class1();
dt = claA.Table; <===== In Class3 ( Class3 is added )

Also , I also did
DataTable dt = new DataTable();
Class1 claA = new Class1();
dt = claA.Table;
in Class1. //<=== In Class1
Is the value null returned?
 
As I said, by default the members of a class are initialized to their default values. In the case of reference types like a DataTable, the default value is null. So in your Class1, the TableA is set to null whenever you create a new instance.

So on line 22 of your Form1 you create a new instance. Therefore TableA for that instance is null.

The instance that you create on line 27 of Form1 is different the instance you create on line 22. Any work you do on lines 27-28 has no impact on any future instances.

If you want both button1_Click() and button2_Click() to share the same instance, just create one instance and store a reference to that instance in your constructor. In other words, delete line 27, then move line 22 to between lines 14 and 15. (E.g. Just like what line 5 on post #4 looks like.)

Is there any other way other than this?
 
If Class3 is added. and did
DataTable dt = new DataTable();
Class1 claA = new Class1();
dt = claA.Table; <===== In Class3 ( Class3 is added )

Also , I also did
DataTable dt = new DataTable();
Class1 claA = new Class1();
dt = claA.Table;
in Class1. //<=== In Class1
Is the value null returned?
Yes. Because you are making new instances.
 
Yes, but they go against good object oriented design.
The reason why I didn't just give the how-to is because our OP has propensity for just taking/copying the answer and running with it without taking into consideration the impact of such things.

Anyway the answer would be to use a singleton design pattern. Consider though that even though the design pattern has been around for years (consider the venerable global variable of days old) and even documented in the GoF's seminal "Design Patterns" book, the past 20+ years of collective object oriented wisdom had figured out that the singleton pattern is an anti-pattern. It needs to be used with a lot of caution and forethought.

So my recommendation is use dependency injection, and inject a single instance of claA into the instances of your Form1 and the theoretical new Class3, as well as any other future classes that needs to have a reference to that single instance. This will make for much more testable code when you start writing unit tests. It will also make documenting your code easier because you will know that you are dependent on that ClassA instance.
 
Back
Top Bottom