Only invoke method if condition is 30 or over - c#

When condition v is higher than 30 it keep sending SMS in app.
However, I want it to only send 1 SMS, then resend SMS when v goes back to 29, then 30.
if(v >= 30)
{
do
{
var SmsMessenger = (CrossMessaging.Current.SmsMessenger);
if(SmsMessenger.CanSendSmsInBackground)
{
SmsMessenger.SendSmsInBackground("+000000", "Test")
}
}
while(((currentLocation.Speed * 3600) / 1000) != 20);
}

You can use a flag, to call the message once.
bool IsSmsSended = false;
if(v >= 30 && !IsSmsSended) {
do {
var SmsMessenger= (CrossMessaging.Current.SmsMessenger);
if(SmsMessenger.CanSendSmsInBackground){
SmsMessenger.SendSmsInBackground("+000000","Test")
}
}
while(((currentLocation.Speed * 3600) /1000) !=20);
IsSmsSended = true;
}else if(v < 30){
IsSmsSended = false;
}
You have to reset IsSmsSended, if the value is under 30. Look at the else if.
P.S.: Make IsSmsSended global.

Related

C# WFA - increase value until limit then decrease loop

I want to do a program in C# - WFA, in which while a button is pressed - the label 1 value is increased by 10 since it reaches 100, then in decreases to 0 and again is increased til 100
like: 0 10 20 30...80 90 100 90 80...20 10 0 10...
i tried this:
private static int i = 0;
protected void button1_MouseClick(object sender, MouseEventArgs e)
{
if (i < 100)
i = i + 10;
else
i = i - 10;
this.label1.Text = i.ToString();
}
but it just decrease from 100 to 90 and the increases back to 100 and goes like this
You need to save two state information about the current value outside the button1_MouseClick() method. One is the value i, which you already do. The other is the information if you are going up or down. You can save this in a simple bool value like:
public static bool goingUp=true;
Then you can use these two fields i and goingUp and decide what you want to do:
if (goingUp)
{
i += 10;
if (i >= 100)
{
goingUp = false;
}
}
else
{
i -= 10;
if (i <= 0)
{
goingUp = true;
}
}
this.label1.Text=i.ToString();
an easy way would be this one
_indexValue = (_indexValue + 1) % 100;
and when it hits 100 then it restart to 0

Issue with a do while loop

The program below takes the customer age and verify if the customer is eligible to see a certain movie. I am having an issue with CustomerAgeCheck() method. Every time I enter age above 100 or below 0, the loop continues to run infinitely and no result is shown on the label.
protected void okButton_Click(object sender, EventArgs e)
{
AgeVerification();
CostOfTickets();
}
protected int CustomerAgeCheck()
{
int age = int.Parse(Cust1AgeTextBox.Text);
do
{
ageVerificationLabel.Text = String.Format("Please enter the correct age");
} while (age < 0 || age > 100);
return age;
}
protected void AgeVerification()
{
int age = CustomerAgeCheck();
if (Movie3RadioButton.Checked && age < 17)
{
ageVerificationLabel.Text = String.Format("Access denied - you are too young");
}
else if (Movie4RadioButton.Checked || Movie5RadioButton.Checked || Movies6RadioButton.Checked && age < 13)
{
ageVerificationLabel.Text = String.Format("Access deniad - you are too young");
}
else
{
ageVerificationLabel.Text = String.Format("Enjoy your Movie");
}
}
protected void CostOfTickets()
{
int cost;
int totalTickets = int.Parse(CustomerDropDownList.SelectedValue);
cost = totalTickets * 10;
resultLabel.Text = String.Format("Your Total is {0:C}", cost);
}
This is a very bad logic, especially since you're working with a GUI and you have events to help you.
All you have to do is to add a NumericUpDown control, set the min to 0 and max to 100 and then listen to the ValueChanged event.
In this event, you can simply use your above code:
age = (int)NumericUpDownAge.Value;
if (Movie3RadioButton.Checked && age < 17)
{
ageVerificationLabel.Text = String.Format("Access denied - you are too young");
}
else if (Movie4RadioButton.Checked || Movie5RadioButton.Checked || Movies6RadioButton.Checked && age < 13)
{
ageVerificationLabel.Text = String.Format("Access deniad - you are too young");
}
else
{
ageVerificationLabel.Text = String.Format("Enjoy your Movie");
}
You can also disable/enable controls based on the age entered in the NumericUpDown and make your GUI much more interactive and/or self-explanatory.
do
{
ageVerificationLabel.Text = String.Format("Please enter the correct age");
} while (age < 0 || age > 100);
The problem is very simple. It is with the above block of code. you don't have a break condition. When you enter a value above 100 or below 0 into age, its value doesn't changes in the execution of loop. Hence the while loop returns true always (Because the condition is whenever age > 100 and age < 0, repeat the loop) and continues to execute. You should edit it to meet the termination condition according to your need.
When you enter some other value after executing once, as the while loop condition fails it will terminate.

