Answered by:
careless interface problem?

Question
-
User-1442981517 posted
Time to time slight alteration of the code is giving me sleepless nights
Now the following does not compile for any reason / help with code please?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace scratch_pad_2
{
//Demonstrate the ISeries Interface
class scratch_pad_2
{
static void Main(string[] args)
{
ByTwos ob = new ByTwos();
{
for(int i = 0 ; i<5; i++)
Console.WriteLine("Next value is"+ob.GetNext());
Console.WriteLine("\nResetting");
ob.Reset();
for(int i=0; i<5;i++)
Console.WriteLine("Next value is"+ob.GetNext());
Console.WriteLine("\nstarting at 100");
ob.SetStart(100);
for(int i=0;i<5;i++)
Console.WriteLine("Next vallue is"+ob.GetNext());
}
Console.Read();}
}public interface ISeries
{
int GetNext();
int ReSet();
int SetStart(int x);
}// implement the ISeries
class ByTwos : ISeries
{
int start;
int val;public ByTwos()
{
start = 0;
val = 0;
}public int GetNext()
{
val = val + 2;
}
public int Reset()
{val = start;
return val;}
public int SetStart(int x)
{
start = x;
val = start;
return val;
return start;
}
}}
ERROR REPORT:-
Error 1 'scratch_pad_2.ByTwos' does not implement interface member 'scratch_pad_2.ISeries.ReSet()' C:\Users\lenovo\documents\visual studio 2010\Projects\scratch pad 2\scratch_pad_2.cs 39 11 scratch pad 2Thursday, April 19, 2012 1:40 AM
Answers
-
User1866467575 posted
I found two problems in your code.
1. The implementation of GetNext function in ByTwos does not return any integer value, it should as its return type is int
2. C# is case sensitive. The Implementation of Reset function is incorrect: Name should match the name in the interface. Rename the function to ReSet as done in the interface.
These two changes should compile your code.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, April 19, 2012 1:53 AM -
User1320101480 posted
Check now:
static void Main(string[] args) { ByTwos ob = new ByTwos(); { for (int i = 0; i < 5; i++) Console.WriteLine("Next value is" + ob.GetNext()); Console.WriteLine("\nResetting"); ob.ReSet(); for (int i = 0; i < 5; i++) Console.WriteLine("Next value is" + ob.GetNext()); Console.WriteLine("\nstarting at 100"); ob.SetStart(100); for (int i = 0; i < 5; i++) Console.WriteLine("Next vallue is" + ob.GetNext()); } Console.Read(); } } public interface ISeries { int GetNext(); int ReSet(); int SetStart(int x); } // implement the ISeries class ByTwos : ISeries { int start; int val; public ByTwos() { start = 0; val = 0; } public int GetNext() { val = val + 2; return val; } public int SetStart(int x) { start = x; val = start; return val; } #region ISeries Members public int ReSet() { val = start; return val; } #endregion
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, April 19, 2012 1:56 AM
All replies
-
User1320101480 posted
I guess we solved it somedays back. Again the same problem?
Thursday, April 19, 2012 1:51 AM -
User1866467575 posted
I found two problems in your code.
1. The implementation of GetNext function in ByTwos does not return any integer value, it should as its return type is int
2. C# is case sensitive. The Implementation of Reset function is incorrect: Name should match the name in the interface. Rename the function to ReSet as done in the interface.
These two changes should compile your code.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, April 19, 2012 1:53 AM -
User1288683547 posted
Again???
in you interface you declare ReSet, but in impl used Reset, change your code like
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace scratch_pad_2 { //Demonstrate the ISeries Interface class scratch_pad_2 { static void Main(string[] args) { ByTwos ob = new ByTwos(); { for (int i = 0; i < 5; i++) Console.WriteLine("Next value is" + ob.GetNext()); Console.WriteLine("\nResetting"); ob.Reset(); for (int i = 0; i < 5; i++) Console.WriteLine("Next value is" + ob.GetNext()); Console.WriteLine("\nstarting at 100"); ob.SetStart(100); for (int i = 0; i < 5; i++) Console.WriteLine("Next vallue is" + ob.GetNext()); } Console.Read(); } } public interface ISeries { int GetNext(); int Reset(); int SetStart(int x); } // implement the ISeries class ByTwos : ISeries { int start; int val; public ByTwos() { start = 0; val = 0; } public int GetNext() { return val + 2; } public int Reset() { val = start; return val; } public int SetStart(int x) { start = x; val = start; return val; return start; } } }
Thursday, April 19, 2012 1:54 AM -
User1320101480 posted
Check now:
static void Main(string[] args) { ByTwos ob = new ByTwos(); { for (int i = 0; i < 5; i++) Console.WriteLine("Next value is" + ob.GetNext()); Console.WriteLine("\nResetting"); ob.ReSet(); for (int i = 0; i < 5; i++) Console.WriteLine("Next value is" + ob.GetNext()); Console.WriteLine("\nstarting at 100"); ob.SetStart(100); for (int i = 0; i < 5; i++) Console.WriteLine("Next vallue is" + ob.GetNext()); } Console.Read(); } } public interface ISeries { int GetNext(); int ReSet(); int SetStart(int x); } // implement the ISeries class ByTwos : ISeries { int start; int val; public ByTwos() { start = 0; val = 0; } public int GetNext() { val = val + 2; return val; } public int SetStart(int x) { start = x; val = start; return val; } #region ISeries Members public int ReSet() { val = start; return val; } #endregion
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, April 19, 2012 1:56 AM -
User-1442981517 posted
Actually it did not solve the problem it was not compiling --- also the code is changed
Thursday, April 19, 2012 1:58 AM -
User1320101480 posted
Yea check now, it's working, replace your code inside the class with the one i have posted above.
Thursday, April 19, 2012 2:02 AM -
User-1442981517 posted
thank you Saurabh Nijhawan
thank you Mãsthän Oli
thank you GopakumarFriday, April 20, 2012 2:50 AM