13 Replies - 1505 Views - Last Post: 01 February 2010 - 03:03 PM Rate Topic: -----

#1 Lunette  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-January 10

Quick question ( Storing user input to variable )

Post icon  Posted 01 February 2010 - 01:10 PM

Hello,

I am fairly new to programming ( I actually just started about a month ago ).
I'm currently learning C#, and I love it.

So I have this little project I must complete and I'm stuck ; I'll make this quick.

I will show you my code first :


This is my class Library ''Person'', in which I have the variables First Name, and Last Name.


using System;

namespace CGS
{

	public abstract class Person
	{
		private string FName = "";
		private string LName = "";

		public string FirstName
		{
			get
			{
				return FName;
			}
			set
			{
				FName = value;
			}
		}
		
		public string LastName
		{
			get
			{
				return LName;
			}
			set
			{
				LName = value;
			}
		}

		public Person()
		{
		}

		public Person(string First, string Last)
		{
			FName = First;
			LName = Last;
		}

		public override string ToString()
		{
			return FName + " " + LName;
		}
	}
}



And here's the ''Gallery'' class which contains the Instance of Artists.

using System;


namespace CGS
{

	public class Gallery
	{
		Gallery gal = new Gallery();

		Artists myArtists = new Artists();

		public void AddArtist(string FName, string LName, string ArtistID)
		{
			throw new System.NotImplementedException();
		}

		public void ListArtists()
		{
			throw new System.NotImplementedException();
		}
	}


}



I am now trying to get my Console Prototype to get the user input for the Artist's first name, and save the user's response to the local fName variable.

I know it might sound a little stupid, but I am just beginning to learn C#.

Here's what I tried in my program prototype which of course, does not work :


using System;
using CGS;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace ArtGallery
{
	class Program

	{
		private string FName;
		private string LName;
		private string ArtistID;

		 Gallery gal = new Gallery();

		static void Main(string[] args)
		{

			Artists artistCollection = new Artists();
			artistCollection.Add(new Artist());
			artistCollection.Add(new Artist());
			foreach (Artist myArtists in artistCollection)
			{
				myArtists.ToString();
			}
			Console.ReadKey();


		}
	}
}




And please don't be to hard on me, I'm really just trying to learn.

Thank you !

Is This A Good Question/Topic? 0
  • +

Replies To: Quick question ( Storing user input to variable )

#2 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Quick question ( Storing user input to variable )

Posted 01 February 2010 - 01:32 PM

You do not specify anywhere in your construction of an artist what you want the name to be:

artistCollection.Add(new Artist());



So, it will not be set. Now, if Artist extends Person (which seems like it might), then pass first and last name in the constructor.

artistCollection.Add(new Artist("John", "Doe"));



And that artist's name is John Doe.

Make sense?
Was This Post Helpful? 0
  • +
  • -

#3 Sailing_Nut  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 17
  • Joined: 29-January 10

Re: Quick question ( Storing user input to variable )

Posted 01 February 2010 - 01:34 PM

Here is one way that you can capture the user's input from the console. I am assuming that you want to allow them to enter more than one name.

string nameInput = " ";
Concole.WriteLine("Enter a blank name to terminate input");
while(nameInput != "")
{
	Console.Write("Enter First Name: ");
	FName = Console.ReadLine();
	if(FName != "")
	{
		// Do something here to save the name into your Artist class and add it to the list
	}
}



Hope this helps!

This post has been edited by Sailing_Nut: 01 February 2010 - 01:34 PM

Was This Post Helpful? 1
  • +
  • -

#4 Lunette  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-January 10

Re: Quick question ( Storing user input to variable )

Posted 01 February 2010 - 01:36 PM

Makes sense yes, but I get this error.

''Error 1 No overload for method 'Artist' takes '2' arguments''

Thanks for the help, I know these are pretty basic methods, but as I said, I'm really just starting and I can't quite seem to make this work.

EDIT : Thank you very much for the code Sailing Nut, this is exactly what I was looking for. Now I'll try to work my way around this and give you guys updates, thanks again.