How to set Countdown For Each Candidate using Jquery

I am working on online Q/A system,i have to show countdown for each candidate like 3 min,on expiring user will be redirect to Result.aspx page.
I am facing following
1.how to set counter for each candidate.
2.on page refresh counter set to default value.
i have following code
<div id="timer">
</div>
<script type="text/javascript">
function countdown(minutes) {
var seconds = 60;
var mins = minutes;
if (getCookie("minutes") && getCookie("seconds")) {
var seconds = getCookie("seconds");
var mins = getCookie("minutes");
}
function tick() {
var counter = document.getElementById("timer");
setCookie("minutes", mins, 10)
setCookie("seconds", seconds, 10)
var current_minutes = mins - 1
seconds--;
counter.innerHTML =
current_minutes.toString() + ":" + (seconds < 10 ? "0" : "") + String(seconds);
//save the time in cookie
//minutesSpan.innerHTML = current_minutes.toString();
//secondsSpan.innerHTML = (seconds < 10 ? "0" : "") + String(seconds);
if (seconds > 0) {
setTimeout(tick, 1000);
}
else {
if (mins > 1) {
// countdown(mins-1); never reach “00″ issue solved:Contributed by Victor Streithorst
setTimeout(function () { countdown(mins - 1); }, 1000);
}
}
}
tick();
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
countdown(3);
</script>
because multiple user will doing these test so i have handle each one differently,i have following code to assign to assign test for each candidate
private void NewCandidate()
{
var ClistCount = (from cl in ef.Candidate_Table where cl.Name == btnname.Text && cl.Status_Id==2 select cl).FirstOrDefault();
if (ClistCount != null)
{
string cnic = ClistCount.Cnic;
Session["token"] = cnic;
Response.Redirect("MainTestForm.aspx?id=" + cnic);
}
else
{
MsgLitteral.ShowNotification("No Candidate Is Here", true);
btnbrowse.Visible = false;
btnname.Visible = false;
}
}
There are two things which you need to do in order to make things working
1) Create perfectly working count down timer method
2) Solve the reload dependency
for reload thing just before reload/refresh, trigger a function that would store the current time elapsed from the predefined count down.
$(window).bind('beforeunload', function(){
//below function stores current elapsed time in cookie/local storage
callFunction();
return true;
});
for e.g if 3min countdown is set and the user refreshes or moves to
next question at 2min 40 secs then store 2min 40 sec in cookies or
html5 local storage
On every document ready event check the cookie value
if value present then
take this value and set countdown
else
with predefined value (for the first time case)
A simple countdown timer for reference

Can't output to textblock, but messagebox works

I'm having a problem with outputting to textblock. Basicly what I do is this:
private void ReadData()
{
double dHeading = 0;
int iHeading = 0;
String sString = "";
while (!stop)
{
//Get Heading
result = fsuipc.FSUIPC_Read(0x0580, 4, ref token, ref dwResult);
result = fsuipc.FSUIPC_Process(ref dwResult);
result = fsuipc.FSUIPC_Get(ref token, ref dwResult);
dHeading = dwResult;
if (dHeading != 0)
{
dHeading = dHeading * 360 / (65536.0 * 65536.0);
iHeading = Convert.ToInt32(dHeading);
}
if (iHeading < 0)
{
iHeading = 360 + Convert.ToInt32(dHeading);
}
if (iHeading == 0)
{
iHeading = 360;
}
if (result == true && iHeading < 10)
{
sString = "00" + Convert.ToString(iHeading);
}
if (result == true && iHeading >= 10 && iHeading < 100)
{
sString = "0" + Convert.ToString(iHeading);
}
if (result == true && iHeading >= 100)
{
sString = Convert.ToString(iHeading);
}
txbHeading.Text = sString;
// But if I change this line to MessageBox.Show(sString);
// it works fine.
}
}
The program freezes and I can't do anything with it. I have to stop it in VS .
If I change the txbHeading.Text = sString to MessageBox.Show(sString), it works fine.
Please note that I just started with C#.
Thanks in advance!
The while loop in your code causes the UI thread to block, so the program should stop responding when the method is called. A background worker allows your code to be executed in a seperate thread without blocking the GUI.
try
this.Invoke(new Action(() => txbHeading.Text = sString))
instead. i assume you running outside the UI thread.
More on this: The Practical Guide to Multithreading - Part 1

