468,117 Members | 1,422 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,117 developers. It's quick & easy.

OOP Accessing data from a different method

12 Byte
I am a professional mainframe programmer, and I am trying to learn .Net. I have an OOO problem that is stumping me. I can get around it, but it would look like amateur hacking. I would like to do it the OOO way.

Simply put:
In one method (ReadAccounts), I am reading an Access table into a List (AcctsList). I don't have a problem with that; that works.
In another method (DisplayAccounts), I would like to READ that list, and then process it.
I tried to make the list "global", but I am tripping up on the language. Here is my code:
Expand|Select|Wrap|Line Numbers
  1. public void ReadAccounts()
  2. {
  3. var AcctsList = new List<AcctRec>(); // How do I make THIS AcctsList available to other programs?
  4. }
  5.  
  6. public void DisplayAccounts()
  7. {
  8. // Process AcctsList from ReadAccounts above.
  9. }
  10.  
How do I do this? I'm sure there is a simple explanation to this.
Thanks in advance! Any help is really appreciated.
Dave
Feb 1 '21 #1
1 2326
Joseph Martell
198 Expert 128KB
So you have a couple of options here. It partially depends on the context of what you are doing and where your code lives.

Without more information, I would probably recommend changing the ReadAccounts method to return the accounts list:

Expand|Select|Wrap|Line Numbers
  1. public IEnumerable<AcctRec> ReadAccounts()
  2. {
  3.      var AcctsList = new List<AcctRec>(); 
  4.      //populate your account list
  5.      return AcctsList;
  6. }
  7.  
  8.  public void DisplayAccounts()
  9. {
  10.      var accounts = ReadAccounts();
  11.      // do display stuff here.
  12. }
  13.  
  14.  
This should address your immediate question.
Feb 27 '21 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Stevey | last post: by
56 posts views Thread by spibou | last post: by
2 posts views Thread by Allan Ebdrup | last post: by
13 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.