This post has been edited by Lunette: 01 February 2010 - 01:40 PM

Was This Post Helpful? 0
  • +
  • -

#5 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Quick question ( Storing user input to variable )

Posted 01 February 2010 - 01:40 PM

Quote

''Error 1 No overload for method 'Artist' takes '2' arguments''


Basically, you have a constructor that expects 2 arguments. However, you are not passing 2 to it. So, find the instantiation site and ensure that you are passing proper parameters in your constructor.
Was This Post Helpful? 0
  • +
  • -

#6 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Quick question ( Storing user input to variable )

Posted 01 February 2010 - 01:40 PM

OK, let's just start from the beginning. Why is your Person class marked as abstract? It doesn't need to be.

Secondly, could you post a class of your Artist?

Thirdly, you do not need this line of code in your Gallery class:

Gallery gal = new Gallery(); //this line is not needed

Post the missing code and we'll be happy to help you.
Was This Post Helpful? 1
  • +
  • -

#7 Lunette  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-January 10

Re: Quick question ( Storing user input to variable )

Posted 01 February 2010 - 01:47 PM

Oh, wow. I forgot to post the Artist and Artists classes. -.-

Thank you FlashM.


Artist Class :

using System;

namespace CGS
{
	/// <summary>
	/// Summary description for Artist.
	/// </summary>
	public class Artist : Person
	{
		private string ArtistID
		{
			get
			{
				return ArtistID;
			}
			set
			{
				ArtistID = value;
			}
		}

	
		private string FName
		{
			get
			{
				return FName;
			}
			set
			{
				FName = value;
			}
		}

		private string LName
		{
			get
			{
				return LName;
			}
			set
			{
				LName = value;
			}
		}

		public override string ToString()
		{
			return FName + " " + LName;
		}
	}
}




Artists class :

using System;
using System.Collections;
using System.Collections.Generic;

namespace CGS
{
	/// <summary>
	/// Summary description for Artists.
	/// </summary>
	public class Artists : CollectionBase
	{
		public void Add(Artist newArtist)
		{
			List.Add(newArtist);
		}

		public void Remove(Artist newArtist)
		{
			List.Remove(newArtist);
		}

