contact porgramme

help needed!!!!!

Page 1 of 1

2 Replies - 1404 Views - Last Post: 06 September 2007 - 03:12 PM Rate Topic: -----

#1 gogole  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 17-July 07

contact porgramme

Post icon  Posted 04 September 2007 - 01:46 PM

i'm kinda new to c# currently trying to make a programme that saves contact information into a file and is also able to
search through an array(instantiated with contact info from the file) and locate a partcular contact. so far i have been able to
create methods that i think will get the job done , honestly i suck at using access modifiers and returns and thus got stuck connecting all
the methods to make the programme work assuming there are no bugs.please review the code provided and try to connect the
methods ( would be glad to learn from your examples on using returns and access modifiers).question : can arrays be returned ? i was aiming at
at returning an array with code like this : return nameArray[250];didn't include the main cus there is nothing on it.
using System;
using System.IO;

namespace contacts_program
{
	/// <summary>
	/// Description of ManipulateFile.
	/// </summary>
	public class ManipulateFile
	{
		public ManipulateFile()
		{
		}
		public void ContactInfo(){
			string[] contactArray = new string [5];
			string contactName,contactEmail,contactNumber,contactAddress,contactGroup;
			Console.WriteLine("Enter name of new contact : ");
			contactName = Console.ReadLine();
			contactName = contactName.Trim();
			Console.WriteLine("Enter email of new contact : ");
			contactEmail = Console.ReadLine();
			contactEmail = contactEmail.Trim();
			Console.WriteLine("Enter number of new contact : ");
			contactNumber = Console.ReadLine();
			contactNumber = contactNumber.Trim();
			Console.WriteLine("Enter address of new contact : ");
			contactAddress = Console.ReadLine();
			contactAddress =contactAddress.Trim();
			Console.WriteLine("choose group of new contact : |friends|family|vip|others|");
			contactGroup =Console.ReadLine();
			contactGroup = contactGroup.Trim();
			short i = 0;
			contactArray[i] = contactName;
			++i;
			contactArray[i] = contactEmail;
			++i;
			contactArray[i] = contactNumber;
			++i;
			contactArray[i] = contactAddress;
			++i;
			contactArray[i] = contactGroup;
		}
		public string GetGroup(string []array){
			string file = "";
			string fileName;
			string group =array[4];
			bool again = true;
			while (again == true){
				switch (group){
						
					case ("friends") :
						file ="friends";
						again = false;
						break;
					case ("family") :
						file = "family";
						again = false;
						break;
					case ("vip") :
						file = "vip";
						again = false;
						break;
					case ("others"):
						file ="others";
						again = false;
						break;
					default:
						{
							Console.WriteLine("Invalid group : enter a valid group");
							again = true;
							break;
						}
						
				}
			}
			
			fileName = file+"_file";
			return fileName;
		;
		}
		public void InfoSaved(string fileName,string []array){
			bool itExist = false;//would have to work on the detection of  file with same name
			//cus this wouldn't work, itExist is always falsewhen method is called
			if(itExist == true){
				FileStream contactFile = new FileStream(fileName,FileMode.Open);
				StreamWriter writer = new StreamWriter(contactFile);
				for (int i = 0;i<=4;++i){
					writer.WriteLine(array[i]);
					
					
				}
				itExist =true;
				writer.Close();
				contactFile.Close();
			}
			else
			{
				FileStream contactFile = new FileStream(fileName, FileMode.Create);
				StreamWriter writer = new StreamWriter(contactFile);
				for (int i = 0;i<=4;++i){
					writer.WriteLine(array[i]);
				}
				itExist =true;
				writer.Close();
				contactFile.Close();
			}
			Console.WriteLine("contact saved in file : {0}",fileName);
			Array.Clear(array,0,array.Length);
			
		}
		
		
		public string SearchInfo(){
			string searchName = "";
			string file = "";
			string group ="";
			string concatReturn;
			bool again = true;
			while(again == true){
				Console.WriteLine("Enter name of contact to be searched for");
				searchName = Console.ReadLine();
				searchName = searchName.Trim();
				Console.WriteLine("Choose group to search for contact");
				Console.WriteLine("|friends|family|vip|others|");
				group = Console.ReadLine();
				group = group.Trim();
				
				switch (group){
					case ("friends") :
						file = "friends_file";
						again = false;
						break;
					case ("family") :
						file = "family_file";
						again = false;
						break;
					case ("vip") :
						file = "vip_file";
						again = false;
						break;
					case ("others"):
						file ="others_file";
						again = false;
						break;
					default:
						{
							Console.WriteLine("Invalid group : enter a valid group");
							again = true;
							break;
						}
				}
			}
			concatReturn = searchName+" "+file;
			return concatReturn;
		}
		public string LoadSearchFile(string concatReturn){
			string[] components;
			string fileName,searchName;
			components = concatReturn.Split(' ');
			searchName = components[0];
			fileName = components[1];
			FileStream openFile = new FileStream(fileName,FileMode.Open);
			StreamReader reader = new StreamReader(openFile);
			string[] nameArray = new string [250];
			short i = -1;
			while(reader.ReadLine() == string.Empty){
				nameArray[++i] = reader.ReadLine();
				reader.ReadLine();
				reader.ReadLine();
				reader.ReadLine();
				reader.ReadLine();
			}
			reader.Close();
			openFile.Close();
			return nameArray[250];//trying to return an array 
		}
		void SearchMechanism(string[] array,string concatReturn){
			string[] nameArray = new string[250];
			nameArray[250] = LoadSearchFile( concatReturn);
			string[] components;
			string fileName ="";
			string searchName;
			string[] searchArray = new string [1250];
			short j = -1;
			FileStream openFile = new FileStream(fileName,FileMode.Open);
			StreamReader reader = new StreamReader(openFile);
			while(reader.ReadLine() == string.Empty){
				array[++j] = reader.ReadLine();
			}
			reader.Close();
			openFile.Close();
			components = concatReturn.Split(' ');
			searchName = components[0];
			fileName = components[1];
			int index = System.Array.BinarySearch(array,searchName);
			Console.WriteLine("index is {0}",index);
		}
		


Is This A Good Question/Topic? 0
  • +

Replies To: contact porgramme

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4424
  • View blog
  • Posts: 12,293
  • Joined: 18-April 07

Re: contact porgramme

Posted 04 September 2007 - 09:59 PM

well there are certainly a lot of things going on here. So lets address a few of them and we shall see what we can do to get you moving in a good direction.

1) In your contactInfo method, why are you setting i to zero and then incrementing it? Just load straight if you want to do that. It will shorten up things a little and even makes it a little more readable. Here is how it would work...

// Initialize the array using variables
string[] contactArray = new string[]{contactName, contactEmail, contactNumber, contactAddress, contactGroup};



2) In getGroup there is a null statement towards the bottom that you might want to get rid of (a null statement is a single semicolon just sitting on a line by itself).

