471,066 Members | 1,337 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

OOP Accessing data from a different method

20 16bit
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. }
  6. public void DisplayAccounts()
  7. {
  8. // Process AcctsList from ReadAccounts above.
  9. }
How do I do this? I'm sure there is a simple explanation to this.
Thanks in advance! Any help is really appreciated.
Feb 1 '21 #1
1 2958
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. }
  8.  public void DisplayAccounts()
  9. {
  10.      var accounts = ReadAccounts();
  11.      // do display stuff here.
  12. }
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
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.