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. - public static void AddMember()
-
{
-
-
for(int j=0;j<2;j++)
-
{
-
-
string name1 = "", add1 = "", emal1 = "";
-
-
-
Console.Write("Please enter name : ");
-
name1 = Console.ReadLine();
-
-
Console.Write("Please enter Address : ");
-
add1 = Console.ReadLine();
-
-
Console.Write("Please enter Email : ");
-
emal1 = Console.ReadLine();
-
-
-
Member memobj = new Member(name1, add1, emal1);
-
Member[] memb = new Member[2];
-
-
memb[j] = memobj;
-
-
-
-
List<Member> memp = new List<Member>(2);
-
-
-
memp.Add(memb[j]);
-
-
-
-
Console.Write("Current contents:{0} ",memp.Count);
-
foreach(Member mem in memp)
-
Console.Write(mem + " ");
-
Console.ReadLine();
-
-
-
-
}
-
-
-
-
}
-
-
}
4 978
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: - public static void AddMember()
-
{
-
for (int j = 0; j < 2; j++)
-
{
-
string name1 = "", add1 = "", emal1 = "";
-
Console.Write("Please enter name : ");
-
name1 = Console.ReadLine();
-
Console.Write("Please enter Address : ");
-
add1 = Console.ReadLine();
-
Console.Write("Please enter Email : ");
-
emal1 = Console.ReadLine();
-
-
Member memobj = new Member(name1, add1, emal1);
-
Member[] memb = new Member[2];
-
memb[j] = memobj;
-
-
List<Member> memp = new List<Member>(2);
-
memp.Add(memb[j]);
-
-
Console.Write("Current contents:{0} ", memp.Count);
-
foreach (Member mem in memp)
-
Console.Write(mem + " ");
-
Console.ReadLine();
-
}
-
}
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
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.
[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. - public static void Main(string[] args)
-
{
-
Console.WriteLine("Enter a number\n");
-
-
//this prompts user to create how many times loop is
-
//going to execute.
-
-
int num = System.Int32.Parse(Console.ReadLine());
-
-
-
do
-
{
-
Console.WriteLine("Enter a name\n");
-
-
string nam1 = Console.ReadLine();
-
-
Member mem = new Member(nam1);
-
-
List<Member> obj1 = new List<Member>();
-
-
obj1.Add(mem); // Problem may be here
-
-
Console.WriteLine("The number of element {0}", obj1.Count);
-
Console.WriteLine(" This is your name {0}", mem.name);
-
-
num--;
-
} while (0 < num);
-
-
-
-
-
Console.ReadLine();
-
}
i just altered the previous code
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: -
do
-
{
-
Console.WriteLine("Enter a name\n");
-
string nam1 = Console.ReadLine();
-
Member mem = new Member(nam1);
-
List<Member> obj1 = new List<Member>();
-
obj1.Add(mem); // Problem may be here: NOT
-
Console.WriteLine("The number of element {0}", obj1.Count);
-
Console.WriteLine(" This is your name {0}", mem.name);
-
num--;
-
} while (0 < num);
-
- Get a name
- create a Member object
- create a new collection of members (and thus get rid of the previous collection)
- add the single Member object to the collection
- write the data
So, no matter how many time you go through the loop, how many items will be in the collection?
Sign in to post your reply or Sign up for a free account.
Similar topics
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;
...
|
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...
|
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*...
|
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 ...
|
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
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |