Implementing an interface method within same class that inherits interface.

jrock020w

New member
Joined
May 6, 2020
Messages
3
Programming Experience
Beginner
Hello,
My name is Jay, I'm new here this is my first question. I am working on project and am trying to implement an interface method in the same class that inherits the interface - and I'm receiving several errors when trying to do so. I'm not sure what the proper way to implement the method is, and when searching online for this particular issue I wasn't able to locate anything. Here is the code:

Interface:
C#:
namespace c
{
    public interface IMeasuringDevice
    {
        string PrintValues(FixedQueue<double> myCollection);
    }
}

Class:
C#:
namespace c
{
      public class Measure : IMeasuringDevice
      {
            string IMeasuringDevice.PrintValues(FixedQueue<double> myCollection)
            {
                 StringBuilder myString = new StringBuilder();
                 foreach (var i in myCollection.q)
                       myString.AppendLine(i.ToString());
                 return myString.ToString();
            }
            public string History => IMeasuringDevice.PrintValues(myQueue);[/ICODE]   //This is where Im having the problem.  Using the PrintValues method inside Measure class
      }
}

public string History => IMeasuringDevice.PrintValues(myQueue); //This is where Im having the problem. Using the PrintValues method inside the Measure class.


-------
Essentially, I just need to know what the syntax would be to call the PrintValues method in this class. Thank you so much for any help you can provide!!!
Best Regards,
Jay
 
Last edited:
Hi, and welcome to the forums. ;)

Please remember to use the code tags button in the forum editor when posting code to the forums. I have made an exception and added them for you this time.
 
You're missing braces on the class.
C#:
class ClassName : IFace
{
    // content of class here
}
 
I do have braces on the class ,this is just a snippet of the code. The specific problem I'm having is in reference to implementing the interface method into the class that implements the interface .. the syntax im using: public string History => IMeasuringDevice.PrintValues(myQueue) <-- is not working. Im 100% sure my problem lies in the syntax for the History method, because that same method History and PrintValues work just fine before implementing the interface. So, something about implementing the interface and the syntax im using to access the interface method inside the class that inherits from the interface is causing an error.

I hope this clarifies it for you, thanks again for your time!
 
Last edited:
Jay, please do not edit your original topic after people have replied. It just makes them look like idiots, when they are describing something that is no longer in the initial topic. Instead, just reply with your updated code. After looking at the revision of your initial post, you did in fact post it without braces.

if public string History => IMeasuringDevice.PrintValues(myQueue) is giving you an error, it will also be marked with a squiggly red line. Hover your mouse and tell us what the error is on the line of code reported as defective.
 
Hey guys , im new here and new to programming. To clarify, the code I provided didn't have the brackets because i copied it as a snippet, I just forgot to add the ending brackets since it was a snippet, I apologize .. I have figured out where I went wrong … I needed to instantiate both the class and the interface, even though im instantiating the same class that the method is in, which I find odd...but this did work … Im not sure if there is a better way to accomplish this, but here is what fixed it for me:

C#:
        // History method.      
        static MeasureLengthDevice measurementDevice = new MeasureLengthDevice();
        public IMeasuringDevice measuring = measurementDevice;
        public string History => measuring.PrintValues(myQueue);

Ill be checking back to see if there is possibly a better way to accomplish this feat, but aside from that I was able to solve the problem. Thanks
 
Cast or use as operator:
C#:
public string History => ((IMeasuringDevice)this).PrintValues(myQueue);
C#:
public string History => (this as IMeasuringDevice).PrintValues(myQueue);
 
Back
Top Bottom