3) In your InfoSaved method I am assuming you are going to build in the file existence checking to make that boolean value true or false... because right now it will never go to your if statement, making it jump to the else. A file check would be simple. Just include a using statement at the top for System.IO namespace so you can get access to the File object. Then use a condition like this...

if (File.Exists("pathtofilegoeshere")) {
	  // do something if file exists
}
else { 
	  // do something if file doesn't exist
}



4) You loadsearchfile returning an array is pretty close. Remember that the return type is where you have to specify that it is returning an array and then all you have to do is return the array's name. Here is an example...

private void Form1_Load(object sender, EventArgs e)
{
	  // Returned array is stored into returnedarray variable
	  string[] returnedarray = returnarray();
	  MessageBox.Show(returnedarray[1]);
}

// Notice here I am returning "string[]"
private string[] returnarray()
{
	  string[] myarray = new string[] { "hello1", "hello2", "hello3" };

	  // Here I just return the name of the array
	  return myarray;
}



5) Your searchmechanism method really should specify a modifier on the beginning, even if public is assumed it is just good style. You may also want to use a different variable name than "array" since that is a keyword in C#. So you might want something like private void SearchMechanism(string[] myArray,string concatReturn){. Just be sure to modify the array name in the function too.

Now as for tying it all together, that is what your GUI does in a way. You create a bunch of methods in various events and tie them together by having the control then interact and modify one another. So put together a GUI and put these events in the proper buttons and such. For instance, put your loadsearch file in a button called "load file" or something. They click it and it will load the file, when it is done you can post a message to a label saying "File has been loaded!"

Hope this advice helps you out.
Was This Post Helpful? 0
  • +
  • -

#3 gogole  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 17-July 07

Re: contact porgramme

Posted 06 September 2007 - 03:12 PM

thanks matyr2 for your tips and advice, they have been really helpful . i haven't started using forms (just clearing up the assumption ) yet but definately but that is where i'm going with this programme. thanks alot though!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1