Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# VisualStudio2010
Hey guys!
 
I'd like to write in a textbox a string and then give it a a value like "2". (Case 2Smile | :)
After this, I'd like to press a button and after a random script get one of the sentences i wrote. How can I achieve this?
 
Something like:
x=0
 
If button is clicked:
new case?!
new random(1,x)
 
Switch (x)
case 1:
 
case 2:
 
I am a newbie at this.. If I can help you to further understand my thought I'll do my best... Thanks Smile | :)
Posted 13-Dec-12 13:49pm
Comments
Matt T Heffron at 13-Dec-12 18:55pm
   
So it *seems* you want:
repeat {input a string and associate it with a (unique?)number} some number of times.
on button click:
pick one of the previously input strings at random and display it
 
Is that your problem?
Filipe Luna at 13-Dec-12 19:09pm
   
Exactly! How can I do that?
jibesh at 13-Dec-12 19:12pm
   
did you start writing the code? if not please start writing and paste the code where you stuck and what part of the code you dont understand. we dont code! we solve error!!
Sergey Alexandrovich Kryukov at 13-Dec-12 19:13pm
   
There is no scripting here. C# is not a scripting language, .NET is not a scripting platform...
--SA
Filipe Luna at 13-Dec-12 19:15pm
   
I just want to create a string, give it an int value and then randomly generate an int value and use it to get the string.
And another thing i needed to do would be to somehow save the generated strings after closing the program...
Sergey Alexandrovich Kryukov at 13-Dec-12 20:23pm
   
How is this "I just want" related to the issue? First of all, this is not scripting, everything else goes next. :-)
-SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can use a dictionary to associate ints with strings. Then you can look them up from there. So make the int the key, store the string, keep track of the ints you created, and look them up. A dictionary is perhaps a slightly complex container to learn about at this stage, but that's how you'd do it
  Permalink  
Comments
Filipe Luna at 13-Dec-12 19:34pm
   
Thanks again Chris!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

In C# you have set of Collection classes where you can store and retrieve data using a key or index.
 
refer these links to know more about Collection classes.
http://msdn.microsoft.com/en-us/library/ms172181(v=vs.80).aspx[^]
http://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.80).aspx[^]
 
Define any of the collection class in your application to deal with the strings when the application is running.
 
to restore the strings for later use you must save the file before closing the application and read the file while loading your application. check this for working on .txt files.
http://www.csharp-examples.net/filestream-open-file/[^]
  Permalink  
Comments
Filipe Luna at 13-Dec-12 19:34pm
   
I'll have to do some more reading! Thank you for sharing this with me :)
jibesh at 13-Dec-12 19:36pm
   
Yes.. thats how you have to start... more reading.. more knowledge :) good luck
Filipe Luna at 13-Dec-12 19:46pm
   
Thanks man.. I'm just very new to this and quite ambitious :p

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Afzaal Ahmad Zeeshan 259
1 Maciej Los 240
2 BillWoodruff 240
3 OriginalGriff 220
4 Sergey Alexandrovich Kryukov 195
0 OriginalGriff 6,419
1 Sergey Alexandrovich Kryukov 6,008
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,070


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 13 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100