When not entering any value, my program crashes [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a problem in my windows form in c#. The program is simple;
there are 3 textboxes and then it sums their values. however, when i click on the sum button without entering any values in the other 3 textboxes, the program crashes.
How can i make these textboxes accept only positive numbers and zeros?
this is what i did
private void button1_Click(object sender, EventArgs e)
{
double FirstNumb = Convert.ToDouble(txtFirstValue.Text);
double SecondNumb = Convert.ToDouble(txtSecondValue.Text);
double ThirdNumb = Convert.ToDouble(txtThirdValue.Text);
double m;
m = FirstNumb + SecondNumb + ThirdNumb;
listBox1.Items.Add(m);
}

Try to resolve using int.TryParse. This handles string as well.
Demo Reference

You are likely trying to convert the textboxes textual content to int. Unfortunately, you cannot convert an empty string to a number. Try setting the textboxes default content to "0".

You'll want to check that the string value from the text box is not null or empty, or otherwise invalid. Change this line:
double FirstNumb = Convert.ToDouble(txtFirstValue.Text);
to this
double FirstNumb = 0;
double.TryParse(txtFirstValue.Text, out FirstNumb);
FirstNumb will remain 0 if the parsing fails. Note that TryParse returns a bool, true if the parsing was successful and false otherwise. You can also take action on that as well, perhaps showing a MessageBox asking the user to fill a value and exiting the summation method.
See the documentation for more information: http://msdn.microsoft.com/en-us/library/994c0zb1(v=vs.100).aspx
EDIT: If you want to enforce only positive values, after the try parse you'll have to check that FirstNumb >= 0 and use a MessageBox to alert the user why, and abort the summation method.

Related

How to round up the number in c# [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How can I round up values like this:
1.001 => 2
3.3 => 4
Means if the number has fractional part than i want the smallest integer number greater than the number ?
I used Math.Ceiling() but is not helping. How can i do this ?
Math.Ceiling will work. can you tell what its not working ? in term of any errors or returned result.
var returnVal=Math.Ceiling(yourValue);
Use Math.Ceiling() method.
Returns the smallest integer greater than or equal to the specified
number.
var i = Math.Ceiling(1.001);
var j = Math.Ceiling(3.3);
Console.WriteLine(i);
Console.WriteLine(j);
Output:
2
4
Math.Ceiling(value);
Should work.
double x;
x = Math.Ceiling(5.2) ;//Result; 6
x = Math.Ceiling(5.7) ;//Result; 6
x = Math.Ceiling(-5.2) ;//Result;-5
x = Math.Ceiling(-5.7) ;//Result;-5
This is a simple example. How can't you use it? Maybe you miss to assign a variable to
Math.Ceiling();

Convert double array to list [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
txt file that contains several columns of numbers, every column is saved in a double array, what I want to do is get the average of a specific column, but to do that I have to convert the array to a list and then began to calculate. I have this code so far:
List<double> 1 = new List<double>(NumSepaERG);
List<double> 12 = NumSepaERG.ToList();
But i get and error of Invalid Expression term double
Variable names can't start with a numeric character. Change to something like:
List<double> list1 = new List<double>(NumSepaERG);
but you can compute an average using Linq without converting to a list:
double average = NumSepaERG.Average();
If NumSepaERG is a jagged array (an array of arrays), the syntax would be:
double average = NumSepaERG[i].Average();
where i is between 0 and the number of arrays - 1;
I think it's a syntax error, you can't have numbers as variable names. You actually don't even need to make it a list.
double average = NumSepERG.Average();

TextBox that remembers it's value [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a problem. I am making an app on Windows 8 which has a textbox. I want the textbox to remember it's value when I close the application.
There are many possible solutions on this,
One is by saving the value in the database or
by saving it on Settings.
You have several options:
Properties.Settings (and on form closing use Save)
Windows registry
Use storage file
But you'll have to do a little work by yourself.
For possible solutions, see other answers. Here I give you pseudo-code for storage file.
Create a callback that is called whenever application launches and one for then it closes.
When it launches, load your file, get all of it's text including \n in one string, and set that as text.
When it closes, get the text in the box including \n and write it to the file.
It is quite simple. I would prefer the storage file method here because:
Properties.Settings is slightly overkill, though it's a viable alternative.
Windows Registry will work but it won't be cross platform (you may not care about that but it's always a good idea to make sure your app can be ported, in case it's successful)
You can use the approach to save a value to a File.
Here is a very ugly exa,ple to get you started:
using System;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Double val;
val = LoadDoubleValue();
Console.WriteLine("Previous Value was: " + val);
Console.Write("Please enter Value for next startup: ");
string input = Console.ReadLine();
if (Double.TryParse(input, out val))
{
SaveDoubleValue(val);
}
}
static Double LoadDoubleValue()
{
Double ret = 0;
if(File.Exists("save.dat")){
if (!Double.TryParse(File.ReadAllLines("save.dat")[0], out ret))
{
ret = 15;
}
}else{
ret = 15;
}
return ret;
}
static void SaveDoubleValue(Double val)
{
File.WriteAllLines("save.dat", new string[] { val.ToString() });
}
}
}

MasterMind in C# Console Application [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have an assignment from my school in which we have to make a MasterMind game in C# in a console application.
So far I managed to do the border (with help from friends), an introductory tune (beep) when the game starts, and the user input to insert the numbers.
The problem is when the user ends the game, the game doesn't stop to accept inputs from the user and obviously crashes.
I also have an error in the high score method "use of unassigned local variable".
score = ptsguesses * ptsTime;
Where are ptsguesses and ptsTime initialized? Nowhere, obviously.
You probably want to set ptsguesses and ptsTime before calculating the score.
the use of unassigned value is probably this one:
static void highscore()
{
{
byte ptsguesses,ptsTime, userGuesses, timeTaken;
int score; <<------
change it to int score = 0;
also, ptsguesses,ptstime,userguesses and timetaken have never been initialised.
you might want to try to pass those arguments to your highscore() method.
something like
static void highscore(byte ptsguesses, byte ptsTime, byte userGuesses, byte timeTaken)
you'd have to call the highscore() method then, and pass the actual values to the method. that way if u try to run highscore(), you will have something to actually calculate.

What data types do the RangeValidator control support? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
explain the rangevalidator control,is it take string also? explain thank you
It supports
Currency
Date
Double
Integer
String
For comparing strings better to use Regularexpressionvalidator instead of Rangevalidator.
In case of type string for the range validator it will only check the character by character, not the length of the string.
Yes, it does support string range validation.
Please look at the documentation before asking question.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.rangevalidator.aspx
MSDN:
The RangeValidator control allows you to check whether a user's entry is between a specified upper and a specified lower boundary. You can check ranges within pairs of numbers, alphabetic characters, and dates. Boundaries are expressed as constants.

Categories

Resources