Saving Program Data in C# [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
So I'm working on a quick program for my mom. It has a textbox user input, a combobox for category selection, and multiple textbox for representing each category. The idea is that she can input a number value into the textbox and select a category. Then that value is stored in its respective category's textbox. I am absolutely certain I did not write this code well, but I don't know C# very well. Long story short, I need a way to be able to save the data stored in the textbox categories so she can close the program. I've never worked with something like this before, even in other languages, so this is foreign to me.
EDIT:
Here is the code illustrating what I am currently doing (this code is under a button click action, private void button1_Click(object sender, EventArgs e):
string userInput;
userInput = textBox1.Text;
if(comboBox1.SelectedIndex == 0)
{
category1Box.AppendText(userInput +"\n");
}
I did not specify how I wanted to save the data , as I'm not even sure how I want to do it. Like I said, I'm not experienced in this area at all, so I don't even know what the different options I had were. Hopefully this edit helps.

Serialization is a fairly simple way to save and load data on a single machine. You mention a single user so I think this will fit your requirements, but without more information I can't be sure.

Your question is very ambiguous in terms of how you want to save data, so I'll go with a very simple solution. You can utilize a few methods in the File class in System.IO.
File.WriteAllBytes(string path, byte[] data)
File.WriteAllLines(string path, string[] data)
File.WriteAllText(string path, string data)
These methods are very useful, and I suggest you learn to use them!

Related

List.Reverse() without any effect in C# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Given the Following code:
List<Digit> tempDigits = input.Digits;
string test = tempDigits.ToString(); // -> {2,2,7}
tempDigits.Reverse();
string test2 = tempDigits.ToString(); // -> {2,2,7}
This is my code:
and that the result (in between, I renamed the variable according to naming convention as suggested by Thiessen)
For some reason, the reverse soes not seem to do its job.
It must be something really simple. But I cant get what the Issue is. I hope, someone can help out.
The code you've posted would work correctly, in a vacuum. (Except, in what world does List<>.ToString() produce "{2,2,7}"?)
My main guess would be, as Lesiak commented, that this.Digits and input.Digits points to the exact same List<> instance. So Digits1.Reverse() reverses the list and Digits2.Reverse() reverses the reversal, putting it back how it started. Perhaps this and input are the same thing. Or perhaps they are two different things that happen to use the same underlying Digits list. Who knows?
There are many other possibilities, some of which would be impossible to guess at based on the information provided. For example:
Maybe the input control that's giving you that list is trying to actively manage the list, and reorders it as soon as it notices it's been reversed.
Maybe you're not using the System.Collections.Generic.List<> type, but instead you're referencing some other namespace where someone has written a List<> type that doesn't behave how you'd think it would. Maybe it doesn't even implement Reverse(), so the compiler is picking up on the Enumerable.Reverse() extension method, which does nothing to mutate the underlying data.
It really is impossible to know for sure without knowing more about your environment.
The information you give is incomplete to address the issue.
A reproducible example from the information you provide does not show the same symptoms.
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<int> digits = new List<int> {2,2,7};
foreach(int digit in digits)
Console.Write(digit);
Console.WriteLine();
digits.Reverse();
foreach(int digit in digits)
Console.Write(digit);
Console.WriteLine();
}
}
Which gives the expected output
227
722
https://dotnetfiddle.net/Hs9H0k
My guess would be, this.Digits is not a mutable reference. But that is just an hypothesis.

