简体   繁体   中英

How to fix this ISeries interface implementation in Class1?

I am developing a series of numbers starting from Setstart(2) function from ISeries interface. I tried to implement this interface in Class1 but it threw an error to me. And I am getting stuck at this error and not been able to figure out to fix this. What am I missing? Please help

I tried to make all functions in the interface public I tried to remove public access specifier from interface

public interface ISeries {
    void Setstart (int a);
    int GetNext ();
    void Reset ();
}

class Class1 : ISeries {

    int val;
    void Setstart (int a) {
        val = a;
    }

    int GetNext () {
        return val++;
    }

    void Reset () {
        val = 0;
    }

    static void Main () {
        Class1 c = new Class1 ();
        c.Setstart (2);
        Console.WriteLine (c.GetNext ());
        c.Reset ();
        Console.WriteLine ();
    }
}

I expect the output to be 3 and 0 error is being generated

You need to make your methods public to be accessible outside from class which is missing other than that your code looks fine.

You need to make all the 3 methods public as per your scenario like :

public void Setstart (int a) {

and you will need to first add 1 and then return to get 3 as output as val++ will return the current value and then increment it by 1:

public int GetNext () {

        // return val++; // Post increment will not work according to question.
        val = val + 1;
        return val;
    }

Your class with complete implementation of Interface would be like following:

public class Class1 : ISeries {

    int val;
    public void Setstart (int a) {
        val = a;
    }

    public int GetNext () {
        val = val + 1;
        return val;
    }

    public void Reset () {
        val = 0;
    }
}

Your should try something like this.

Because you have to play with one variable so you have to make use of ref `keyword in this case.

and also you have to mark all the method inside a class as a public otherwise you were not be able to access those method inside main

Code:

using System;

namespace StackoverflowProblem
{
    public interface ISeries
    {
        void Setstart(ref int value);
        int GetNext(ref int value);
        void Reset(ref int value);
    }

    public class Class1 : ISeries
    {
        public int val { get; set; }

        public void Setstart(ref int value)
        {
            this.val = value;
        }

        public int GetNext(ref int value)
        {
            value = value + 1;
            this.val = value;

            return this.val;
        }

        public void Reset(ref int value)
        {
            // Resetting val.
            value = 0;
            this.val = value;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Class1 c = new Class1();

            int value = 2;
            c.Setstart(ref value);

            Console.WriteLine(" " + c.GetNext(ref value));

            c.Reset(ref value);
            Console.WriteLine();
        }
    }
}

Output:

在此处输入图片说明

val++ will incriment after returning the current value, ++val will increment first then return the value, you should now get 3, also make it proper scoped to access the methods from outside

class Class1 : ISeries {

int val;
public void Setstart (int a) {
    val = a;
}

public int GetNext () {
    return ++val;
}

public void Reset () {
    val = 0;
}

static void Main () {

    Class1 c = new Class1();
    c.Setstart(2);
    Console.WriteLine (c.GetNext());
    c.Reset();
    Console.WriteLine ("");
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM