C# windows form project Divide variable error - c#

Greeting everyone, I'm trying to do a windows form for collage were I can get the average of student scores. Seems like I have an issue with the divide function. here is my code , to see if you guys spot something wrong that could help me. I Appreciate in advance
private void getaveragebutton_Click(object sender, EventArgs e)
{
if (customTextboxes4.Texts == "" || customTextboxes5.Texts == "" || customTextboxes6.Texts == "" || customTextboxes7.Texts == "" || customTextboxes8.Texts == "" ||
customTextboxes9.Texts == "" || customTextboxes10.Texts == "" || customTextboxes11.Texts == "" || customTextboxes12.Texts == "" || customTextboxes13.Texts == ""
|| customTextboxes15.Texts == "" || customTextboxes16.Texts == "" || customTextboxes17.Texts == "" || customTextboxes18.Texts == "" || customTextboxes19.Texts == "" ||
customTextboxes20.Texts == "" || customTextboxes21.Texts == "" || customTextboxes22.Texts == "" || customTextboxes23.Texts == "" || customTextboxes24.Texts == "")
{
MessageBox.Show("Fill all the Boxes with Digits");
}
else
{
label4.Visible = true;
label5.Visible = true;
label7.Visible = true;
label61.Visible = true;
label28.Visible = true;
label29.Visible = true;
label30.Visible = true;
label31.Visible = true;
label45.Visible = true;
label46.Visible = true;
//short--test calculations
//students short test score variables
decimal a, b, c, d, e1, f, g, h, i, j;
//students short test value variables
decimal k, l, m, n, o, p, q, r, s, t;
//st results variables
decimal u, v;
decimal r1st;
decimal r2st;
decimal results;
//customboxes st score
a = Decimal.Parse(customTextboxes4.Texts);
b = Decimal.Parse(customTextboxes5.Texts);
c = Decimal.Parse(customTextboxes6.Texts);
d = Decimal.Parse(customTextboxes7.Texts);
e1 = Decimal.Parse(customTextboxes8.Texts);
f = Decimal.Parse(customTextboxes9.Texts);
g = Decimal.Parse(customTextboxes10.Texts);
h = Decimal.Parse(customTextboxes11.Texts);
i = Decimal.Parse(customTextboxes12.Texts);
j = Decimal.Parse(customTextboxes13.Texts);
//custom boxes st values
k = Decimal.Parse(customTextboxes15.Texts);
l = Decimal.Parse(customTextboxes16.Texts);
m = Decimal.Parse(customTextboxes17.Texts);
n = Decimal.Parse(customTextboxes18.Texts);
o = Decimal.Parse(customTextboxes19.Texts);
p = Decimal.Parse(customTextboxes20.Texts);
q = Decimal.Parse(customTextboxes21.Texts);
r = Decimal.Parse(customTextboxes22.Texts);
s = Decimal.Parse(customTextboxes23.Texts);
t = Decimal.Parse(customTextboxes24.Texts);
r1st = a + b + c + d + e1 + f + g + h + i + j;
r2st = k + l + m + n + o + p + q + r + s + t;
//st results variables definitions
u = Decimal.Parse(label28.Text);
v = Decimal.Parse(label29.Text);
//st result
label28.Text = r1st.ToString();
label29.Text = r2st.ToString();
results = u / v;
label4.Text = results.ToString();
return;
}
[enter image description here](https://i.stack.imgur.com/xu8cp.png)
Im trying to get all the score inserted in the text boxes to sum them. then get the results to Student Score and Total (That part is working for me) . After that divide the Student Score / Total to get the results in a hidden label that will be visible after getting the results. (that is the part that Im having issues with)

Well sorry to everyone for not following the proper way to post to get the proper help and thanks for those who let me know what to do for a next time to get the proper help. Also , thank you for explaining me the variable thing that was empty I really appreciate. Here is how I did fix my issue:
private void getaveragebutton_Click(object sender, EventArgs e)
{
if (customTextboxes4.Texts == "" || customTextboxes5.Texts == "" || customTextboxes6.Texts == "" || customTextboxes7.Texts == "" || customTextboxes8.Texts == "" ||
customTextboxes9.Texts == "" || customTextboxes10.Texts == "" || customTextboxes11.Texts == "" || customTextboxes12.Texts == "" || customTextboxes13.Texts == ""
|| customTextboxes15.Texts == "" || customTextboxes16.Texts == "" || customTextboxes17.Texts == "" || customTextboxes18.Texts == "" || customTextboxes19.Texts == "" ||
customTextboxes20.Texts == "" || customTextboxes21.Texts == "" || customTextboxes22.Texts == "" || customTextboxes23.Texts == "" || customTextboxes24.Texts == "")
{
MessageBox.Show("Fill all the Boxes with Digits");
}
else
{
label4.Visible = true;
label5.Visible = true;
label7.Visible = true;
label61.Visible = true;
label28.Visible = true;
label29.Visible = true;
label30.Visible = true;
label31.Visible = true;
label45.Visible = true;
label46.Visible = true;
//short--test calculations
//students short test score variables
decimal studentscorebox1, studentscorebox2, studentscorebox3, studentscorebox4, studentscorebox5, studentscorebox6, studentscorebox7, studentscorebox8, studentscorebox9, studentscorebox10;
//students short test value variables
decimal studentshorttestvalue1, studentshorttestvalue2, studentshorttestvalue3, studentshorttestvalue4, studentshorttestvalue5, studentshorttestvalue6, studentshorttestvalue7, studentshorttestvalue8,
studentshorttestvalue9, studentshorttestvalue10;
//st results variables
decimal studentshortestoverallscore;
decimal shortestoverallvalue;
decimal shortestaverage;
//customboxes student short test scores
studentscorebox1 = Decimal.Parse(customTextboxes4.Texts);
studentscorebox2 = Decimal.Parse(customTextboxes5.Texts);
studentscorebox3 = Decimal.Parse(customTextboxes6.Texts);
studentscorebox4 = Decimal.Parse(customTextboxes7.Texts);
studentscorebox5 = Decimal.Parse(customTextboxes8.Texts);
studentscorebox6 = Decimal.Parse(customTextboxes9.Texts);
studentscorebox7 = Decimal.Parse(customTextboxes10.Texts);
studentscorebox8 = Decimal.Parse(customTextboxes11.Texts);
studentscorebox9 = Decimal.Parse(customTextboxes12.Texts);
studentscorebox10 = Decimal.Parse(customTextboxes13.Texts);
//student short test value
studentshorttestvalue1 = Decimal.Parse(customTextboxes15.Texts);
studentshorttestvalue2 = Decimal.Parse(customTextboxes16.Texts);
studentshorttestvalue3 = Decimal.Parse(customTextboxes17.Texts);
studentshorttestvalue4 = Decimal.Parse(customTextboxes18.Texts);
studentshorttestvalue5 = Decimal.Parse(customTextboxes19.Texts);
studentshorttestvalue6 = Decimal.Parse(customTextboxes20.Texts);
studentshorttestvalue7 = Decimal.Parse(customTextboxes21.Texts);
studentshorttestvalue8 = Decimal.Parse(customTextboxes22.Texts);
studentshorttestvalue9 = Decimal.Parse(customTextboxes23.Texts);
studentshorttestvalue10 = Decimal.Parse(customTextboxes24.Texts);
//Sumatory of Students scores and Sumatory of Test Value
studentshortestoverallscore = studentscorebox1 + studentscorebox2 + studentscorebox3 + studentscorebox4 + studentscorebox5 + studentscorebox6 + studentscorebox7 + studentscorebox8 + studentscorebox9 + studentscorebox10;
shortestoverallvalue = studentshorttestvalue1 + studentshorttestvalue2 + studentshorttestvalue3 + studentshorttestvalue4 + studentshorttestvalue5 + studentshorttestvalue6 + studentshorttestvalue7 +
studentshorttestvalue8 + studentshorttestvalue9 + studentshorttestvalue10;
//String Results to Labels
label28.Text = studentshortestoverallscore.ToString();
label29.Text = shortestoverallvalue.ToString();
//Get Average of Short Test
shortestaverage = studentshortestoverallscore / shortestoverallvalue * 100;
label4.Text = shortestaverage.ToString();
return;
}
}
I did a correction of the variable names to not get confused like I was advised in the first post , then I deleted some variables that were empty like someone here explained me (thanks). Finally I did a re order of how it should be calculated to get the average and got it working. thanks to all and if you see something that I can improve let me know. I really appreciate everyone and every single advise. Have a good day everyone.

You cannot convert label to decimal as long as label contains no value. You have to rearrange the lines as follows
//st result
label28.Text = r1st.ToString();
label29.Text = r2st.ToString();
u = Decimal.Parse(label28.Text);
v = Decimal.Parse(label29.Text);
results = u / v;
label4.Text = results.ToString();
return;

Related

Sum two forces with both signs

I'm trying to make a good algorithm for summing up pair of forces. The main problem here is that you can have force with alternative sign which means that force can be with + or - at any time.
For example:
F1 = ±100 kN, F2 = 200 kN --> maxForce = +100+200 = 300 kN, minForce = -100+200 = 100 kN.
I've already made an simple algorithm which combines all possibilities, but I ask for something better than that. As an output of my method I have:
public List<Force> SumForces(Force firstForce, Force secondForce)
{
Force maxForce = new Force();
Force minForce = new Force();
// All possible sumatuons
double sumCaseFirst = firstForce.ForceValue + secondForce.ForceValue;
double sumCaseSecond = firstForce.ForceValue - secondForce.ForceValue;
double sumCaseThird = -firstForce.ForceValue + secondForce.ForceValue;
double sumCaseFourth = -firstForce.ForceValue - secondForce.ForceValue;
// Calculating all posible sumations
if (firstForce.Sign == ForceSign.Alter && secondForce.Sign == ForceSign.Alter)
{
maxForce.ForceValue = sumCaseFirst;
minForce.ForceValue = sumCaseFourth;
}
else if (firstForce.Sign == ForceSign.Alter && secondForce.Sign == ForceSign.Plus)
{
maxForce.ForceValue = sumCaseFirst;
minForce.ForceValue = sumCaseThird;
}
else if (firstForce.Sign == ForceSign.Alter && secondForce.Sign == ForceSign.Minus)
{
maxForce.ForceValue = sumCaseSecond;
minForce.ForceValue = sumCaseFourth;
}
else if (firstForce.Sign == ForceSign.Plus && secondForce.Sign == ForceSign.Alter)
{
maxForce.ForceValue = sumCaseFirst;
minForce.ForceValue = sumCaseSecond;
}
else if (firstForce.Sign == ForceSign.Plus && secondForce.Sign == ForceSign.Plus)
{
maxForce.ForceValue = sumCaseFirst;
minForce.ForceValue = 0;
}
else if (firstForce.Sign == ForceSign.Plus && secondForce.Sign == ForceSign.Minus)
{
maxForce.ForceValue = sumCaseSecond;
minForce.ForceValue = 0;
}
else if (firstForce.Sign == ForceSign.Minus && secondForce.Sign == ForceSign.Alter)
{
maxForce.ForceValue = sumCaseThird;
minForce.ForceValue = sumCaseFourth;
}
else if (firstForce.Sign == ForceSign.Minus && secondForce.Sign == ForceSign.Plus)
{
maxForce.ForceValue = sumCaseThird;
minForce.ForceValue = 0;
}
else
{
maxForce.ForceValue = 0;
minForce.ForceValue = sumCaseFourth;
}
// Ensure that true maximum force value is at index 0
if (maxForce.ForceValue > minForce.ForceValue)
{
Sum.Add(maxForce);
Sum.Add(minForce);
}
else
{
Sum.Add(minForce);
Sum.Add(maxForce);
}
return Sum;
}
The maximum is always when adding the positive values
double maxValue = Math.Abs(firstForce.ForceValue) + Math.Abs(secondForce.ForceValue);
The minimum is always when adding the negative values
double minValue = -Math.Abs(firstForce.ForceValue) - Math.Abs(secondForce.ForceValue);
There is no need to consider the positive and negative combinations.

Why string type is not converting to int type?

I want to add two numbers. I am getting values from button in my textbox. I also succeeded in splitting string into substrings and store values of these substrings in variables. But i am not able to convert string type to integer type. That results in concatenation not in addtion.
Note :
I am using MVC to perform this task. And in model value1 and value2 is string type in model
Here is my code snippet:
if (button == "1"){
if (model.textBox == "" || model.textBox == null || model.textBox.ToLower().Contains("please enter value")){
model.textBox = "1";
} else {
model.textBox += "1";
}
}
if (button == "2") {
if (model.textBox == "" && model.textBox == null) {
model.textBox = "2";
} else {
model.textBox += "2";
}
if (button == "+") {
if (model.textBox == "" && model.textBox == null){
model.errormsg = "Please enter a number ";
} else {
model.textBox += "+";
}
if (button == "=") {
if (model.textBox.Contains("+")) {
model.value1 = (model.textBox.Split('+'))[0];
int value1 = int.Parse(model.value1);
model.value2 = (model.textBox.Split('+'))[1];
int value2 = int.Parse(model.value2);
model.textBox = model.value1 + model.value2;
}
return View(model);
If i got you correctly all you need to do is:
model.textBox = (value1 + value2).ToString();
model.textBox is of type string, so, when you do model.textBox += "1"; the only possible operation is concatenation.
To add them as integers you first need to convert your textBox to int.
Something like the following will work:
model.textBox = (int.Parse(model.textBox) + 1).ToString();
Try using int.Parse(textBox.Text).
It will crash if there is a non-numeric character in the string, but that's nothing a try-catch block won't fix.
Hope this helps!
if (button == "=") {
if (model.textBox.Contains("+")) {
model.value1 = (model.textBox.Split('+'))[0];
int value1 = int.Parse(model.value1);
model.value2 = (model.textBox.Split('+'))[1];
int value2 = int.Parse(model.value2);
model.textBox = model.value1 + model.value2;
}
Replace above code snippet with below code snippet:
if (button == "=")
{
if (model.textBox.Contains("+"))
{
model.value1 = (model.textBox.Split('+'))[0];
model.value2 = (model.textBox.Split('+'))[1];
model.textBox1 = (int.Parse(model.value1) + int.Parse(model.value2)).ToString();
}

Search between two dates not working

I have the following code:
var q = context.Fuels.AsQueryable();
if (dateEdit1.EditValue != null && dateEdit2.EditValue != null)
{
q.Where(d => d.FuelDate >= dateEdit1.DateTime && d.FuelDate <= dateEdit2.DateTime);
}
and it's working, it will get all rows in table.
but when I'm using this code:
if (dateEdit1.EditValue != null && dateEdit2.EditValue != null)
{
var r = context.ExecuteStoreQuery<Fuel>(String.Format("SELECT * FROM Fuel WHERE FuelDate BETWEEN '{0}' AND '{1}'",
dateEdit1.DateTime.Year + "-" + dateEdit1.DateTime.Month + "-" + dateEdit1.DateTime.Day,
dateEdit2.DateTime.Year + "-" + dateEdit2.DateTime.Month + "-" + dateEdit2.DateTime.Day));
}
it's working but I need to use the first way to get relations working.
Any idea what is the problem in the first one?
Inside of your if clause, you need to assign it to q, if you don't do that q is just a query for all Fuels
This should work:
q = q.Where(d => d.FuelDate >= dateEdit1.DateTime && d.FuelDate <= dateEdit2.DateTime);

C# behind Silverlight5 App TextBox enduser entry quirky

I am using the below code and when testing it, the textBoxes will allow number input, but you have to enter the complete number, then arrow back to enter the decimal. Otherwise the cursor jumps in front of the first number and will not allow the decimal to be entered. I would also like to allow the $ to be inputted first.
Any ideas how I can customize this textBox to do these functions allowing the calculations to function properly given below?
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
//Adding ToolTip
ToolTipService.SetToolTip(textBox1, "Enter Price, numbers first, then arrow back to add decimal.");
if (Double.TryParse(textBox1.Text, out value1))
{
textBox1.Text = value1.ToString();
}
textBox69.Text = value69.ToString();
if (textBox1.Text == null || textBox1.Text == "") value1 = 0;
{
textBox1.Text = value1.ToString();
if (textBox1.Text.EndsWith(".97") == true)
{
value20 = 0; value36 = 0; value69 = 0;
textBox20.Text = value20.ToString();
textBox36.Text = value36.ToString();
textBox69.Text = value69.ToString();
}
if (textBox1.Text.EndsWith("1") || textBox1.Text.EndsWith("2") || textBox1.Text.EndsWith("3") || textBox1.Text.EndsWith("4") || textBox1.Text.EndsWith("5") || textBox1.Text.EndsWith("6") || textBox1.Text.EndsWith("8") || textBox1.Text.EndsWith("9") || textBox1.Text.EndsWith("0") == true && (value1 < 100.00))
{
value69 = 1;
textBox69.Text = value69.ToString();
}
if (value1 > 100.01 && value1 < 149.99)
value69 = 2;
textBox69.Text = value69.ToString();
}
}

How to findout the server controls in webmethod using asp.net?

I have written my webmethod in aspx.cs file , but when i call n.Nautilus() method in same page , i am unable to get the server side controls in Nautilus(), in this method controls becoming NULL , please find the reasons and solution to this problem as soon as possible.
[WebMethod]
public static string Execute4()
{
NewQuote2 n = new NewQuote2();
JavaScriptSerializer j = new JavaScriptSerializer();
string r = string.Empty;
var o = Observable.Start(() =>
{
// Thread.Sleep(7000);
PennStar pn = new PennStar();
r = j.Serialize(new { res = n.Nautilus() });
}, Scheduler.NewThread);
o.First();
// r = n.Nautilus();
return r;
}
public string Nautilus()
{
try
{
if (ddlLineCode.SelectedItem.Value == "GL")
{
deductible = Convert.ToInt32(ddlGLdeductible.SelectedItem.Text);
//ClassCode = Convert.ToInt32(ddlClassCode1.SelectedValues.ToString());
ClassCode = Convert.ToInt32(ddlClasscode.SelectedValue);
}
else if (ddlLineCode.SelectedItem.Value == "PP" || ddlLineCode.SelectedItem.Value == "PR")
{
deductible = Convert.ToInt32(ddlPropdeductible.SelectedValue);
}
string T = ddlTerritory.SelectedItem.Text;
QMSRatingEngine.Nautilus QR = new QMSRatingEngine.Nautilus();
ArrayList Result = new ArrayList();
if (LC == "GL")
{
QMSRatingEngine.NautilusAgents.ReturnGLRate gl = QR.GLRateObject(state, ED, deductible, ClassCode, ddlLimit.SelectedItem.Text, T);
Result.Add(gl);
getNautilusRatedata(gl);//this method code i can write the bellow
}
if (LC == "PP" || LC == "PR")
{
QMSRatingEngine.NautilusAgents.ReturnPropRate PRop = QR.PropertyRateObject(state, ED, ddlPropFormtype.SelectedItem.Text, ddlPropconstructiontype.SelectedItem.Text, ddlPropcovergetype.SelectedItem.Text, deductible, ddlPropuwscale.SelectedItem.Text, ddlPropprotectionclass.SelectedItem.Text, T);
Result.Add(PRop);
}
}
return lbltext.Text;
}
private void getNautilusRatedata(QMSRatingEngine.NautilusAgents.ReturnGLRate gl)
{
lblNautilusPremiumbasis.Text = GetPremiumBasisFormat(txtExposure.Text);
lblNautilusDeductible.Text = getCurrencyFormat(ddlGLdeductible.SelectedItem.Text);
string Type = ddlPremiumBasis.SelectedItem.Text;
decimal Premium = Convert.ToDecimal(gl.BaseRate_Prem.ToString());
decimal nautilusPremiumBasis = Convert.ToDecimal(txtExposure.Text);
decimal Prod = Convert.ToDecimal(gl.BaseRate_Prod.ToString());
decimal CalcPremium = 0, CalcProd = 0;
if (Type == "A - AREA" || Type == "M - ADMISSION" || Type == "U - UNITS" || Type == "O - OTHER")
{
CalcPremium = (Premium * nautilusPremiumBasis);
lblNautilusPremisesOp.Text = getCurrencyFormat(Math.Round(CalcPremium).ToString());
CalcProd = (Prod * nautilusPremiumBasis);
lblNautilusProductsCoop.Text = getCurrencyFormat(Math.Round(CalcProd).ToString());
}
if (Type == "C - TOTAL COST" || Type == "P - PAYROLL" || Type == "S - GROSS SALES")
{
CalcPremium = (Premium * nautilusPremiumBasis) / 1000;
lblNautilusPremisesOp.Text = getCurrencyFormat(Math.Round(CalcPremium).ToString());
CalcProd = (Prod * nautilusPremiumBasis) / 1000;
lblNautilusProductsCoop.Text =getCurrencyFormat(Math.Round(CalcProd).ToString());
}
lblNautilusGLPremium.Text = getCurrencyFormat(Math.Round((Math.Round(CalcProd) + Math.Round(CalcPremium))).ToString());
lblNautilusSubTotal.Text = getCurrencyFormat(Math.Round((Math.Round(CalcProd) + Math.Round(CalcPremium))).ToString());
lblNautilusGrandTotal.Text = getCurrencyFormat(Math.Round((Math.Round(CalcProd) + Math.Round(CalcPremium))).ToString());
//string jstablelattest = "moneyCoverage";
//ClientScript.RegisterClientScriptResource(this.GetType(), jstablelattest);
}
Because you are calling Nautilus() function from public static string Execute4() and because Execute4() is a webmethod and thus static you are not able to find any controls as they are not static.
You should try to return the results and values from your webmethod and do the operations like show/hide divs and other stuff with those results on the client side.

Categories

Resources