Requesting code recommendations for building a multi-layered, conversation bot [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
My company wants an Azure bot to respond to a series of “yes” and “no” answers along a conversational path with three layers. I'm using Visual Studio Community 2019 to edit code and Bot Framework Emulator (V4) to test the bot.
Here's a link to an image of the questions and answers and how the conversational flow should look:
https://cdn1.imggmi.com/uploads/2019/5/21/738797c22a7756a01fdf6786f26eb39b-full.png.
I'm not sure know how to link multiple layers of dialogue together. I've only been able to construct a bot with one layer of dialogue as shown at the URL below:
https://cdn1.imggmi.com/uploads/2019/5/21/99019e8e881d62f56ce449397ca9f191-full.png
I tried using the "SuggestedActionsBot" template, but it seems to need a major rework to make our deliverable possible.
https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore/08.suggested-actions
Just for reference, here's an example of how the code for the first layer of dialogue looks:
private static string ProcessInput(string text)
{
const string FirstSequenceResponseYes = "Sure, can you explain the issue? " +
"Do you see error message such as 'Power BI Access Tokens Not Allocated?'";
const string FirstSequenceResponseNo = "Thanks, user. " +
"It's good to interact with you. Have a great day!";
switch (text)
{
case "yes":
{
return $"{FirstSequenceResponseYes}";
}
case "no":
{
return $"{FirstSequenceResponseNo}";
}
default:
{
return "Please select one of the suggested action choices.";
}
}
}
What is the best way to go about writing the code to make this deliverable possible? Are there URLs and resources someone could provide that are directly pertinent to what we're trying to achieve?
I'm not sure I understood your question exactly, as the comment say.
But for navigating layers in the bot, I would encourage you to read this documentation.
With the bot-community solution, you can create a complex dialog with several levels.
Finally, in order to create a dialog that receives only yes and no answers, I would suggest using ConfirmPrompt.

Generating folders using textboxes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hello experts, I have to generate series of folders from a TextBox into specified location.I am having two textboxes to specify the limit of folders(say 30 folders).The problem am facing is that the folder names that i will be providing are alpha-numeric(say 121cs3h101) .
How to set limit when i provide an alpha-numeric values?
(For example: i provide textbox1=12cs3h101 and textbox2=12cs3h131 , i need the series limit to be generated). I am working with visual studio 2013 in c# windows form application. Thanks in advance.
ok I will try to give you a lead.
To parse a string or find specific characters one can use RegEx.Match or a simler method called String.Split. In both cases you have to be aware how your string is structured and how it can vary. The limits of variation are very important.
If as you say the beginning is always"12cs3h" you can either split the string at the character 'h'.
string[] sa = s.Split('h');
Or you can even use the index of 'h' (since the length seems to be fixed) and take the rest of the string to get the numbers.
int index = s.IndexOf('h');
The rest is up to you, ... convert, enumerate and so on.
EDIT: There is a nice method that does the enumeration job for you: Enumerable.Range Good luck

String to decimal [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
The application that I've made I've got a text box called service cost. This allows the user to enter the cost of the service they have provided and this is decimal. I'm trying to get this service cost displayed in a DGV, I've got everything else working apart from this.
currentComputer.ServiceCost = Convert.ToDecimal(txtServiceCost);
The above is the code that I currently have, is there something wrong that I've done here?
From the question it is clear that txtServiceCost is the TextBox, and Convert.ToDecimal() expects a string as input so you should use txtServiceCost.Text instead for txtServiceCost. Since txtServiceCost is a control where as txtServiceCost.Text is a string
currentComputer.ServiceCost = Convert.ToDecimal(txtServiceCost.Text);
But i would like to suggest you to use decimal.TryParse
decimal userInput;
if (!decimal.TryParse(txtServiceCost.Text, out userInput))
{
// Throw some warning here that invalid input
}
currentComputer.ServiceCost = userInput;

C# Checking if text-based answer is correct [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Good day all. Say I have a question:
What is a reverse reaction?
And the answer to this question is:
A reverse reaction is a reaction in which the products react to form
reactants and vise versa.
Now what would be the best way to determine if a user-inputted answer this question is correct or not? I can think of a couple of ways but they aren't practical.
One of the ways:
string answer = "A reverse reaction is a reaction in which the products react to form reactants and vise versa.";
string input = Console.ReadLine();
if (input.Equals(answer))
{
//answer is correct
}
Other way:
Checking to see how many words match and getting a percentage from that. If it calculates to a certain percentage then the answer is right.
Number of words: 17
Number of words in input that match answer: 17
Correctness percentage: 100%
Another way:
Check to see if the input contains certain key phrases.
string input = Console.ReadLine();
string[] keyPhrases = new string[] { "Products react to form reactants" };
foreach (string keyPhrase in keyPhrases)
{
if (!input.Contains(keyPhrase))
{
//answer is incorrect
return;
}
}
If what you mean by correctness is semantically correct, and the user is free to put up his answer, then I believe there is no simple way at this moment to do that by programming at all.
If you do it with the first way:
string answer = "A reverse reaction is a reaction in which the products react to form reactants and vise versa.";
string input = Console.ReadLine();
if (input.Equals(answer))
{
//answer is correct
}
And the user forgot to put the last little dot ".",
"A reverse reaction is a reaction in which the products react to form reactants and vise versa"
then he will get wrong, but he is actually correct
If you do it the second or the third way, then if the user simply mentions its negation, he may have high percentage of match but totally wrong in his concept:
"A reverse reaction is NOT a reaction in which the products react to form reactants and vise versa"
As of now, I believe the best way to do this is by restricting the user inputs to multiple choices provided by you.
And one of the best items to do this is the radio buttons. But you could do this by combo box and button or ListBox which allows single/multiple choices as you want it, but the bottom line is the same:
restrict your user inputs or you cannot tell whether his answer is semantically right/wrong easily.
It may require expertise in grammatical understanding, lots of dictionary words, complex words-meanings relationship models, and excellent background contexts interpretations otherwise.
That being said,
Regex cannot help to check if an answer is semantically correct - it can only help you to find a pattern which you may use to check if the user puts a semantically correct answer.
Thus...
If it is used together with human inspection, then probably your second and third ways + Regex would give some benefits.

Categories

Resources