C# how to break public Void [closed] - c#

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 9 years ago.
Improve this question
I have an class like this (this is an example ) I need return whole of class from everywhere I need it
Line1 :Public void test(bool start)
Line2 :{
Line3 : if (start) //do something;
Line4 : else // i use else for update with help other code and... forget it
Line5 : {
Line6 : if (xxX)
Line7 : if (sadasdas)
Line8 : if(sdaeqwr324f)
Line9 : if (Asdasdsa)
Line10: for (int a=0;a<6;a++)
Line11: {
Line12: if (c[a]==1321334)
Line13: {
Line14: t=c[1];
Line15: /* <<< i neeeed reset trace of test to first line of test Line16 :>>>*/
Line17: }
Line18: }
Line19: there are about 400 lines codes like Up at under of this line }
Line20:}
My target is line 15 – if I add a return at The tracer jump to line 17 – I need tracer jump to line 2
Sorry for my bad English ( I search to find why to solve it but I cant find anythings )

To "break" from a function use return:
public void test(bool start)
{
if (start) //do something;
else // i use else for update with help other code and... forget it
{
//do somethings (it is loop )
pleas reset trace of test to first line of test
return; // This ends the method, and returns to the caller
}
}

Related

How to force StateHasChanged in blazor? [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 12 months ago.
Improve this question
I have this code:
<div class="col-lg-12 control-section toast-default-section">
<SfToast ID="toast_default" #ref="ToastObj" Title="Error" Content="#a" </SfToast>
</div>
#code{
a = "Test";
StringBuilder b = new StringBuilder();
b.Append("Hello ");
if (b.ToString() != "")
{
a = a+ b;
StateHasChanged();
await ToastObj.ShowAsync();
}
}
When this "ToastObj" open, I need click 2 times to refresh. I don't know why.
For example:
First Result: Test
Second Result: Test Hello
I need click two times to refresh.
For the future, it would be helpful to see all the code, including the SFToast component.
However, You are calling it correctly. All you have to do to Get the page to refresh is to have the code hit the "StateHasChanged()" function. I think the problem is that you have code sitting outside of any function. Consider the #code{} to act like a normal class file with a few diffrences. For example there is no "Constructor" but the first method called on creation will be the OnInitalized(). I suggest wrapping the code and making sure all variables are initialized
#code{
protected override void OnInitialized()
{
var a = "Test";
StringBuilder b = new StringBuilder();
b.Append("Hello ");
if (b.ToString() != "")
{
a = a+ b;
StateHasChanged();
await ToastObj.ShowAsync();
}
}
}
Try use InvokeAsync(StateHasChanged);

C# ureachable code detected with if and do while [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 1 year ago.
Improve this question
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int x = 0;
do
{
if (false)
{
Console.WriteLine("hold UP");
}
++x;
Console.WriteLine(x);
} while (false);
}
}
}
That's the compiler warning you that some part of your code will never be executed. In your case it refers to this part.
if (false)
{
Console.WriteLine("hold UP");
}
A similar thing would happen in cases like this:
void Test()
{
return;
// more code here.
}
//or
void Test2()
{
while(true) //loop forever.
{ }
// more code here.
}
Console.WriteLine("hold UP"); is unreachable because if (false) will never be true, thus the code in the following block can never be executed.
Andy is correct. For the code to run inside an If statement the conditions must be true. What you have there is a static condition of False, thus it will not enter into the If statement.

