Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
Why, when I am running this code, does the question from the 2nd method repeat?
using System;
namespace mdisafmidf
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("Hello world!");
CallMethod0();
Console.WriteLine(CallMethod0());
Console.ReadKey();
}
public static string CallMethod0()
{
string x;
Console.WriteLine("Do you agree this is a good day?");
Console.WriteLine("a)Yes b)No");
x = Console.ReadLine();
if (x == "Yes")
{
return ("Of course this is a good day");
}
else
{
return ("Oh, try and stay more positive!");
}
}
}
}
You're calling the method twice, so it's running twice.
CallMethod0();
Console.WriteLine(CallMethod0());
When you run CallMethod0, it returns a string. You need to store the result to a string variable, and then Console.Write the variable. Since you have the method call in there twice, it is running twice.
In other words, change it to:
public static void Main(string[] args)
{
Console.WriteLine("Hello world!");
string result = CallMethod0(); // store the result like this
Console.WriteLine(result); // print the result - don't call the method again
Console.ReadKey();
}
Just modify your code to be like this:
var message = CallMethod0();
Console.WriteLine(message);
You are using CallMethod0() twice.
Above you have CallMethod0(); and Console.WriteLine(callMethod0());
If you remove CallMethod0(); it works fine.
Related
This question already has answers here:
Use a variable from another method in C#
(2 answers)
Closed 1 year ago.
Hey I'm very very new to c# coding and OOP, I'm currently working on a system that allows users to create login details, and then login with said details, however when I get to the part where I verify the users details I am having issues getting it to recognize my object, AccName, credentialCheck() can't successfully get the information from accountCreation(), I know this is a very basic thing to know but I have tried so many things and none of them have worked, or I haven't been doing them properly. My code is shown below and any guidance that could be provided would be greatly appreciated.
static void accountCreation()
{
Console.Write("Username: ");
string userNameInput = Console.ReadLine();
Console.Write("Email: ");
string userEmailInput = Console.ReadLine();
Console.Write("Password: ");
string userPasswordInput = Console.ReadLine();
Account AccName = new Account($"{userNameInput}", $"{userEmailInput}", $"{userPasswordInput}");
Console.Write($"{userNameInput} Registered successfully");
Console.WriteLine();
}
static void credentialCheck()
{
Console.Write("Email: ");
string userEmailCheck = Console.ReadLine();
Console.Write("Password: ");
string userPasswordCheck = Console.ReadLine();
if (userEmailCheck == AccName.userEmail )
{
Console.WriteLine();
Console.WriteLine("Success");
}
I suppose you are calling credentialCheck() just after accountCreation() and in the same thread. That out of the way, I supposed you have accName as a static variable and your class overall looks like this:
class Login {
protected static Account accName;
static void accountCreation()
{
// ...
accName = new Account(userNameInput, userEmailInput, userPasswordInput);
}
}
This means the variable accName remains as far as the execution context is concerned and not immediately destroyed after accountCreation() returns.
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 4 years ago.
Improve this question
I'm trying to make a proof-of-concept command-line software for my team, however something isn't right about my code. The compiler is treating the code as if I'm trying to implicitly convert a string to a bool value, yet I'm not using any booleans here at all, and I by no means am trying to convert anything into one. The following snippet can be used to reproduce the issue:
namespace default {
class test {
static void Main() {
Console.Clear();
string IsDemoMode;
Console.WriteLine("Activate Demo Mode? (Y/N)");
IsDemoMode = Console.ReadLine();
if (string IsDemoMode = "Y")
{
// Demo code...
} else
{
// Non-Demo Code...
};
}
}
}
The question here is: Why is Visual Studio thinking I'm trying to convert a string to a boolean when I'm not even close to doing that? Does the compiler expect a boolean to be there instead of a string?
You are using = instead of ==. So string = "some text" will give you string. And string == "some text" will give you bool
namespace default {
class test {
static void Main() {
Console.Clear();
string IsDemoMode;
Console.WriteLine("Activate Demo Mode? (Y/N)");
IsDemoMode = Console.ReadLine();
if (IsDemoMode == "Y")
{
// Demo code...
} else
{
// Non-Demo Code...
};
}
}
}
UPD. And it is more recommended to use Equals in such cases. So you can ignore case if you want, etc.
namespace default {
class test {
static void Main() {
Console.Clear();
string IsDemoMode;
Console.WriteLine("Activate Demo Mode? (Y/N)");
IsDemoMode = Console.ReadLine();
if (IsDemoMode.Equals("Y", StringComparison.InvariantCultureIgnoreCase))
{
// Demo code...
} else
{
// Non-Demo Code...
};
}
}
}
change the code to
if(IsDemoMode.ToLower() == "y")
{
// Demo code...
}
else
{
// Non-Demo Code...
}
You should use == instead of = in your if :
if (IsDemoMode == "Y")
{
//Demo mode
}
This question already has answers here:
What's does the dollar sign ($"string") do? [duplicate]
(2 answers)
Closed 4 years ago.
this might be a trivial question for the experienced programmers, however I came across this line of code and want to understand what it does.
please see the code below where we are adding the in the entries list and also incrementing the count as well.
public class Journal
{
private readonly List<string> entries = new List<string>();
private static int count = 0;
public int AddEntry(string text)
{
entries.Add($"{++count}: {text}"); // my question is about this line of code
return count;
}
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
what is the purpose of doller sign in the mentioned line.
it is used for string interpolation,
with it you can write something like:
Console.WriteLine($"Test, this is your message: {message}");
instead of using the older command and syntax:
Console.WriteLine(String.Format("Test, this is your message: {0}", message));
Greeting fellow programmers!
I am currently studying software development (I started not a month ago) and I have a problem that needs a solution. Underneath you can find the code and as you can see, the method WriteNumber needs help. I need to write the code in a way that when I run the program, on the console screen the following two lines will be shown:
Hello World!
81
The Main method code cannot be changed and also I cannot add more methods to the class Calculator so the code needs to be done only within the WriteNumbers method. I have tried a lot of things but I am still grasping how everything works so any help is welcome! Thank you in advance for your time.
Namespace CalculatorTest
{
class Calculator
{
public static string WriteText (string input)
{
return "" + input;
}
public static string WriteNumber()
{
}
}
class Program
{
static void Main(string[] args)
{
string s = Calculator.WriteText("Hello World!");
Console.WriteLine(s);
string n = Calculator.WriteNumber(53 + 28);
Console.WriteLine(n);
Console.Read();
}
}
Not to do your homework for you to just be copied/pasted, hopefully I can give you some hints...
Notice how the method is being invoked:
Calculator.WriteNumber(53 + 28)
The 53 + 28 part happens first, then the result of that operation is passed to the method. That result, naturally, is 81. What's important about that is its type, which is an integer.
So, reasonably, the method signature needs to accept an int as a parameter. This would be done very similarly to how the other method accepts a string as a parameter:
public static string WriteText(string input)
What, then, does that method need to do with that input? Well, it's only a single value, so there aren't any calculations to be performed on it. It would appear that the method simply needs to return the value as a string. (It's your homework so you tell me, is that correct?)
This can be done with exactly two things:
Calling .ToString() on the value
Using the return keyword to return the result of that operation
(Note: The .ToString() operation does something very intuitive on value types, such as int or double or bool. As you progress into using reference types, you're going to find that it does something very different. Any time you have a custom class on which you want to call .ToString(), you'll need to override the .ToString() method on that class first.)
Please read David's answer, it's important that you make the effort to understand why this works the way it does. That being said:
public static string WriteNumber(int number)
{
return number.ToString();
}
Thank you all for your valuable input but special thanks to David because he showed where I made my error. I forgot that the two numbers in the main function will be summed up FIRST and THEN forwarded to the method in the class Calculator. After that got cleared up, it was easy to understand what to do (basically adjust the type of the input parameter to int).
namespace CalculatorTest
{
class Calculator
{
public static string WriteText (string input)
{
return "" + input;
}
public static string WriteNumber(int sumOfNumbers)
{
return "" + sumOfNumbers;
}
}
class Program
{
static void Main(string[] args)
{
string s = Calculator.WriteText("Hello World!");
Console.WriteLine(s);
string n = Calculator.WriteNumber(53 + 28);
Console.WriteLine(n);
Console.Read();
}
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
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.
Improve this question
I'm getting a “Possibly mistaken empty statement” warning when I compile this code:
class Lab6
{
static void Main(string[] args)
{
Program fileOperation = new Program();
Console.WriteLine("Enter a name for the file:");
string fileName = Console.ReadLine();
if (File.Exists(fileName))
{
Console.WriteLine("The file name exists. Do you want to continue appendng ? (Y/N)");
string persmission = Console.ReadLine();
if (persmission.Equals("Y") || persmission.Equals("y"))
{
fileOperation.appendFile(fileName);
}
}
else
{
using (StreamWriter sw = new StreamWriter(fileName)) ;
fileOperation.appendFile(fileName);
}
}
public void appendFile(String fileName)
{
Console.WriteLine("Please enter new content for the file - type Done and press enter to finish editing:");
string newContent = Console.ReadLine();
while (newContent != "Done")
{
File.AppendAllText(fileName, (newContent + Environment.NewLine));
newContent = Console.ReadLine();
}
}
}
I tried to fix it, but I couldn't. What does this warning mean and where's the problem?
A “Possibly mistaken empty statement” warning means there's a statement in your code, would should be compound (i.e. contain a “body” like this: statement { ... more statement ... }), but instead of the body there a semicolon ; which terminates the statement. You should immediately know what and where's wrong, just by double-clicking on the warning a navigating to the respective line of code.
Common mistakes like this look like:
if (some condition) ; // mistakenly terminated
do_something(); // this is always executed
if (some condition); // mistakenly terminated
{
// this is always executed
... statement supposed to be the 'then' part, but in fact not ...
}
using (mySuperLock.AcquiredWriterLock()); // mistakenly terminated
{
... no, no, no, this not going to be executed under a lock ...
}
Specifically, in your code in this statement:
using (StreamWriter sw = new StreamWriter(fileName)) ;
there's a ; at the end, making the using empty (=useless). The immediately following line of code:
fileOperation.appendFile(fileName);
has nothing to do with any StreamWriter whatsoever, so there's apparently something missing in your code (or something left over — the using, probably?).