Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Consider this code:
int total = 0;
using(var inFile = new StreamReader("text.txt"))
{
string inValue = "";
while ((inValue = inFile.ReadLine()) != null)
{
if(Int32.TryParse(inValue, out number))
{
total += number;
Console.WriteLine("{0}", number);
}
else
Console.WriteLine("{0} - not a number", inValue);
}
}
Console.WriteLine("The sum is {0}", total);
If I do MessageBox.Show("{0}", number);, it gives me an error. Why is this and how can I fix it?
The MSDN states pretty clearly, that MessageBox.Show does take two strings. But those two are not format and parameter, but text and caption. If you want to format your text, use string.Format to format a string before calling the method. You may also use one of the other overloads, but whichever you use, you need to do your own formatting.
Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
This means you need this namespace and you need the dll in your references. Both does not happen by default in a Console Application.
It definitely looks like you are writing a console application and then Messagebox is out of the question. But if you are in fact writing a Windows Forms application, here is the answer:
Messagebox doesn't have a "built-in" formatter, like Console.WriteLine. If you want to format the string you must use String.Format:
MessageBox.Show(String.Format("{0}", number));
Alternatively:
MessageBox.Show(number.ToString());
MessageBox will not work in Console Applications.
The problem is with the line
MessageBox.Show("{0}", number);
You can not call MessageBox.show() in the way you call Console.writeline(). The second parameter in MessageBox.show is MessageBox title. Try writing
MessageBox.Show(number)
or
MessageBox.Show("The sum is "+number);
Related
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;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
in c# i need to compare 2 numbers one from a local file, and one another from a downloaded file like a Patcher.
if I use Streamreader c# sad to me that he can't convert string into INT.
are there a solution for this?
file a contains the value "1" , the file b contains the value "2"
so if b>a then download the new files catch from another updater file.
thanks
If that is the only number in the file, you can use File.ReadAllText (or File.ReadAllLines in a multiline file) and convert to int like this:
string[] lines = File.ReadAllLines(#"c:\t.txt");
int number = Convert.ToInt32(lines[0]);
try to use the Convert.ToInt32 method.
If your file contains olny one number, you could use the File.ReadAllLine method, insted of streamreader.
void CompareVersions()
{
WebClient client = new WebClient();
var serverVersion = client.DownloadString("http://yourwebsite.com/version.txt");
using (StreamReader sr = new StreamReader("file.txt"))
{
if (Convert.ToInt32(serverVersion) > Convert.ToInt32(sr.ReadLine()))
{
// server version bigger
}
else
{
// up to date
}
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
Currently, I have something like the following:
public char functName(int n)
{
some functionality....
if(condition1)
return convertFuncToChar(variable) + functName(modifiedNumber);
else
return convertFuncToChar(variable);
}
however, I realize that doesn't give me a string (and the syntax shows that there's an error).
I know that for c++, I would most likely use char* to initialize the function, but this is C#.
I don't think it works if I initialize with String either.
Assuming I understand the question correctly, you can take advantage of two things:
1) all basic types implement .ToString(), which creates a string for you
2) string implements operator+()
The following is an example of a recursive function that will recursively concatenate characters to create a string. The output is the number in reverse as a string.
static string funcName(int n)
{
if (n<10)
return (n%10).ToString();
return (n%10).ToString() + funcName(n/10);
}
Of course, it would be more efficient to write this non-recursively.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I print different length of spaces in C#?
Write(number*" ");
doesn't work here.
EDIT: But it works in Python.
Indeed, there's no * operator taking a string and an integer.
The simplest option is probably to use the string(char, int) constructor:
Write(new string(' ', number));
Depending on your actual use case, you might want to look at string.PadLeft and string.PadRight too, if you're actually trying to pad an existing string.
You can do it like that:
Write(new String(' ', number)); // <- ' ' character, number times
That's, probably the simplest way. You may also want to look String.Format esp. if you try to print out a padded data. For instance:
// Prints out myData padded left up to 10 characters
Write(String.Format("{0,10}", myData));
Try with this snippet:
int index = 0;
while(index<number)
{
System.Console.Write(" ");
index++;
}
You can encapsulate this in a wrapper function, and call it as
printblank(number);
using:
void printblank(int number)
{
int index = 0;
while(index<number)
{
System.Console.Write(" ");
index++;
}
}
Thereby you can use this function repeatedly, as per your requirement.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am currently trying to get a label(lets name it lblMessage) to pull information based on what the user has picked threw radio buttons and check boxes. Lets make the theme a sundae maker form.. people can select there flavor, size, and addons.
I'm just trying to make it so the label displays once they click the confirm button, a message that identifies the number of sundaes, sundae flavor and size the person ordered. like.. (You ordered 2 small hot fudge sundaes which will cost you
$x.xx (where x.xx is the cost for all sundaes ordered). I tried looking this up but i couldn't seem to find what to put into the label code... =\
I am a beginner at C# ... If any other information is needed, I can provide.
What you want to do is set the text of the label. You can use the String.Format method to take a string and replace certain values with values from variables, for example:
var result = MessageBox.Show("Are you sure?", "Are you sure?", MessageBoxButtons.YesNo);
if (result == System.Windows.Forms.DialogResult.Yes)
{
Decimal total = int.Parse(textBox1.Text) * 1.25m;
// The {0} will be replaced with the first argument after the format string.
// The total.ToString("C") tells the decimal to format the string into a currency string (http://msdn.microsoft.com/en-us/library/fzeeb5cd(v=vs.110).aspx)
label1.Text = String.Format("You ordered {0} small hot fudge sundaes, which will cost you {1}", textBox1.Text, total.ToString("C"));
}
Now this example still fails if the textbox does not contain an int, but that can easily be handled using the TryParse method.