Can’t get a specific output using a boolean [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 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"

Related

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.

Unity3D with C# - Using Input field to answer question [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 2 years ago.
Improve this question
I'm creating a unity quiz game using input fields.
How can I use text from an input field to match with an answer in a c# script?
hijinxbassist's example is good, however, i made an example that includes the other parts as well, like adding event listeners and declaring the fields.
Checking for single correct answer:
public Button submitAnswerBtn; // assign a UI button object in editor
public InputField answerInput; // assign a UI inputfield object in editor
private string a1_right_answer = "foo"; // make it public and edit the answer in editor if you like
private void Awake()
{
// add event listener when button for submitting answer is clicked
submitAnswerBtn.onClick.AddListener(() => {
// validate the answer
if(answerInput.text.ToLower() == a1_right_answer) {
// success
Debug.Log("Correct");
} else {
Debug.Error("Wrong");
}
});
Checking for multiple correct answers:
public Button submitAnswerBtn; // assign a UI button object in editor
public InputField answerInput; // assign a UI inputfield object in editor
private string[] a1_right_answers = { "foo", "bar", "foo1", "bar1" }; // multiple right answers
private bool is_right_answer = false; // default value
private void Awake()
{
// add event listener when button for submitting answer is clicked
submitAnswerBtn.onClick.AddListener(() => {
// loop through all the right answers
for (int i = 0; i < a1_right_answers.Length; i++)
{
// validate the answer
if(answerInput.text.ToLower() == a1_right_answers[i]) {
// success
is_right_answer = true;
break;
}
}
// check if the user got the right or wrong answer
if(is_right_answer) {
Debug.Log("Correct");
is_right_answer = false; // reset for next guess
}
else {
Debug.Log("Wrong");
// no need to reset 'is_right_answer' since its value is already default
}
});
I am not sure which part of this problem you are stuck on, but I will attempt to answer.
I think the most important thing when comparing an input field with a stored answer is to make sure the comparison is case insensitive. You can do this by converting both texts to either lowercase or uppercase.
var usersAnswer = answerInputField.text.ToLower();
var actualAnswer = "Some Answer".ToLower();
if (usersAnswer == actualAnswer)
{
Debug.Log("You got it right!");
}

How to pass information from one void to another? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm new to coding and I have a simple problem that I cannot find the solution anywhere for.
All I would need is to pass information from one void to another.
I have tried to find a solution but many solutions go on for passing information from class to class and script to script, and that's not exactly what I need and none have worked.
The Code right now is something like this:
public static void Main(string[] args){
GetVoid.DoCheck();
}
public void GenerateInformation(){
DateTime StoredDateTime;
//Code that does something
StoredDateTime = //Date generated from the rest of the code not included
}
public void DoCheck(){
DateTime CheckedDateTime;
GenerateInformation();
CheckedDateTime = StoredDateTime;
//Rest of the code for that void
}
My end goal is for the date so simply be stored in the other DateTime but I get the error
The name 'StoredDateTime' does not exist in the current context.
As far as I know this is because it does not know what StoredDateTime is, and to fix this I would need to pass on the information from one void to the other.
But how would I do that?
void mean:
not valid or legally binding.
completely empty.
So, you cannot expect from void method to pass you result. You need to change your method signature and implement return.
public static DateTime GetDate()
{
var dateVal = DateTime.Now;
return dateVal;
}
then
public static void Main(string[] args){
var dateRetult = GetDate();
Console.WriteLine(dateResult);
}

get current class instance [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 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();
}
}
}

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