Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,564 members, 7,809,057 topics. Date: Thursday, 25 April 2024 at 10:02 PM

Help! Im Stuck With This C# Program - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help! Im Stuck With This C# Program (1600 Views)

Please Guys Help Me With This C++ Program Exam Question. It's Urgent. / Fix This C++ Code If You can / Please Programmars Help Me Out With This C# Program! I Have Test Tommorrow!!!!! (2) (3) (4)

(1) (Reply) (Go Down)

Help! Im Stuck With This C# Program by Nobody: 6:21pm On Apr 01, 2016
I'm a beginner programmer learning C#I coded two apps, which I named the Contact app and list app. What the first one does is printing out the input that was passed into the parameter. Here"s the code:
using System;

namespace contact
{
public class Contact
{
private string fullName;
private string phoneNumber;

public string getName()
{
return fullName;
}

public void setName(string fullname)
{
this.fullName = fullname;
}

public string getPhonenumber()
{
return phoneNumber;
}

public void setPhoneNumber(string phoneNumber)
{
this.phoneNumber = phoneNumber;
}

}

public class ContactManager
{
public static void Main()
{
Contact cnt = new Contact();

cnt.setPhoneNumber("01234"wink;
cnt.setName("John Smith"wink;

Console.WriteLine("Name:" + cnt.getName());
Console.WriteLine("phone number:" + cnt.getPhonenumber());
}
}
}


The second one makes use of C# List to add and remove elements:
using System;

using System.Collections.Generic;

namespace ArrayListApplication
{
class Program
{


public static void printMyList(List<int> arr)
{
for (int i = 0; i < arr.Count; i++)
{
Console.WriteLine(arr[i]);
}
}

public static void Main(string[] args)
{

int[] myNumbers = new int[] {10, 11, 12,13,14,15};

// Its not possible to expand C# arrays. I had to convert to a flexible List
// copying array into list
List <int> mylist = new List<int>(myNumbers);

printMyList(mylist); // printing array

for (int i = 101; i <= 105; i++)
{
mylist.Add(i); // adding new elements
}

printMyList(mylist); // printing added elements

// removing elements

mylist.RemoveAt(1); // remove first element
mylist.Remove(15); // remove 15
mylist.RemoveAt(mylist.Count - 1); // remove last element

printMyList(mylist); // printing remaining elements after removal

Console.ReadKey();
}
}
}


Now, I tried combining these two programs so that I create a List of Contact with a loop that runs and asks the user
to enter a name, a phone number, and add all the info into the list. This is what I came up with but it's not working:



using System;

namespace contact
{
public class Contact
{
private string fullName;
private string phoneNumber;

public string getName()
{
return fullName;
}

public void setName(string fullname)
{
this.fullName = fullname;
}

public string getPhonenumber()
{
return phoneNumber;
}

public void setPhoneNumber(string phoneNumber)
{
this.phoneNumber = phoneNumber;
}

}

public class ContactManager
{
public static void Main()
{

List<Contact> nameslist = new List<Contact>();

Console.Write("Enter your name"wink;

string name = Console.ReadLine(); // I've stored it into a string now

nameslist.Add(name);

int i = 0;

while(name!="Quit"wink // I want the program to end if a user
enters "Quit".


Console.WriteLine(i);i++;
}
}
}



I ended up getting this message from VS
Error 1 The best overloaded method match for
'System.Collections.Generic.List<ConsoleApplication4.contact.Contact>.Add(ConsoleApplication4.contact.Contact)'
has some invalid arguments c:\users\user\documents\visual studio
2010\Projects\ConsoleApplication4\ConsoleApplication4\Program.cs 49 17 ConsoleApplication4


Error 2 Argument 1: cannot convert from 'string' to
'ConsoleApplication4.contact.Contact' c:\users\user\documents\visual
studio 2010\Projects\ConsoleApplication4\ConsoleApplication4\Program.cs 49 31 ConsoleApplication4

Pls what do I do to get things right? I think I've really lost my track.

larisoft
Re: Help! Im Stuck With This C# Program by Nobody: 7:10pm On Apr 01, 2016
Why are you converting from string to a public class....
Re: Help! Im Stuck With This C# Program by Nobody: 7:25pm On Apr 01, 2016
Use

List<string> namelist = new List<string>();

add

using System.Collections.Generic;
Re: Help! Im Stuck With This C# Program by larisoft: 7:42pm On Apr 01, 2016
Well done, bro.

The problem with your code is that you declared your list to accept only contact objects (List<Contact>wink, but you are trying to force a string object into it (namelist.add(name)).

Now, this will work;
List<Contact> nameslist = new List<Contact>();

Console.Write("Enter your name" );

string name = Console.ReadLine(); // I've stored it into a string now

//we are adding a contact object to a contacts list
Contact contact = new Contact();
contact.setFullName(name);
nameslist.Add(contact);

int i = 0;

while(name!="Quit"wink // I want the program to end if a user enters "Quit".

Console.WriteLine(i);
i++;

------OR---
//use a simple string list instead of a contact list

List<String> nameslist = new List<String>();

Console.Write("Enter your name" );

string name = Console.ReadLine(); // I've stored it into a string now

nameslist.Add(name);

int i = 0;

while(name!="Quit"wink // I want the program to end if a user enters "Quit".

Console.WriteLine(i);
i++;
}
Re: Help! Im Stuck With This C# Program by larisoft: 7:45pm On Apr 01, 2016
Also note that you should use if(!name.Equals("Quit")) as opposed to (name!="Quit").

Keep it up.
Re: Help! Im Stuck With This C# Program by mbatuku1: 8:46pm On Apr 01, 2016
Thanks larisoft and basbone. I will work on the suggestions and update the house.
Re: Help! Im Stuck With This C# Program by elfico(m): 4:18pm On Apr 04, 2016
mbatuku2:


int[] myNumbers = new int[] {10, 11, 12,13,14,15};

// Its not possible to expand C# arrays. I had to convert to a flexible List
// copying array into
You can use an ArrayList for a dynamic array
Re: Help! Im Stuck With This C# Program by Nobody: 8:21am On Apr 05, 2016
Try to combine the three class together .

(1) (Reply)

Where To Learn Software Development And Engineering In Abuja On Part Time / Business Startup / Abeg App Has Been Hacked

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 23
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.