0

I've declared a list in a class. I want to access the list from another class. How can I access the list from a module of another class?

// ClsUser.cs
namespace WebLll.ApiPayment.BusinessObject
{
    public class ClsUser
    {
        Data.MyEntity db = new Data.MyEntity("MyEntity1");

        public List<Data.GetPaymentRslt> BRIlstTxn = db.GetPayment(obj.PaymentCode, dtFrom, dtTo, obj.PaymentMode).ToList(); 

        //... remaining code
    }
}
// clsWebLllAPI.cs
namespace WebLll.ApiPayment.BusinessObject
{
    public class clsWebLllAPI : clsBaseApi
    {
        public void Initialize(api_rule_setup obj)
        {
            // access the BRIlstTxn here
        }

    }
}
user4221591
  • 2,084
  • 7
  • 34
  • 68

2 Answers2

2

Since the list is public you can simply create an instance of the class and access it like follow,

ClsUser clsuser=new ClsUser();
List<Data.GetPaymentRslt> mylist=clsuser.BRIlstTxn; // Simply access PUBLIC field

From MSDN

Accessing a field in an object is done by adding a period after the object name

But as good programming practice,I suggest you to use Accessors over making a field public (need to know why, check this)

Suggestion code :

// ClsUser.cs
namespace WebLll.ApiPayment.BusinessObject
{
    public class ClsUser
    {
        Data.MyEntity db = new Data.MyEntity("MyEntity1");

        private List<Data.GetPaymentRslt> BRIlstTxn = db.GetPayment(obj.PaymentCode, dtFrom, dtTo, obj.PaymentMode).ToList(); 

        // Only GET . Provide protection over setting it
        public  List<Data.GetPaymentRslt> brIlstTxn{
           get
           {
               return BRIlstTxn;
           }
        }

        //... remaining code
    }
}
// clsWebLllAPI.cs
namespace WebLll.ApiPayment.BusinessObject
{
    public class clsWebLllAPI : clsBaseApi
    {
        public void Initialize(api_rule_setup obj)
        {
             ClsUser clsuser=new ClsUser();
             List<Data.GetPaymentRslt> mylist=clsuser.brIlstTxn; // Now you are accessing GET accesor rather than field directly
        }

    }
}
Community
  • 1
  • 1
Kavindu Dodanduwa
  • 12,193
  • 3
  • 33
  • 46
0

You can use Dot, member access operator to access public/internal/protected data member (list) of other class.

namespace WebLll.ApiPayment.BusinessObject
{
    public class clsWebLllAPI : clsBaseApi
    {
        public void Initialize(api_rule_setup obj)
        {
            ClsUser clsUser = new ClsUser ();
            var lst = clsUser.BRIlstTxn;
        }

    }
}
Adil
  • 146,340
  • 25
  • 209
  • 204