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);
}
Related
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"
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I've spent the better part of an hour reading over linked items on Google about this topic; however, maybe it just is not sinking in. What do I need to do to capture the return value from a helper class in C#? This is the code:
protected void Page_Load(object sender, EventArgs e)
{
HelperClass.Calculate(a, b);
}
public static string Calculate(string a, string b)
{
string value = string.Empty;
// inner code workings
return value;
}
I know I'm missing something but I cannot for the life of me determine what. Any help would be appreciated! Thank you!
var x = HelperClass.Calculate(a, b);
Just like any other function, which returns a value?
protected void Page_Load(object sender, EventArgs e)
{
string value = HelperClass.Calculate(a, b);
}
public static string Calculate(string a, string b)
{
string value = string.Empty;
// inner code workings
return value;
}
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 6 years ago.
Improve this question
Is there any error in the following statement?
var str = "Hello how are you.";
MessageBox.Show("{0}",str);
My problem was in the following code where I was not getting it correctly. The code below is part of learning process. In the MessageBox the 'designation' variable is not coming as I stated above! I originally posted it due to my ignorance that when I test something I can use the MessageBox like Console.WriteLine().
abstract class Employee //Abstract class
{
public virtual void WhichCoEmployee()
{
MessageBox.Show("I am employed in XYZ Corporation as its {0}", designation); //My problem was in this line.
//designation varaiable was not received in
//placeholder {0} for display in MessageBox.Show.
}
public void Designation(string desig)
{
designation = desig;
}
public string designation { get; set; }
}
class CEO : Employee //Inheritance
{
public void Name()
{
MessageBox.Show("My name is Satheeshkumar K");
}
}
private void button2_Click(object sender, EventArgs e)
{
CEO ceo = new CEO(); //Initializing the CEO class.
ceo.Name();
ceo.Designation("CEO");
ceo.WhichCoEmployee();
}
Subsequenly I have rectified the MessageBox.Show problem by changing the
the Message Box code.
MessageBox.Show("I am employed in XYZ Corporation as its " + designation);
That worked fine. I have nothing more to say on this. Being a member of the stackoverflow has really helped me to learn some things due to the help from other members.
EDIT:
Simply change
MessageBox.Show("I am employed in XYZ Corporation as its {0}", designation);
to
MessageBox.Show("I am employed in XYZ Corporation as its " + designation);
BTW, you need to call this method somewhere for the code to run!
No. Your two lines seems to be perfectly correct according to the syntax.
But it seems that what you are trying to achieve is something different than what you have done.. Here's the thing: You just called this overloaded version of Show method of MessageBox class, which accepts two strings.
public static DialogResult Show(string text, string caption);
Hence in your case, a message box with "{0}" text and "Hello how are you." caption will be shown.
However, Console.WriteLine("{0}", var); is completely different. It is used to print output to the console. It also has many overloaded varieties, and in this case, it will replace {0} with the value of var variable. Hence, "Hello how are you." will be outputted to the Console.
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();
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have practised in C++ . It is a solution for 8-Queens that outputs all the 92 possible solutions.
C++ code example: What makes this loop so many times?
Then I have written it in C#. Here it is, But I have an error at the very end.
int[,] state = new int[8, 8];
solve_state(state, 0); // Error: an object reference is required for non-//static field,method
}
}
}
}
Try declaring the solve_state method as static.
// ↓
private static void solve_state(int[,] state, int count)
{
// method implementation here
}
It looks like you declared the solve_state as an instance (i.e. non-static) method. However, you cannot call an instance method without a reference to an instance of the parent class. Instead, make the solve_state method static, like this:
public class Program
{
public static void Main(string[] args)
{
...
int[,] state = new int[8, 8];
solve_state(state, 0);
...
}
private static void solve_state(int[,] state, int x)
{
...
}
}
Further Reading
static (C# Reference)