		public Artists()
		{
		}
		public Artist this[int artistIndex]
		{
			get
			{
				return (Artist)List[artistIndex];
			}
			set
			{
				List[artistIndex] = value;
			}
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#8 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Quick question ( Storing user input to variable )

Posted 01 February 2010 - 02:04 PM

I'm contemplating helping you out quite a bit but I think you're going to have a lot more fun fumbling in the nettles on your own.
I will say one thing though. You started a month ago and are already working with indexers. Very nice.

Quote

public Artist this[int artistIndex]
		{
			get
			{
				return (Artist)List[artistIndex];
			}
			set
			{
				List[artistIndex] = value;
			}
		}



Now, I will provide that help I mentioned if you actually want to hear it. So, let me know.
Was This Post Helpful? 0
  • +
  • -

#9 Lunette  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-January 10

Re: Quick question ( Storing user input to variable )

Posted 01 February 2010 - 02:17 PM

Yes, the program in which I'm in is actually very... compressed if I might say. We are asked to learn very quickly.

I couldn't agree more with you about how working on my own could be a lot more ''fun'' and challenging and this is exactly what I would do if I had more time to work with this project. (It's due for tomorrow).

So yes, I would very much appreciate your help for now. And I can't thank you enough, all of you for the help so far. It might not seem like a lot, but this is very very important for me, and I simply cannot fail this project.
Was This Post Helpful? 0
  • +
  • -

#10 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Quick question ( Storing user input to variable )

Posted 01 February 2010 - 02:23 PM

Your Person class should look like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CGS
{
	public class Person
	{
		private string FName;
		private string LName;


		public Person()
		{
		}

		public Person(string First, string Last)
		{
			FName = First;
			LName = Last;
		}


		public string FirstName
		{
			get
			{
				return FName;
			}
			set
			{
				FName = value;
			}
		}

		public string LastName
		{
			get
			{
				return LName;
			}
			set
			{
				LName = value;
			}
		}

		public override string ToString()
		{
			return FName + " " + LName;
		}

	}
}





Your Artist class should look something like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CGS
{
	public class Artist : Person
	{
		private string _artistId;

		public Artist()
		{
		}

		public Artist(string id, string firstName, string lastName)
		{
			_artistId = id;
			FirstName = firstName;
			LastName = lastName;
		}

		private string ArtistID
		{
			get
			{
				return _artistId;
			}
			set
			{
				_artistId = value;
			}
		}
	}
}




Your Artists class should look something like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace CGS
{
	public class Artists : CollectionBase
	{
		private List<Artist> artistsList;

		public Artists()
		{
			artistsList = new List<Artist>();
		}

		public void Add(Artist newArtist)
		{
			artistsList.Add(newArtist);
		}

		public void Remove(Artist newArtist)
		{
			artistsList.Remove(newArtist);
		}

		public Artist this[int artistIndex]
		{
			get
			{
				return artistsList[artistIndex];
			}
			set
			{
				artistsList[artistIndex] = value;
			}
		}
	}
}




Your Program.cs class should look something like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CGS
{
	class Program
	{
		static void Main(string[] args)
		{
			Artists artists = new Artists();

			string nameInput = null;
			Console.WriteLine("Enter a blank name to terminate input");

			while (nameInput != string.Empty)
			{
				Console.Write("Enter First Name: ");
				nameInput = Console.ReadLine();

				if (nameInput != string.Empty)
				{
					artists.Add(new Artist("1", nameInput, "SomeLastName"));
				}
				else
				{
					return;
				}
			}
		}
	}
}




Hope this helps. If you need any explanation feel free to ask.
Was This Post Helpful? 0
  • +
  • -

#11 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Quick question ( Storing user input to variable )

Posted 01 February 2010 - 02:43 PM

Very well. Let's get started.

First, your Person class:

using System;

namespace CGS
{

	public abstract class Person
	{
		private string FName = "";
		private string LName = "";

		public string FirstName
		{
			get
			{
				return FName;
			}
			set
			{
				FName = value;
			}
		}
	   
		public string LastName
		{
			get
			{
				return LName;
			}
			set
			{
				LName = value;
			}
		}

		public Person()
		{
		}

		public Person(string First, string Last)
		{
			FName = First;
			LName = Last;
		}

		public override string ToString()
		{
			return FName + " " + LName;
		}
	}
}



The very first thing you should ask yourself is why is this class abstract? I don't know the answer to this because I didn't write it. You need to know why you made this decision though so I await your answer.

Next, if there is no validation on the field you're working with, it's prettier to simply make them automatic properties. What this means is that it's a publicly exposed property that has no apparent private field - but in actuality, the compiler creates a special backing field for you behind the scenes (fun facts to know and tell).

Here's that class with this point illustrated:

using System;

namespace CGS
{

	public abstract class Person
	{
		public string FirstName
		{ get; private set; }
	   
		public string LastName
		{ get; private set; }

		public Person()
		{
		}

		public Person(string First, string Last)
		{
			FirstName = First;
			LastName = Last;
		}

		public override string ToString()
		{
			return FName + " " + LName;
		}
	}
}



So, in this case, the property is accessible to read, but it cannot be written to. This is usually the preferred way to do that.

Moving on, you have two constructors. What you want to do is take your most complex constructor which in this case is the one that accepts two string parameters, and make the less complex constructors call into it. That's done by using : this() syntax. Here's that class updated:

using System;

namespace CGS
{

	public abstract class Person
	{
		public string FirstName
		{ get; private set; }
	   
		public string LastName
		{ get; private set; }

		public Person() : this(string.Empty, string.Empty)
		{
		}

		public Person(string First, string Last)
		{
			FirstName = First;
			LastName = Last;
		}

