|
I would have approached this issue 'a bit differently' - it depends if you want to have to whole file in memory, or what you wish to optimise ...
Assuming you dont want the whole file in memory, you read the file multiple times
In the OnLoad event for the WinForm, I would fire off a Background thread, to parse the file to extract all the questions, their start lines in the file, the number of answers, to a data structure - possible an array of struct(string question, int start line, int num answers) - this is your 'index'
once you have a random number, you get that element from the index and use the start line and num answers to locate the answers in the file
If you can keep the entire set of data in memory, then you parse the file to something like array of struct(string question, List<string> answers), which means once you have your random number, you can jump straight to the index and get the question and answers
|
|
|
|
|
I would suggest that the best thing to do would be to change your file format.
If you used each line to hold a single question, together with it's possible answers, separated by '|' characters:
Planete i njihovi sateliti|Zemlja=Mesec|Mars=Fobos|Jupiter=Io|Saturn=Titan|Uran=Titania|Neptun=Triton|Pluton=Haron|Merkur=Nema satelit
Čuveni parovi iz umetnosti|Hamlet=Ofelija|Ruslan=Ljudmila|Zevs=Hera|Otelo=Dezdemona|Paris=Helena|Abelard=Eloiza|Paolo=Frančeska|Lanselot=Ginevra
...
Then the problem becomes trivial:
Use File.ReadAllLines to read the whole file into an array of strings: each element of the array is a question together with it's answers, so selecting a random question is a trivial use of the Random class.
Each question and answer can be easily split out into an array of strings using string.Split - the first element of the array is the question, the rest of them are possible answers.
But do yourself a favour: stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
How to change format?
My text file has 15000 lines!!!!
|
|
|
|
|
Simple!
Write a quick app to do it for you. Something like this should do the donkey work:
private void ConvertFile(string inPath, string outPath)
{
string[] inData = File.ReadAllLines(inPath);
List<string> outData = new List<string>();
StringBuilder sb = new StringBuilder();
foreach (string line in inData)
{
if (line.StartsWith("*"))
{
if (sb.Length > 0)
{
outData.Add(sb.ToString());
sb.Clear();
}
sb.Append(line.Substring(1));
}
else
{
sb.Append("|");
sb.Append(line);
}
}
if (sb.Length > 0)
{
outData.Add(sb.ToString());
}
File.WriteAllLines(outPath, outData);
}
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I have added questions and answer to list!How to get random question with it's answers from the list?
string[] data = File.ReadAllLines("spojnice1.txt");
List<string> questions = new List<string>();
foreach (string line in data)
questions.Add(line);
|
|
|
|
|
You know the number of questions, so you should be easily able to use the Random class to pick the appropriate question. And I'm pretty sure that Griff recommended that you used a parent-child relationship so you added only the lines starting with * to the questions list, and then you would expose a list of answers as a child of the question.
This space for rent
|
|
|
|
|
He told me to change to this format text file:
Planete i njihovi sateliti|Zemlja=Mesec|Mars=Fobos|Jupiter=Io|Saturn=Titan|Uran=Titania|Neptun=Triton|Pluton=Haron|Merkur=Nema satelit
Čuveni parovi iz umetnosti|Hamlet=Ofelija|Ruslan=Ljudmila|Zevs=Hera|Otelo=Dezdemona|Paris=Helena|Abelard=Eloiza|Paolo=Frančeska|Lanselot=Ginevra
...
When I change to that format I cannot add lines starting with * to list!!!
|
|
|
|
|
Sigh. Let me simplify this for you.
Griff has suggested that you break your original list down (the ones that originally started with * being your question). These then have a number of answers stored as a series of pipe delimited fields. That's your parent/child relationship there.
The lines originally marked with * will now be the first entry in this pipe delimited list.
You know how many entries you have in this list, so you use this as the upper bound of the Random number.
That's it - the code is trivial. To make things more random, don't forget to seed the Random when you instantiate it. Read the documentation for details on how to do this.
This space for rent
|
|
|
|
|
Hi,
if the majority of your application needs to know the file structure, then something is definitely wrong.
At the minimum you should define data structures that reflect the problem domain, read your file once, and stuff all the info into the data structures, then work from there.
However, I would also consider using a database; from what I understood you would need two tables: one for questions, one for the remaining info; and an N-to-1 relationship between them.
|
|
|
|
|
Hi all,
I am using Selenium ver 2.53.0, Firefox 46.0.1. I want to create an application that use Selenium WebDriver to do parallel tasks. I have created an app like that but: My app opened 2 webdrivers and its task was the same, not different task as I have assigned to each thread before. I wonder what was going on? Was there one-one connection between thread 1 - Webdriver 1 AND thread 2 - Webdriver 2 OR not? If not I suggest each thread was pushing its data to active Webdriver at that time. Was I right?
Hope to get any help from all of you, so I can solve my issue. Thank you!
Best Regards,
Sokhanh
|
|
|
|
|
You don't provide the correct information to diagnose your problem.
But I will say that WebDriver is not thread-safe. You can't have multiple threads using the same WebDriver instance.
You CAN, however, have each thread create it's own instance of WebDriver, but each one of those is going to create it's own instance of the browser. This is going to greatly increase the amount of memory your app is using.
|
|
|
|
|
So sorry if my question was not clear enough or did not have right information to diagnose my problem. But what I was trying to say is I want to create, for example, 2 threads and WebDriver instance of each one. Then, pass parameter (different task for each thread) to each thread. However, the two threads did not do its tasks, I did the same task of thread 2. I don't know what was wrong? I can describe the app I created as follow:
Main thread create 2 child threads: thread 1, thread 2
thread 1 -> create Webdriver 1 --> do task 1
thread 2 -> create Webdriver 2 --> do task 2
The problem is thread 1 and thread 2 --> do the same task 2
for (int n = 0; n < 2; n++)
{
Thread myNewThread = new Thread(() => createNewThread(taskN));
myNewThread.Name = "[Thread " + n + "]";
myNewThread.Start();
Console.WriteLine("Thread {0} started\n\n", n);
}
public static void createNewThread(task)
{
FirefoxProfile prof = new FirefoxProfile();
prof.SetPreference("browser.startup.homepage_override.mstone", "ignore");
prof.SetPreference("startup.homepage_welcome_url", "about:blank");
prof.SetPreference("startup.homepage_welcome_url.additional", "about:blank");
prof.SetPreference("browser.startup.homepage", "about:blank");
IWebDriver driver = new FirefoxDriver(prof);
doTask(driver, task);
Console.WriteLine("Thread {0} completed!\n\n", Thread.CurrentThread.Name);
}
|
|
|
|
|
I installed Visual Studio 2017 a few days ago and for now I have 3 problems.
1.- After starting and stopping the project several times, when compiling appears the "Access Denied" indicating that do not have permissions in bin\Debug to copy the .exe file from obj\debug to bin\debug. The only thing that works is to go back to restart the pc. The .exe file not is in memory and unlocker does not indicate anything.
2.- I deleted the Debug directory but when compiling or recompiling the bin\Debug directory is not regenerated and error CS0246 appears because it does not find the references to the Dlls that i use in the project. Simply bin\debug remains empty.
SOLVED. Last minute: Solved. In the .csproj file there were several lines
modified 26-Mar-17 6:34am.
|
|
|
|
|
Why not report it to Microsoft?
|
|
|
|
|
This has nothing to do with C#, and is really an issue for Microsoft.
|
|
|
|
|
Hi I am a beginner programmer and I want to developers and application where I register members and add members to a specific group example sport group,study group or prayer group etc
Please can you assist or tell me what tutorial I could read to learn this.
|
|
|
|
|
Member 12997782 wrote: what tutorial I could read to learn this The first thing you need to do is decide what language you plan to write in. Since this is the C# forum, I assume you are going to use C#. And since you want to store information you probably want to use a database. So Start with C# and when you feel comfortable using that go and learn about databases (SQL in particular). .NET Book Zero by Charles Petzold[^] is a great starter on C#, and then you can go and look at some of the articles here on CodeProject. For database there is a good tutorial at Lesson 01: Introduction to ADO.NET – C# Station[^], and again lots of further articles on CodeProject.
|
|
|
|
|
Need to know step by step in construction of this Download manager using c# MyDownloader: A Multi-thread C# Segmented Download Manager[^]
i want to learn all the concepts required for it anyone who knows or have worked with the same project ?
please guide me to what to do i want to understand this project and modify it to make it more powerful
|
|
|
|
|
Read the article. Download the code and read it. Identify those areas that you think you can improve on and start from there.
This space for rent
|
|
|
|
|
Member 13063143 wrote: please guide me to what to do With all due respect, you're not going to get very far as a software engineer if you expect people to stop what they're doing and spoonfeed you. This is a field where you learn by taking the trouble to read, write and debug code. It starts with YOU. If you have specific questions, feel free to ask and we'll do our best to answer them. But please don't for a moment think that this is a field where you can sit back and have someone explain software engineering to you.
/ravi
|
|
|
|
|
No offense taken Mr. Ravi but i am asking for tutorial or steps on how to construct the app, the thing is am too new for both the language as well as V.S. need just the direction and for problems if any errors occurs i do it by myself and do not ask others unless i have at least tried it a few more times than my satisfaction level. there isnt any documentation on making or working of it as far as i have seen if you found any please share it w/ me
|
|
|
|
|
It's an article. The article should explain how it works. If there are bits in there you are unclear about, post a question on the forum at the end of the article. If you are new to .NET, pick a simpler project to start.
This space for rent
|
|
|
|
|
Hello can someone help to create an addins to read and edit a yaml file on visual studio 2015
Thanks a lot
|
|
|
|
|
Sorry, you might want to try rentacoder. We don't work that way here.
This space for rent
|
|
|
|
|