none
Interface in C# RRS feed

  • Question

  • Hello everyone!

    This is my exam in school and i dont know how to fix it.

    I should store the Methods of my Programm in C# to an extra class. All classes will be process together so i need to do an Interface called "operation" with an Method which called"execute". 

    My Methods at the moment in one class together called:

    - "Text_Speichern"'
    - "Zeichen_Zaehlen"
    - "Zeilen_Zaehlen"
    - "Ersetzen_Clear"

    Im very glad about any Reply! If you have to know more details about my project, please let me know! 

    Wednesday, March 13, 2019 6:19 PM

All replies

  • Hello,

    Since you are new here we tend to assist with students by pointing them in the proper direction but not to provide code samples as you learn nothing from that which is why I recommend the following, a already done code sample.

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/interface

    This is the first example on the page above which help.

    interface ISampleInterface
    {
        void SampleMethod();
    }
    
    class ImplementationClass : ISampleInterface
    {
        // Explicit interface member implementation: 
        void ISampleInterface.SampleMethod()
        {
            // Method implementation.
        }
    
        static void Main()
        {
            // Declare an interface instance.
            ISampleInterface obj = new ImplementationClass();
    
            // Call the member.
            obj.SampleMethod();
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, March 13, 2019 7:34 PM
    Moderator
  • Hi Ankomedia,

    An interface is defined as a syntactical contract that all the classes inheriting the interface should follow. The interface defines the 'what' part of the syntactical contract and the deriving classes define the 'how' part of the syntactical contract.
    Interfaces define properties, methods, and events, which are the members of the interface. Interfaces contain only the declaration of the members.It is the responsibility of the deriving class to define exactly what the method will do. 

    For example, I create interface, there are two methods: showTransaction and getAmount, I also have one class Transaction, if I want to implement these two method in Transaction class, need to extend this interface.

     public interface ITransactions
        {
            // interface members
            void showTransaction();
            double getAmount();
        }
    
        public class Transaction : ITransactions
        {
            private string tCode;
            private string date;
            private double amount;
    
            public Transaction()
            {
                tCode = " ";
                date = " ";
                amount = 0.0;
            }
            public Transaction(string c, string d, double a)
            {
                tCode = c;
                date = d;
                amount = a;
            }
            public double getAmount()
            {
                return amount;
            }
            public void showTransaction()
            {
                Console.WriteLine("Transaction: {0}", tCode);
                Console.WriteLine("Date: {0}", date);
                Console.WriteLine("Amount: {0}", getAmount());
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Transaction t1 = new Transaction("001", "8/10/2012", 78900.00);
                Transaction t2 = new Transaction("002", "9/10/2012", 451900.00);
    
                t1.showTransaction();
                t2.showTransaction();
                Console.ReadKey();
            }
        }
     

    Here is the detailed info about interface, you can take a look:

    https://www.c-sharpcorner.com/UploadFile/sekarbalag/interface-best-example-in-csharp/

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 14, 2019 3:06 AM