sccfornow
sccfornow

Reputation: 11

I'm trying to return out of a method but I get a CS0219 warning

I'm trying to return out of a method in C#, but every time I run the program I get a message that I'm creating a variable but never using it, then I get errors that the current variable does not exist in current context.

I am relatively new to C#, and I might be really dumb but if anyone can help me that will be great!

The code:

using System;

class MainClass 
{
    public static void Main() 
    {
        int health = 3;
        int powerCrystals = 0;
        int healthCrystals = 0;
        int basicEnemyDamage = 1;
        int basicEnemyScore = 10;

        string basicEnemyDrops = "1 powerCrystal";
        int score = 0;
    
        // Basic Enemies Drop 1 powerCrystal.
        // Moderate Enemies Drop 1 powerCrystal and 1 healthCrystal.
        // Advanced Enemies Drop 2 powerCrystals and 1 healthCrystal.
        // Bosses Drop 3 powerCrystals and 3 healthCrystals.
       
        Console.WriteLine("Input your username:");
        string userName = Console.ReadLine();
    
        Console.WriteLine("Welcome, " + userName);
    
        Console.WriteLine("To check your stats, write: stats");
     
        TrollFight();
    }
    
    static void TrollFight() 
    {
        Console.WriteLine(userName + "! There is a pack of trolls aproaching! Guess the right number to kill them!");
        Console.WriteLine("Your current health is = " + health);
        Console.WriteLine("Guess the correct number and then you can defeat the trolls! It is between 1-9!");
    
        int guessedNumber = Convert.ToInt32(Console.ReadLine());
        
        if (guessedNumber == 4) 
        {
            Console.WriteLine("You killed the trolls! You gained: " + basicEnemyDrops);
            score = score + basicEnemyDamage;
            Console.WriteLine("Your score is = " + score);
            return;
        }
        else
        {
            Console.WriteLine("WRONG! You took damage! Come on, guess again!");
            health = health - basicEnemyDamage;
        }
    
        int guessedNumber2 = Convert.ToInt32(Console.ReadLine());
        
        if (guessedNumber2 == 4) 
        {
            Console.WriteLine("You killed the trolls! You gained: " + basicEnemyDrops);
            score = score + basicEnemyScore;
            Console.WriteLine("Your score is = " + score);
            return;
        }
        else
        {
            Console.WriteLine("WRONG! You took damage! Come on, guess again! Your health is = 2");
            health = health - basicEnemyScore;
        }
    
        int guessedNumber3 = Convert.ToInt32(Console.ReadLine());
        
        if (guessedNumber3 == 4) 
        {
            Console.WriteLine("You killed the trolls! You gained: " + basicEnemyDrops);
            score = score + basicEnemyDamage;
            Console.WriteLine("Your score is = " + score);
            return;
        }
        else
        {
            Console.WriteLine("WRONG! You took damage! Come on, guess again! Your health is = 1");
            Console.WriteLine("You are almost dead! You do not have any Health Crystals so you can't heal!");
            health = health - basicEnemyScore;
        }
    
        int guessedNumber4 = Convert.ToInt32(Console.ReadLine());
        
        if (guessedNumber4 == 4) 
        {
            Console.WriteLine("You killed the trolls! You gained: " + basicEnemyDrops);
            score = score + basicEnemyScore;
            Console.WriteLine("Your score is = " + score);
            return;
        }
        else
        {
            Console.WriteLine("WRONG! You died! Your final score = " + score);
            health = health - basicEnemyDamage;
        }
    
        Console.WriteLine("To check your stats, write: stats");
    }
}

Upvotes: 1

Views: 63

Answers (1)

Jeremy Thompson
Jeremy Thompson

Reputation: 65594

I'm trying to return out of a method but it gives me a CS0219 warning

Ok a few things. The difference between a Void/Subroutine and a Function is that a Function returns something. Both your methods are voids, they don't return anything:

public static void Main() 
static void TrollFight() 

The problem with the variables is that they are out of scope.

Local - visible within the method
Private - visible to the class
Public - visible outside the assembly

One solution is to make the variables Private member variables:

