get current class instance [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
Ho do you get the current instance of a class?
The class have search method and cancel method.
Code example:
if (btnSearch.Text =="Search")
{
Searcher srch = new Searcher();
srch.Search();
btnSearch.Text = "Cancel";
return;
}
if (btnSearch.Text == "Cancel")
{
//call a method in the instance above for example
srch.Cancel();
}
I want to create the instance only when btnSearch.Text =="Search"; and when btnSearch.Text =="Cancel"; i want to call srch.Cancel();
////
Thanks to nmclean, problem solved, makes sense i had to declare the Search class at a higher scope to be able to access the current running instance.

Your srch variable must be declared at a higher scope than the function, otherwise it will not persist to the next time the function is called. Most likely this means it should be a field of the class:
class YourClass
{
private Searcher srch;
void YourMethod()
{
if (btnSearch.Text == "Search")
{
srch = new Searcher();
srch.Search();
btnSearch.Text = "Cancel";
return;
}
if (btnSearch.Text == "Cancel")
{
srch.Cancel();
}
}
}

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);

Can’t get a specific output using a boolean [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
I want a specific output depending on wether the Boolean is set to true or not. I can’t seem to find the solution so I’m just asking here. I don’t what to output „true“ or „false“ but a specific text.
What I tried:
public class Program
{
bool isAlive = true;
public static void Main()
{
if (isAlive == true){
Console.WriteLine("Is True");
}
}
}
What worked:
public class Program
{
public static void Main()
{
bool isAlive = true;
if (isAlive == true){
Console.WriteLine("Is True");
}
}
}
The Error was that I tried to use the non-static variable in the static void. Thanks to everyone that helped.
you can do either
string output = bool_variable ? "true value" : "false value";
Debug.Log(output);
or
if(bool_variable == true){
Debug.Log("true value")
}else{
Debug.Log("false value")
}
Edit:
You are getting the non-static error because you are trying to access isAlive from a static method when it is not static. So you either need to create an instance of Program, make isAlive static, or remove the static keyword from Main.
Further more this does not seem to be a unity question since you are using Console.WriteLine and not implementing MonoBehaviour
You can use the following code
{boolean_variable} ? "Boolean value is true" : "Boolean value is false"

how to write method which returns object? [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
Is this a write way?suggest me.
public Init_Circle Init_Circle(Point pt,double rad)
{
Point center=pt;
if (rbCircle.Checked==true)
{
pt.x = double.Parse(txtCirCntPtX.Text.Trim());
pt.y = double.Parse(txtCirCntPtY.Text.Trim());
rad = double.Parse(txtCirRadius.Text.Trim());
}
return this.Init_Circle(pt,rad);
}
You can't return anything from the constructor, but what you can do is create a static method that creates the object for you.
So in your constructor do it like this.
private Init_Circle()
{
}
and create a method like this
public static Init_Circle CreateInstance(// parameters here)
{
// do object creation here
}
You should create a method which will returns object. You can't return anything in a constructor.
Method should look like this:
public Init_Circle ReturnCircle(Point pt,double rad)
{
Point center=pt;
if (rbCircle.Checked==true)
{
pt.x = double.Parse(txtCirCntPtX.Text.Trim());
pt.y = double.Parse(txtCirCntPtY.Text.Trim());
rad = double.Parse(txtCirRadius.Text.Trim());
}
return this.Init_Circle(pt,rad);
}

MultiThreading in winform c# [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
I Want to know that i have define a function in form
Eg:Dynamically Display (Running) Current Date Time
It display the current system time on that form
Is it possible to call the same function without creating a new function on each and every form?
please help
Solution 1: You dont need to write a function for this:
Label1.Text = DateTime.Now.ToString();
Solution 2: but if you want to access it using function create a static function
public static class Utility
{
public static string DisplayDateTime()
{
return DateTime.Now.ToString();
}
}
Call the above function wherever you want as below:
Label1.Text = Utility.DisplayDateTime();
Solution 3: if you want to change datetime for every second try this:
public static class Utility
{
public static string DisplayDateTime()
{
return DateTime.Now.ToString();
}
}
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
timer1.Interval=1000;//one second
timer1.Tick += new System.EventHandler(timer1_Tick);
timer1.Start();
private void timer1_Tick(object sender, EventArgs e)
{
//do whatever you want
Label1.Text = Utility.DisplayDateTime();
}

invoked function is not working in winforms [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
am calling the function of a form from another form. It executes but do not really show up. so any suggestions??
My code goes like
In Form FrmA
private void Print()
{
FrmMenu ObjMain = new FrmMenu();
ObjMain.Show();
}
public void CreateButtons(string Action)
{
btn.Text=Action;
}
And When the Form FrmMenu gets open then another function gets executed in constructor of FrmMenu
Public FrmMenu()
{
FrmA f2 = new FrmA();
f2.CreateButtons("NEW");
}
But nothing happens ...!!! The function CreateButtons executes but do not show any changes
The text of button remain the same.
so please help me out.
EDITED
You must pass the instance of FrmA into the constructor of FrmMenu.
In FrmA:
private void Print()
{
FrmMenu ObjMain = new FrmMenu(this);
ObjMain.Show();
}
In FrmMenu:
public FrmMenu(FrmA f2)
{
f2.CreateButtons("NEW");
}

Categories

Resources