Possibly mistaken empty statement warning [closed] - c#

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?).

Related

Problems with Unity Random Function [closed]

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 1 year ago.
Improve this question
Good evening, I was working on a graphic adventure and I want to make a random function with a 4 digit code, turns out that as I am a very novice programmer, I crashed with an inconvenient
private void CheckResults()
{
if (codeSequence = RandomCode.RanCode.ToString())
{
SoundManager.PlaySound("nice");
}
else
{
SoundManager.PlaySound("wrong");
}
"RandomCode.RanCode" is a int void, and adding "ToString" will shot an error saying
"RanCode is a method which is not valid in the given context"
This is the RandomCode void:
public void RanCode()
{
Code = Random.Range(1111, 9999);
}
If anyone out there has any ideas or solves, I will be eternally grateful
There's no such thing as an "int void". A method either returns nothing (void) or returns a value (in your case you seem to expect int).
// Returns nothing
// |
// v
public void RanCode()
{
// assigns result of `Range` to `Code` property/field
Code = Random.Range(1111, 9999);
}
If you want to also return the value, you need to rewrite your method like this:
public int RanCode()
{
Code = Random.Range(1111, 9999);
return Code;
}
You do have another issue here:
RandomCode.RanCode.ToString()
To call a method you need (), so it should be this:
RandomCode.RanCode().ToString()
Also, for equality checks you want == not = (which is assignment).
I do wonder why you're assigning a value to Code here. If we use your existing code (without my fix above), I guess that perhaps you intended something like this instead?:
private void CheckResults()
{
RandomCode.RanCode();
if (codeSequence == RandomCode.Code.ToString())
{
SoundManager.PlaySound("nice");
}
else
{
SoundManager.PlaySound("wrong");
}
}

Can not write text to a file [closed]

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 1 year ago.
Improve this question
So, basically, I am trying to make a save point in my text rpg. I wanted to do it by using a .txt file and jumping to each method that way. However, when I try and put in the path of the file and the string contents, it returns a CS0029 error and states that it can not convert a void to a string.
public void PickingUpScroll()
{
savePoint = "PickingUpScroll";
saveData = File.WriteAllText(savePath, savePoint);
hasFireMagic = true;
Console.WriteLine("You approach the strange scroll in your room and pick it up. A strange light eminates from the foreign symbols on the paper. You feel a warmth in your hands.\nLooking down, you notice they are glowing with a strange light, almost like fire.");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("You have accquired 'Fire Magic'");
Console.ForegroundColor = ConsoleColor.White;
if (hasWeapons == false)
{
Console.WriteLine("Do you approach the weapons?\n[1] Yes [2] No");
int scrollToSwordCheck = Convert.ToInt32(Console.ReadLine());
if (scrollToSwordCheck == 1)
{
PickingUpSword();
}
}
else
{
LeavingRoom();
}
}
The File.WriteAllText method is a void (it doesn't return any value)
https://learn.microsoft.com/en-us/dotnet/api/system.io.file.writealltext?view=net-5.0
You can get rid of the variable saveData in the line:
saveData = File.WriteAllText(savePath, savePoint);
and just use the method:
File.WriteAllText(savePath, savePoint);
(and keep in mind that you will need to instantiate and assign the variable savePath somewhere prior its usage)

I'm a beginner and I can't solve some errors within C# code [closed]

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 2 years ago.
Improve this question
I'm new to C# and I'm currently learning it step by step and I also exercise at every chapter. I'm trying to make a small mad lipz game with what I learned so far but it's not working, something is missing and I think it has to be with the functions.
I'm getting these errors :
CS0103 The name 'color' does not exist in the current context
CS0103 The name 'noun' does not exist in the current context
CS0103 The name 'person' does not exist in the current context
My code:
using System;
namespace Tutorial
{
class Program
{
static void Main(string[] args)
{
Words();
Game();
Console.ReadLine();
}
static void Words()
{
Console.Write(" Write a color: ");
string color = Console.ReadLine();
Console.Write(" Write a noun: ");
string noun = Console.ReadLine();
Console.Write(" Write a person: ");
string person = Console.ReadLine();
}
static void Game()
{
Console.WriteLine("Roses are " + color);
Console.WriteLine( noun + " is dead ");
Console.WriteLine("I should vote " + person);
}
}
}
Thanks a lot!
color, noun and person are local variables in the Words method, and cannot be used outside of it. One way to solve this problem is to declare them as (static) members instead of local variables.
The problem is the scope of the program. The function Words() does not know of the variables, as they are defined in the function Game(). One possible solution would be to add the three variables as parameters to the function Words().
static void Game(string person, string color, string noun) {}

Why is Visual Studio saying that I'm trying to convert a string to a boolean when I'm not using a boolean at all? (CS0029) [closed]

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
}

Method incorrectly being run twice [closed]

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.

Categories