473,325 Members | 2,480 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,325 software developers and data experts.

generic collection

2
hi'
i have a following code. AddMember method is being called in the main method.
when i run this code ,it traverse and allow me to enter data through the keyboard.
but when i try to display them,they didnt come up.
memp.count =1 but it has to come up 2.
can you find the mistake and post me the answer.

Expand|Select|Wrap|Line Numbers
  1. public static void AddMember()
  2.         {
  3.  
  4.             for(int j=0;j<2;j++)
  5.             {
  6.  
  7.                 string name1 = "", add1 = "", emal1 = "";
  8.  
  9.  
  10.                 Console.Write("Please enter name  : ");
  11.                 name1 = Console.ReadLine();
  12.  
  13.                 Console.Write("Please enter Address  : ");
  14.                 add1 = Console.ReadLine();
  15.  
  16.                 Console.Write("Please enter Email  : ");
  17.                 emal1 = Console.ReadLine();
  18.  
  19.  
  20.                 Member memobj = new Member(name1, add1, emal1);
  21.                 Member[] memb = new Member[2];
  22.  
  23.                 memb[j] = memobj;
  24.  
  25.  
  26.  
  27.                 List<Member> memp = new List<Member>(2);
  28.  
  29.  
  30.                 memp.Add(memb[j]);
  31.  
  32.  
  33.  
  34.                 Console.Write("Current contents:{0} ",memp.Count);
  35.                foreach(Member mem in memp)
  36.                   Console.Write(mem + " ");
  37.                 Console.ReadLine();
  38.  
  39.  
  40.  
  41.             } 
  42.  
  43.  
  44.  
  45.         }
  46.  
  47.     }
Mar 16 '07 #1
4 978
SammyB
807 Expert 512MB
hi'
but when i try to display them,they didnt come up.
memp.count =1 but it has to come up 2.
can you find the mistake and post me the answer.
}
You need to think about what items go inside the For loop and which items go outside the For loop. Look at your code again:
Expand|Select|Wrap|Line Numbers
  1. public static void AddMember()
  2. {
  3.     for (int j = 0; j < 2; j++)
  4.     {
  5.         string name1 = "", add1 = "", emal1 = "";
  6.         Console.Write("Please enter name : ");
  7.         name1 = Console.ReadLine();
  8.         Console.Write("Please enter Address : ");
  9.         add1 = Console.ReadLine();
  10.         Console.Write("Please enter Email : ");
  11.         emal1 = Console.ReadLine();
  12.  
  13.         Member memobj = new Member(name1, add1, emal1);
  14.         Member[] memb = new Member[2];
  15.         memb[j] = memobj;
  16.  
  17.         List<Member> memp = new List<Member>(2);
  18.         memp.Add(memb[j]);
  19.  
  20.         Console.Write("Current contents:{0} ", memp.Count);
  21.         foreach (Member mem in memp)
  22.             Console.Write(mem + " ");
  23.         Console.ReadLine();
  24.     }
  25. }
You have Data Entry, Object creation, Array creation, Array assignment, List creation, List Add, and Data display. You put all of these in the For loop. Which ones really belong there? HTH --Sam
Mar 16 '07 #2
kenobewan
4,871 Expert 4TB
Welcome to the site. Your For loop counts 0 then 1, i.e. 2. Is that the problem you are asking about? More information please.
Mar 16 '07 #3
thaya
2
[quote=thaya]hi'
i have a following code.

after entering two data, obj1.count has to be increased by one.
but it resets the count and displays 1 again when i try via F10 key.

Every time i enter new data ,it erases previous one and add latest one.

Expand|Select|Wrap|Line Numbers
  1.  public static void Main(string[] args)
  2.         {
  3.             Console.WriteLine("Enter a number\n");
  4.  
  5.           //this prompts user to create how many times loop is 
  6.          //going to execute.
  7.  
  8.             int num = System.Int32.Parse(Console.ReadLine());
  9.  
  10.  
  11.             do
  12.             {
  13.                Console.WriteLine("Enter a name\n");
  14.  
  15.                string nam1 = Console.ReadLine();
  16.  
  17.                Member mem = new Member(nam1);
  18.  
  19.                List<Member> obj1 = new List<Member>();
  20.  
  21.                obj1.Add(mem);   // Problem may be here
  22.  
  23.                Console.WriteLine("The number of element {0}", obj1.Count);
  24.                Console.WriteLine(" This is your name  {0}", mem.name);
  25.  
  26.                num--;                
  27.             } while (0 < num);
  28.  
  29.  
  30.  
  31.  
  32.             Console.ReadLine();
  33.         }
i just altered the previous code
Mar 17 '07 #4
SammyB
807 Expert 512MB
That's good that you are learning different kinds of loops, but you need to think about what goes into the loop; ie, what needs to be repeated and what needs to just be done one time. Think about what happens each time you go through your loop:
Expand|Select|Wrap|Line Numbers
  1. do
  2. {
  3.     Console.WriteLine("Enter a name\n");
  4.     string nam1 = Console.ReadLine();
  5.     Member mem = new Member(nam1);
  6.     List<Member> obj1 = new List<Member>();
  7.     obj1.Add(mem); // Problem may be here: NOT
  8.     Console.WriteLine("The number of element {0}", obj1.Count);
  9.     Console.WriteLine(" This is your name {0}", mem.name);
  10.     num--; 
  11. } while (0 < num);
  12.  
  1. Get a name
  2. create a Member object
  3. create a new collection of members (and thus get rid of the previous collection)
  4. add the single Member object to the collection
  5. write the data
So, no matter how many time you go through the loop, how many items will be in the collection?
Mar 17 '07 #5

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

Similar topics

8
by: JAL | last post by:
Here is my first attempt at a deterministic collection using Generics, apologies for C#. I will try to convert to C++/cli. using System; using System.Collections.Generic; using System.Text; ...
2
by: dcew | last post by:
Here's what I'm trying to understand; how can you store a generic collection in a variable/field? If I have an abstract generic collection class as follows... public abstract class...
8
by: Steven Cummings | last post by:
Hello, I've scoured this usenet group and didn't find anything specific to my problem, so hopefully this won't be a repeated question. I'm all but certain it's not. I would like to *declare*...
3
by: snesbit | last post by:
I have a structure called SearchAreaListItem. The structure has some properties. The application implements this as a collection.generic.list(of SearchAreaListItem) I load the collection up ...
2
by: AdawayNoSpam | last post by:
Said that I have the following class Class MyRootClass(Of T) End Class Class MySubClass1(Of T) Inherits MyRootClass(Of T) End Class
2
by: Angel Mateos | last post by:
I have this structure: Class ElemBase Class Elem1 : Inherits ElemBase Class ColecBase(Of GenElem As {ElemBase, New}) : Inherits System.ComponentModel.BindingList(Of GenElem) Class Colec1...
4
by: =?Utf-8?B?QkogU2FmZGll?= | last post by:
We have a class that has a public property that is of type List<T>. FXCop generates a DoNotExposeGenericLists error, indicating "System.Collections.Generic.List<Tis a generic collection designed...
1
by: Kuldeep | last post by:
Framework: Visual Studio 2005, ASP.NET Programing Language: C#.NET I am using a Generic List Collection to fetch a particular master data from the database. Once collected, I use this Collection...
5
by: sloan | last post by:
I've noticed alot of people tacking on a "T" for a generic abled version of an older class. Ex: 1.1 Code IDataStore
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
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
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...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.