473,326 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 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 9317
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.