class MainClass {
      
private int health = 3;
private int powerCrystals = 0;
private int healthCrystals = 0;
private int basicEnemyDamage = 1;
private int basicEnemyScore = 10;
private string basicEnemyDrops = "1 powerCrystal";
private int score = 0;

public static void Main() {

Run the project, this should fix it. The problem with using Private variable scope is it can get messy when multiple methods can change variables (without the other knowing). In this case we can use parameters to pass variables to voids or functions:

TrollFight(basicEnemyDamage);

...
static void TrollFight(string basicEnemyDamage) {

A good design will use all three with local variables:

class MainClass {
      
private int health = 3;
private string basicEnemyDrops = "1 powerCrystal";
private int score = 0;

public static void Main() {

... }

static void TrollFight(string basicEnemyDamage) {
     int powerCrystals = 0;
     int healthCrystals = 0;
     int basicEnemyDamage = 1;
     int basicEnemyScore = 10;

I'd even change the void to return the Score:

static int TrollFight(string basicEnemyDamage, int score)
{
   return score;
}

The Code:

using System;

public class MainClass {
    
    private int health = 3;
    private int score = 0;
    private int powerCrystals = 0;
    private int healthCrystals = 0;
    private int basicEnemyDamage = 1;
    private int basicEnemyScore = 10;
    private string basicEnemyDrops = "1 powerCrystal";

    public static void Main() {
      
    //Basic Enemies Drop 1 powerCrystal.
    //Moderate Enemies Drop 1 powerCrystal and 1 healthCrystal.
    //Advanced Enemies Drop 2 powerCrystals and 1 healthCrystal.
    //Bosses Drop 3 powerCrystals and 3 healthCrystals.
      
    Console.WriteLine("Input your username:");
    string userName = Console.ReadLine();
    
    Console.WriteLine("Welcome, " + userName);
    
    Console.WriteLine("To check your stats, write: stats");
    
    TrollFight(userName);
    
    }
    
    static void TrollFight(string userName) {
        
        Console.WriteLine(userName + "! There is a pack of trolls approaching! Guess the right number to kill them!");
    Console.WriteLine("Your current health is = " + health.ToString());
    Console.WriteLine("Guess the correct number and then you can defeat the trolls! It is between 1-9!");
    
    int guessedNumber = Convert.ToInt32(Console.ReadLine());
        
        if (guessedNumber == 4) 
    {
        Console.WriteLine("You killed the trolls! You gained: " + basicEnemyDrops);
        score = score + basicEnemyDamage;
        Console.WriteLine("Your score is = " + score.ToString());
        return;
    }
    else
    {
        Console.WriteLine("WRONG! You took damage! Come on, guess again!");
        health = health - basicEnemyDamage;
    }
    
    int guessedNumber2 = Convert.ToInt32(Console.ReadLine());
        
        if (guessedNumber2 == 4) 
    {
        Console.WriteLine("You killed the trolls! You gained: " + basicEnemyDrops);
        score = score + basicEnemyScore;
        Console.WriteLine("Your score is = " + score.ToString());
        return;
    }
    else
    {
        Console.WriteLine("WRONG! You took damage! Come on, guess again! Your health is = 2");
        health = health - basicEnemyScore;
    }
    
     int guessedNumber3 = Convert.ToInt32(Console.ReadLine());
        
        if (guessedNumber3 == 4) 
    {
        Console.WriteLine("You killed the trolls! You gained: " + basicEnemyDrops);
        score = score + basicEnemyDamage;
        Console.WriteLine("Your score is = " + score.ToString());
        return;
    }
    else
    {
        Console.WriteLine("WRONG! You took damage! Come on, guess again! Your health is = 1");
        Console.WriteLine("You are almost dead! You do not have any Health Crystals so you can't heal!");
        health = health - basicEnemyScore;
    }
    
     int guessedNumber4 = Convert.ToInt32(Console.ReadLine());
        
        if (guessedNumber4 == 4) 
    {
        Console.WriteLine("You killed the trolls! You gained: " + basicEnemyDrops);
        score = score + basicEnemyScore;
        Console.WriteLine("Your score is = " + score.ToString());
        return;
    }
    else
    {
        Console.WriteLine("WRONG! You died! Your final score = " + score.ToString());
        health = health - basicEnemyDamage;
    }
    
    Console.WriteLine("To check your stats, write: stats");
  }
}

Upvotes: 1

Related Questions