Multiple if conditions check in single button click

My C# code is something like as follows.
if(TextBox1.Text.Length > 5)
{
if(TextBox2.Text.Length > 5)
{
if(TextBox3.Text.Length > 5)
{
if(TextBox4.Text.Length > 5)
{
//Action to pass to the next stage.
}
else
{
error4.text = "Textbox4 value should be minimum of 5 characters.";
}
}
else
{
error3.text = "Textbox3 value should be minimum of 5 characters.";
}
}
else
{
error2.text = "Textbox2 value should be minimum of 5 characters.";
}
}
else
{
error1.text = "Textbox1 value should be minimum of 5 characters.";
}
1) In the above kind of sample. I am using nested If-Else concept where on button click, if TextBox1 value is less than 5 is moves to else part and shows the error1 value but it will not check for further errors.
2) If I change If conditions to step by step If conditions then it will not work for me because the action must be done only if all the IF conditions satisfies.
3) If I use && operator to check all conditions I will not get individual error to each "error label"
How can I check multiple IF conditions on a single button click?
My original code
if (checkavail == "available")
{
if (name.Text.Length > 0)
{
if (email.Text.Length > 5)
{
if (password1.Text.Length > 7 && password1.Text == password2.Text)
{
if (alternate.Text.Contains("#") && alternate.Text.Contains("."))
{
if (question.Text.Length > 0)
{
if (answer.Text.Length > 0)
{
Response.Redirect("next_page.aspx");
}
else
{
error5.Text = "Please enter your security answer";
}
}
else
{
error4.Text = "Please enter your security question";
}
}
else
{
error3.Text = "Invaild alternate email address";
}
}
else
{
error2.Text = "Password should be minimum 8 characters and must match confirm password";
}
}
else
{
error1.Text = "Email address should be minimum 6 characters";
}
}
else
{
error.Text = "Please enter your name";
}
}
else
{
error1.Text = "This email address is already taken. Please try another";
}
I need the Redirect action to be done upon satisfying all conditions. If more than one error was found each error should get each error message.
Make a function that just handles the error messages. Return an Enumerable from this function. Then you can format your if-statements like this and it will return an Enumerable:
private IEnumerable GetErrors()
{
if (TextBox1.Text.Length > 5) { yield return "Textbox1 minimum bla bla"; }
if (TextBox2.Text.Length > 5) { yield return "Textbox2 minimum bla bla"; }
if (TextBox3.Text.Length > 5) { yield return "Textbox3 minimum bla bla"; }
}
Make another function that handles your non-error message logic and just perform an if-statement to see if there were zero errors or not:
public void DoSomething()
{
var errors = GetErrors();
if (errors.Count == 0)
Response.Redirect("next_page.aspx");
else
error.Text = "Please fix your errors";
}
Thanks to all. I found my answer in below manner
string p1, p2, p3, p4;
if (TextBox1.Text.Length > 5)
{
p1 = "pass";
Label1.Text = "";
}
else
{
Label1.Text = "Textbox1 value should be minimum 5 characters.";
p1 = "fail";
}
if (TextBox2.Text.Length > 5)
{
p2 = "pass";
Label2.Text = "";
}
else
{
Label2.Text = "Textbox2 value should be minimum 5 characters.";
p2 = "fail";
}
if (TextBox3.Text.Length > 5)
{
p3 = "pass";
Label3.Text = "";
}
else
{
Label3.Text = "Textbox3 value should be minimum 5 characters.";
p3 = "fail";
}
if (TextBox4.Text.Length > 5)
{
p4 = "pass";
Label4.Text = "";
}
else
{
Label4.Text = "Textbox4 value should be minimum 5 characters.";
p4 = "fail";
}
if (p1 == "pass" && p2 == "pass" && p3 == "pass" && p4 == "pass")
{
Status.Text = "All pass";
}

Categories

Resources