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.

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.

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).

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:

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:

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.

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.

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.