472,783 Members | 1,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

OOP Accessing data from a different method

22 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. }
  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
2 8745
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
Arushi
7 Nibble
The simplest solution would be to make the List a member variable of the class:

Expand|Select|Wrap|Line Numbers
  1. public class MyClass
  2. {
  3.     private List<AcctRec> AcctsList = new List<AcctRec>();
  4.  
  5.     public void ReadAccounts()
  6.     {
  7.         // Read the accounts into the list
  8.     }
  9.  
  10.     public void DisplayAccounts()
  11.     {
  12.         // Process the list
  13.     }
  14. }
Nov 17 '22 #3

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

Similar topics

3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
6
semanticnotion
by: semanticnotion | last post by:
Hi sir i want to transform the data of one table into another through foreign key but the following error come to my browser Here is my code and data base structure. CREATE TABLE IF NOT...
10
by: fadhili | last post by:
<?php /*the points system for this Q&A has to be passed from question to question. We are going to do this by passing it in the URL. After the first question, the current score is passed...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.