![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jul 2008
Posts: 51
Rep Power: 1
![]() |
best way to write this program help
ok im trying to make a program that for example i have....a list of activities to do
so for this example lets say 35-40 activities ranging from sports or normal everyday activities to other stuff and i want to click a button that randomly picks one of those activities and i have some checkboxes that allow the certain categories to be picked from so my question is what is the most efficient way of doing this im only halfway through my first year in computer science so i dont know the best ways of doing things yet some help would be appreciated thanks |
|
|
|
|
|
#2 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,039
Rep Power: 5
![]() |
Re: best way to write this program help
There are various ways, depending on whether you're using a database or not, among other things. However, the general idea is to build a list of all candidate activities. Thus, if the user checks 'sports' (which has things like soccer, frisbee, cycling, basketball, etc), 'entertainment' (stuff like movies, go to the pub, etc), and 'chores' (vaccuum the house, clean the kitchen, bathe the cat, etc), you'd add all the associated activities to a list. If the list has zero items in it (this should only be possible if you have empty categories, or if the user selects none), you're done. If you have one or more items in your list, then you generate a random integer in the range [0..n) (this means 'from 0 to n, including zero but not including n'), and pick the appropriate item from the list. Assuming you're using either an array or a standard .NET collection type (such as List<T>), you can directly use the random number as an index into your list (this is why you want it to be from zero to, but not including, the number of elements).
Hopefully this gives you an idea of where to start for this.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Jul 2008
Posts: 51
Rep Power: 1
![]() |
Re: best way to write this program help
hey thanks a bunch i already started on it and the trouble im getting is that i made an array called activities and i have all the activities in there
and then on the form i have checkboxes that are like entertainment chores blah blah etc so when all = checked it just randomly chooses an index number of the activities array but heres when the code starts getting messy i have an if statement for every possibility and i used the same array but just took out the category items for example: string[] activites = new string[6];
activities[0] = "BasketBall";
activities[1] = "FootBall";
activities[2] = "Watch Movie";
activities[3] = "Go to pub";
activities[4] = "Vacuum";
activities[5] = "Do dishes";
if(chkchores.checked == true && chksports.checked == true && entertainment == false)
{
activites = new activities[4];
activities[0] = "basketball"
activities[1] = "football"
activities[2] = "vacuum"
activites[3] = "do dishes"
}pretty much like that for all the possibilities, i was wondering if there was an easier way of doing that *WITHOUT a database* forgot to mention that. |
|
|
|
|
|
#4 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,039
Rep Power: 5
![]() |
Re: best way to write this program help
You still need some easy way to determine what category an activity belongs to. Hardcoding it like you've done might seem easier at first- and probably is, if you've only got a few activities- but it quickly becomes very hard to maintain.
There are two general approaches here. The first is easier, so let's cover that here. Basically, the idea is to maintain separate lists, and process those to build the temporary list after the user selects one or more categories: C# Syntax (Toggle Plain Text)
List<string> rather than string[] (ie, a collection rather than an array), you can dynamically add elements. This would let you ask the user for new activities, add them, and even let you save them to disk and load them up next time you run the program.However, there is another shortcoming with this approach, and that is that it doesn't let you add new categories. Those are totally hardcoded, and the way to get around this is to store the category with the string, hence the second approach. If you've covered classes in your programming course(s) yet, an object makes perfect sense here. If each object has a property that lets you determine its category, and you have a list of such objects, you can quickly iterate through the list and build up a temporary list of all objects that fall into a specific category (or group of categories). Of course, you need some method to ensure the categories are uniform; if the user adds "basketball" as "sport", and "baseball" as "sports", you've got a problem. This is where a good UI comes into play. You can do something like populate a ComboBox with the various categories, and a user can select from these. Also, if a user can plainly see there is already a "sports" category, they're unlikely to add the similarly-named "sport" category.Anyways, if you understand the first method, and are ready to work on the second, let me know. There's no sense in me explaining more at this point if I'm just confusing you. ![]()
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Jul 2008
Posts: 51
Rep Power: 1
![]() |
Re: best way to write this program help
thank you so much i was wondering if there was a way to put them all in categories like that and i knew that List had to do with it i just did know you could put arrays in lists so i will give this a try and i can message you back in pm? would that be ok?
|
|
|
|
|
|
#6 | ||
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,039
Rep Power: 5
![]() |
Re: best way to write this program help
Quote:
When you use the List<T> type, it is a generic type; these collections all live in the System.Collections.Generics namespace. The 'T' is a placeholder for the actual type, which is why you get things like List<int> or List<string>. If you've ever used C++, it's a similar idea to templates. Basically, the idea is that you can write generic code to support similar operations across different types. For example, a collection of integers is going to have much the same properties as a collection of strings. You're going to have methods to add items, remove items, perhaps sort them, and so on. Since the logic will be identical, using generics lets you write the code once, and use it for different types. Of course, there are some requirements in some cases. For example, if the collection supports sorting into ascending order, then the item type needs to have some mechanism for comparing two items (otherwise, it doesn't know which is 'greater' and which is 'lesser'); .NET handles this by requiring you implement the appropriate interface (IComparable, in this case). If you haven't covered interfaces yet, don't worry about it.There are non-generic collections (ie, List, Queue, etc instead of List<T>, Queue<T> etc) that live in the System.Collections namespace. These hold collections of type object, which means that you can store any object in them. The downside to this, though, is that it's not type-safe. If I get an object from a List, it can (potentially) be any type, whereas if I get it from a List<string>, then I know it's a string. I don't have to cast it as I would for the non-generic List, possibly risking an exception in the event it can't be cast to a string.Anyways, the reason I used a List<T> is because you can add to it, without needing to worry about the size. With an array, you set the size when you declare it, and then you're basically stuck with that. Technically, you can resize .NET arrays, but if you know you'll need this capability, it's better to go with a collection that's designed to support this in the first place. Collections like List<T> will dynamically grow to accommodate additional elements as you add them, without you needing to worry about it at all. On the other hand, if you know you do not need this ability, an array might be a better choice, since it may be more efficient (and it will make it harder for the caller to add items, which is generally a good thing).Quote:
[edit] To prove my point about not being an expert, if you look at the code example in my previous post, the first method should be 'private', not 'public'. Oops. [/edit]
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
||
|
|
|
|
|
#7 |
|
Programmer
Join Date: Jul 2008
Posts: 51
Rep Power: 1
![]() |
Re: best way to write this program help
okay sorry it took so long i got lazy but i implemented the list into my program so i have it all working but heres my next question
is there a way to search through all the string in the list and lets say if i typed in a search textbox "ball" it would find "football","baseball","basketball" and if there was any other activity with ball in it, it would find that and place them into a new list and you could call that list to display? |
|
|
|
|
|
#8 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,039
Rep Power: 5
![]() |
Re: best way to write this program help
Yup. You can use the
Contains method of the string class to do this.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Jul 2008
Posts: 51
Rep Power: 1
![]() |
Re: best way to write this program help
so how would you do that like
List<string> activities = new List<string>();
for(int i = 0; i < sports.length; i++)
{
blah blah blah
}
//now i want it to loop through the list looking for "ball"
for(int i = 0; i < activities.count; i++)
{
string s1 = activites[i] ?? //stuck there dont know how to find a single value in the list
string s2 = txtsearch.text;
bool b;
b = s1.Contains(s2);
list<string> search = new list<string>();
if(b = true)
{
search.add(s1);
}
}
//im using a label to display
lblDisplay.text = search[rand.Next(search.count)];so would that work or am i missing something or how would i go about doing that putting the string into a new list then displaying all that had "ball" in the string |
|
|
|
|
|
#10 |
|
Programmer
Join Date: Jul 2008
Posts: 51
Rep Power: 1
![]() |
Re: best way to write this program help
well i tried that and i havent been able to check if it works yet because for some reason it said my assemblyversion is invalid and i didnt touch it since the last time i worked on it so i have no clue how to fix that....
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| hello, I'd like to write a program for my work. | blake_jl | Community Introductions | 13 | Nov 23rd, 2007 4:31 PM |
| Trying to write a program to count words | boxcar182 | C | 5 | Nov 30th, 2006 2:55 AM |
| Language display in program | Prm753 | C++ | 3 | May 30th, 2006 5:45 PM |
| Creating a program to test a program | sixstringartist | C | 8 | Jan 21st, 2006 1:15 PM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C++ | 0 | Mar 2nd, 2005 4:12 PM |