The Code Won't Execute all [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 5 years ago.
Improve this question
I have the following code:
private void G1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
decimal quantity = 0, cost = 0;
decimal totalstock = 0, newtotalstock = 0;
if (decimal.TryParse(G1.Rows[e.RowIndex].Cells["RealExport"].Value.ToString(), out quantity) && decimal.TryParse(G1.Rows[e.RowIndex].Cells["Cost"].Value.ToString(), out cost))
{
decimal price = quantity * cost;
G1.Rows[e.RowIndex].Cells["Total"].Value = price.ToString();
}
if (decimal.TryParse(G1.Rows[e.RowIndex].Cells["TotalStock"].Value.ToString(), out totalstock) && decimal.TryParse(G1.Rows[e.RowIndex].Cells["RealExport"].Value.ToString(), out quantity))
{
newtotalstock = totalstock - quantity;
G1.Rows[e.RowIndex].Cells["TotalStock"].Value = newtotalstock.ToString();
return;
}
decimal avai = 0, newavai = 0;
if (decimal.TryParse(G1.Rows[e.RowIndex].Cells["RealExport"].Value.ToString(), out quantity) && decimal.TryParse(G1.Rows[e.RowIndex].Cells["AvailableStock"].Value.ToString(), out avai))
{
newavai = avai - quantity;
G1.Rows[e.RowIndex].Cells["AvailableStock"].Value = newavai.ToString();
return;
} }
The problem is, it only execute 2 out of 3 the code, I mean, when the newtotalstock is calculated, the code will end.
I try to change the newavai to up above, and the result is the same, it will calculate the newavai and pass the newtotalstock. I dont know why, all the code are correct. Please help
word "return" ends function, if you are using void type method you don't really need to use return, unless you want to quit it at certain point. The code after "return" will never be executed (only exception might be during usage "yield return", but that is another story).
void someMethod()
{
doSomething();
return;
StartWorldWarThree(); //no waries, this will never be executed :)
}
Furethermore you can always make a breakpoint in your code (click on the left border of your window, or just read about it) and then check how your code is being executed :) F10/F11 to make step in your code, F5 to go to next breakpoint or end execution, if there are no more breakpoints.
Taking in count that all conditions are true the return; will stop executing the rest of the code, if you want the 3 if to be executed, remove the return; and place it the last line of the method as
void Method() {
if() {}
if() {}
if() {}
return;
}
Or dont place it at all, because that method is void and does not need it

xamarin Android.Util.AndroidRuntimeException: Activity{x} did not call through to super.onStart() [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
I get this error for the code shown below and don't know why.
my code is:
protected override void OnStart()
{
if (WCHSBMobileApplication.Current.SuperBillObject == null)
{
AddSuperBill();
}
else
{
EditSuperBill();
}
}
what can I do?
The exception message says it all:
You removed this line:
base.OnStart();
You need to add it back:
protected override void OnStart()
{
base.OnStart();
if (WCHSBMobileApplication.Current.SuperBillObject == null)
{
AddSuperBill();
}
else
{
EditSuperBill();
}
}

How to know that a void runs? [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 8 years ago.
Improve this question
want to ask about void type, so I could know that it works or no
in PHP i could have a condition like this:
if(mysql_query($query))
{ bla bla }
else
{ print error }
how to do like that on ASP.NET?
i'm trying like this:
if (k.EditPassword(username.Text, oldPassTxt.Text, newPassTxt.Text) == true )
{
Response.Redirect("sample.aspx");
}
else
{ print error }
but of course, it cannot be like that, because void isn't boolean
Usually void functions that do work that can fail will have some other way of informing you that they failed. Often they will throw an Exception:
try
{
k.EditPassword(...)
}
catch(ApplicationException ex)
{
// print Exception
}
Response.Redirect(...)
Other times they will set a status variable or something:
k.EditPassword(...)
if (k.Result == Result.OK)
Response.Redirect(...)
else
// print error...
Looking at documentation or source code for the conditions you are trying to handle is the only way to know how to handle it.
You can use a literal control and add your text to it. So your code will go something like,
if (k.EditPassword(username.Text, oldPassTxt.Text, newPassTxt.Text) == true )
{
Response.Redirect("sample.aspx");
}
else
{ Literal1.Text = error; }
Literal should be in design file, you can add it from toolbox.
But a better and proper way would be to,
Log it. (You would need a log mecahnism)
Write a unit test :)

Categories

Resources