Help with enumerator

Hi guys!
I’m study game development course and can’t finish assignment and nobody respond me at these course forum, so I came for help here. That what should I do: “Add code to the Game1 GetProjectileSprite method to return the appropriate sprite based on the provided projectile type”
The part of code that I have problem with.

#region Public methods

    /// <summary>
    /// Gets the projectile sprite for the given projectile type
    /// </summary>
    /// <param name="type">the projectile type</param>
    /// <returns>the projectile sprite for the type</returns>
    public static Texture2D GetProjectileSprite(ProjectileType type)
    {

        // replace with code to return correct projectile sprite based on projectile type
        return null;
    }

This is code for ProjectileType class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameProject
{
///


/// An enumeration of the projectile types
///

public enum ProjectileType
{
FrenchFries,
TeddyBear
}
}

I already Loaded sprites. And I tried few things, but I’m getting error after my code.
Error CS0161 ‘Game1.GetProjectileSprite(ProjectileType)’: not all code paths return a value

This is my code

#region Public methods

    /// <summary>
    /// Gets the projectile sprite for the given projectile type
    /// </summary>
    /// <param name="type">the projectile type</param>
    /// <returns>the projectile sprite for the type</returns>
    public static Texture2D GetProjectileSprite(ProjectileType type)
    {

        // replace with code to return correct projectile sprite based on projectile type



        if (type.Equals(ProjectileType.FrenchFries))
        {
            return frenchFriesSprite;
        }
        if (type.Equals(ProjectileType.TeddyBear))
        {
            return teddyBearProjectileSprite;
        }

Please Help!

Compiler is stupid, can’t tell that ProjectileType has only two types and you cover all cases. You have to add return null; at the end.

You have IF statements before all your returns… That means the code could fail all the ifs, and have nothing to return…

You need to make sure you return something for EVERY occasion :slight_smile:

That’s what I thought, but I started study programming like month ago, so I wasn’t sure.
Thank you, I’ll try it!

lol… Worlds apart, seconds apart…

1 Like

A better approach would be to throw an InvalidArgumentException at the end. Look it up.
Also instead of ifs , try the Switch statement, it’s suitable for this kind of code.