Freddy Wieffering
Freddy Wieffering

Reputation: 1

Running Test file from exercism C#

I am having trouble getting a test file to run. I can't see what I need to do differently. This is the first exercism project for C#. I think I have the problem solved but I can't get the Test file to run successfully in Microsoft Visual Studio. The goal is to make a function to test whether the year is a leap year.

I want to use the class Year in this file below, in my project it is titled Class1.cs

File with class year:

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

namespace Exercism
{
    public class Year
    {   public bool IsLeap(int year)
        {
            if (year % 4 == 0)
                if (year % 100 == 0)
                    if (year % 400 == 0)
                        return true;
                    else
                        return false;
                else
                    return true;
            else
                return false;
        }
    }
}

The test file is below. When I try to run it I get the message 'The name 'Year' does not exist in the current context' for each test function below.

using NUnit.Framework;
using Exercism;




[TestFixture]
public class LeapTest
{
    [Test]
    public void Valid_leap_year()
    {
        Assert.That(Year.IsLeap(1996), Is.True);
    }
    [Ignore]
    [Test]
    public void Invalid_leap_year()
    {
        Assert.That(Year.IsLeap(1997), Is.False);
    }

    [Ignore]
    [Test]
    public void Turn_of_the_20th_century_is_not_a_leap_year()
    {
        Assert.That(Year.IsLeap(1900), Is.False);
    }

    [Ignore]
    [Test]
    public void Turn_of_the_25th_century_is_a_leap_year()
    {
        Assert.That(Year.IsLeap(2400), Is.True);
    }
}

I know this is a basic question but any help would be appreciated

Upvotes: 0

Views: 521

Answers (2)

Trey Mack
Trey Mack

Reputation: 4705

public static bool IsLeap(int year)

Upvotes: 1

Jonesopolis
Jonesopolis

Reputation: 25370

You are using IsLeap as if it were a static method, but declared it as an instance method.

You can either use new Year().IsLeap(..) or set IsLeap to public static bool IsLeap(...). I'm pretty sure you want the latter.

It's very important to understand the difference between the two, I would recommend reading up on the subject.

Upvotes: 2

Related Questions