-
January 20th, 2010, 10:41 PM
#16
Re: Display highlighted data from Listbox in Textboxes
Originally Posted by CarlMartin
If you read my post, I am trying to learn C# on my own, so I bought a book, and I am trying to do the problems in the book as I go through it. I need to learn C#, so I thought I would do it this way.
I did read your post. Just checking to make sure it isn't homework.
Since it isn't homework, I'd like to suggest that you try to do it using BigEd's approach. (or try it BigEd's way and then the array way or vice versa).
BigEd is really correct that you wouldn't want to track items in a list using an array. He's giving a little insight about how it would really be done.
-
January 20th, 2010, 10:44 PM
#17
Re: Display highlighted data from Listbox in Textboxes
I understand that. I did do it his way after reading the post, but I will not be able to let it go without doing it the original way, just to be able to do it. I need to prove to myself that it can be done, lol. Thanks.
-
January 20th, 2010, 10:57 PM
#18
Re: Display highlighted data from Listbox in Textboxes
Originally Posted by CarlMartin
I understand that. I did do it his way after reading the post, but I will not be able to let it go without doing it the original way, just to be able to do it. I need to prove to myself that it can be done, lol. Thanks.
Great. That's the attitude. Best of luck.
-
January 21st, 2010, 12:36 AM
#19
Re: Display highlighted data from Listbox in Textboxes
Check to see if the next chapter of the book asks...
Now change your Address Book program, so that it uses Objects rather than an Array
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
January 21st, 2010, 07:19 PM
#20
Re: Display highlighted data from Listbox in Textboxes
Originally Posted by BigEd781
Instead of using raw arrays to store information you should create a class to mirror a "contact". This was you simply store "Contact" objects, override "ToString()" so that they display properly in the list box, and when you retrieve them you have all of the information you need to populate your UI. I would also consider making those textboxes into a single UserControl, but that is not completely necessary as this looks like a school project. An example:
Code:
class Contact
{
// these may have different accessibility modifiers if needed
public string Name { get; set; }
public string PhoneNumber { get; set; }
public string Address { get; set; }
public Contact( string name, string phoneNumber, string address )
{
this.Name = name;
this.PhoneNumber = phoneNumber;
this.Address = address;
}
}
// snip
Contact c = new Contact( "Ed", "123-4567", "123 Fake Street" );
myListView.Items.Add( c );
This is obviously a very simple example, so change things as you see fit.
I think that, if I can use classes, but use an array to store the data (per the requirement), then thic will still technically be correct. It does say simply that an array must be used to "store" the date and that is it. Time to figure this all out. Thanks.
-
January 21st, 2010, 07:21 PM
#21
Re: Display highlighted data from Listbox in Textboxes
Originally Posted by CarlMartin
I think that, if I can use classes, but use an array to store the data (per the requirement), then thic will still technically be correct. It does say simply that an array must be used to "store" the date and that is it. Time to figure this all out. Thanks.
Yup, that is exactly what I suggested a few responses ago.
I don't mean to belabor the point, but there is no sense in learning how to do something the wrong way first. If you need to learn how arrays are used then do something that is suited to that task, not something like this.
-
January 21st, 2010, 07:30 PM
#22
Re: Display highlighted data from Listbox in Textboxes
Originally Posted by BigEd781
Yup, that is exactly what I suggested a few responses ago.
I don't mean to belabor the point, but there is no sense in learning how to do something the wrong way first. If you need to learn how arrays are used then do something that is suited to that task, not something like this.
I agree with you there, I just hate to not follow directions. It's just me. Anyway, let me ask you a question. Do you think the better way to do this, and still use an array to store the data, is to use a class as you suggested, or to use an array of structures? I can see both ways working, but I am not sure which is the preferred and easiest method...Remember, I am still trying to learn all this . Thanks.
-
January 21st, 2010, 07:33 PM
#23
Re: Display highlighted data from Listbox in Textboxes
Structures are rarely used in C#. They have value-type semantics and are generally used to express very simple data stores. Beyond that you should always be using classes.
Also, I dropped out of college because they had me doing nonsense work which didn't even make sense and was flat out wrong. When I did things the proper way teachers would literally take points away because I didn't "follow the directions exactly to the letter" instead of rewarding me for going above and beyond what they were looking for. So yeah, I don't put much stock into "directions" which make no sense.
-
January 21st, 2010, 07:35 PM
#24
Re: Display highlighted data from Listbox in Textboxes
Thanks for the advice. I was trying to avoid going to class by teaching myself, as you can see, for some of the same reasons. I like how the directions can still be followed, but it can still be done a better way with this solution. Thanks. I will work on this, and if I can't figure something out, I will be back.
-
January 21st, 2010, 07:38 PM
#25
Re: Display highlighted data from Listbox in Textboxes
Put it this way; if you're assignment is actually preventing you from learning how this stuff works in the real world, something is wrong
-
January 21st, 2010, 07:41 PM
#26
Re: Display highlighted data from Listbox in Textboxes
Yeah, there have been many lessons in this book that are designed to illustrate the material, but are way more complicated to use than other methods...many lessons! I guess it is good to learn how to use all of the tools available, even if they will not be used very often in real life...just in case, I guess. Thanks. I will let you know how this progresses. I have not gotten to using classes yet, so I do not know what to expect. I will skip forward in this book and see what I can come up with. Thanks.
-
January 22nd, 2010, 02:07 PM
#27
Re: Display highlighted data from Listbox in Textboxes
Wow, this is super complicated. After reading the chapter in the book on Classes, I am super confused. What I do not understand is how does the class get the data input into the textboxes by the user? I do not understand how to do that part at all.
-
January 22nd, 2010, 02:30 PM
#28
Re: Display highlighted data from Listbox in Textboxes
The following is an example of a Contact class, which is just a way to encapsulate some data about a Person.
Code:
using System.Collections.ObjectModel;
class Program
{
static void Main(string[] args)
{
Collection<Contact> contacts = new Collection<Contact>();
Contact person1 = new Contact("Mario", "Catch", "123 NillyWilly Ln", "555-555-5555");
Contact person2 = new Contact("Luigi", "IHadTo", "124 NillyWilly Ln", "555-555-5556");
contacts.Add(person1);
contacts.Add(person2);
foreach (var contact in contacts)
{
contact.Show();
}
Console.ReadLine();
}
}
public class Contact
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string PhoneNumber { get; set; }
public Contact() { }
public Contact(string firstName, string lastName, string address, string phoneNumber)
{
FirstName = firstName;
LastName = lastName;
Address = address;
PhoneNumber = phoneNumber;
}
public void Show()
{
Console.WriteLine(
"Person Information:\n-Name: {0}\n-Address: {1}\n-PhoneNumber: {2}\n\n",
FirstName + ' ' + LastName,
Address,
PhoneNumber);
}
}
With that, you now have access to any Contact in the contacts Collection. You can edit their properties, you can also set your form's textbox's to their properties.
ie:
Code:
textBoxFirstName.Text = contacts[0].FirstName;
-
January 22nd, 2010, 02:32 PM
#29
Re: Display highlighted data from Listbox in Textboxes
My issue is adding the contact info INTO the Class FROM a textbox, not getting the info out and displaying it into a textbox. And I need to use an array to store the data, also.
-
January 22nd, 2010, 02:43 PM
#30
Re: Display highlighted data from Listbox in Textboxes
In the example the data is fed to the new object through its constructor:
Code:
Contact person1 = new Contact("Mario", "Catch", "123 NillyWilly Ln", "555-555-5555");
Inside of the constructor the data is assigned to class level variables/properties for future access.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|