		public override string ToString()
		{
			return FName + " " + LName;
		}
	}
}



Alright. Now when someone instantiates a derived class, the work-horse constructor is called.

Next on the list - your usage of overriding ToString(). This is sort of a bad idea for what you're using it for. Generally, you want your ToString() to provide useful information about the object's state. For example, you may write something like this:

public override string ToString()
{
	return String.Format("First Name={0}, Last Name={1}", FirstName, LastName);
}



In regards to retaining your original functionality, I would make another property for that and call it FullName:

using System;

namespace CGS
{

	public abstract class Person
	{
		public string FirstName
		{ get; private set; }
	   
		public string LastName
		{ get; private set; }

		public string FullName
		{
			 get { return String.Format("{0} {1}", FirstName, LastName); }
		}

		public Person() : this(string.Empty, string.Empty)
		{
		}

		public Person(string First, string Last)
		{
			FirstName = First;
			LastName = Last;
		}

		public override string ToString()
		{
			return String.Format("First Name={0}, Last Name={1}", FirstName, LastName);
		}
	}
}



Finally, convention in C# is that method parameters are camel-cased, so the final class becomes:

using System;

namespace CGS
{

	public abstract class Person
	{
		public string FirstName
		{ get; private set; }
	   
		public string LastName
		{ get; private set; }

		public string FullName
		{
			 get { return String.Format("{0} {1}", FirstName, LastName); }
		}

		public Person() : this(string.Empty, string.Empty)
		{
		}

		public Person(string firstName, string lastName)
		{
			FirstName = firstName;
			LastName = lastName;
		}

		public override string ToString()
		{
			return String.Format("First Name={0}, Last Name={1}", FirstName, LastName);
		}
	}
}



I made them slightly more explanatory too.

Now, why don't you follow these principles on your other code and post your updated source. After that, I'll be happy to help some more.

This post has been edited by MentalFloss: 01 February 2010 - 02:47 PM

Was This Post Helpful? 0
  • +
  • -

#12 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Quick question ( Storing user input to variable )

Posted 01 February 2010 - 02:53 PM

MentalFloss, that's a very good job you've done.


Just one expert advice. When overriding the ToString() method, it's better to use the StringBuilder and append text to its instance and finally return a stringBuilder.ToString() then adding the string objects with a + or string.Format, because string is a reference type and every time you add two string together with a plus sign, you create a new (third) string object that is placed in your memory. But this is really not something that a beginner should concern.
Was This Post Helpful? 0
  • +
  • -

#13 Lunette  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-January 10

Re: Quick question ( Storing user input to variable )

Posted 01 February 2010 - 02:54 PM

Wow. Where to start...

Your help is so appreciated right now, you guys have no idea. Thank you so much once again !
I will post my updated codes, but I feel like I can take it from here, after all, you guys did a lot for me today.

I'll go grab myself a couple redbulls and try to wrap up this whole project before tomorrow, and will give you the heads up.

:^: :^: :^:
Was This Post Helpful? 0
  • +
  • -

#14 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Quick question ( Storing user input to variable )

Posted 01 February 2010 - 03:03 PM

Quote

When overriding the ToString() method, it's better to use the StringBuilder and append text to its instance and finally return a stringBuilder.ToString() then adding the string objects with a + or string.Format, because string is a reference type and every time you add two string together with a plus sign, you create a new (third) string object that is placed in your memory. But this is really not something that a beginner should concern.


I suppose that the pertinence to the ToString() method itself should be taken into account. If ToString() is only ever called during debugging etc then the gain from instantiating a StringBuilder just to return the values might be slightly overkill. However, if this were a heavily used method then I can agree with you. It's a decision to be made against the context.

Quote

Wow. Where to start...

Your help is so appreciated right now, you guys have no idea. Thank you so much once again !
I will post my updated codes, but I feel like I can take it from here, after all, you guys did a lot for me today.

I'll go grab myself a couple redbulls and try to wrap up this whole project before tomorrow, and will give you the heads up.


It's refreshing to see a new coder with zeal. Good luck with your project.

Oh and remember, if you post your updated code, I can probably help you out some more.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1