Print value from another class using Interface in C# and VB.Net

SUJAYS
 
on Apr 12, 2021 04:58 AM
591 Views

How to input the varialble in Class A and print the values from Class B as list using interface?

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Apr 13, 2021 02:07 AM

Hi SUJAYS,

Check this example. Now please take its reference and correct your code.

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    string varialble = "aspsnippets.com";

    ClassA a = new ClassA();
    string message = a.Test1(varialble);

    ClassB b = new ClassB();
    string output = b.Test2(message);

    Response.Write(string.Format("Welcome message from ClassB is <b>{0}</b>", output));
}

public interface Test
{
    string Test1(string a);
    string Test2(string a);
}

public class ClassA : Test
{
    public string Test1(string a)
    {
        return a;
    }

    public string Test2(string a)
    {
        return a;
    }
}

public class ClassB : Test
{
    public string Test1(string a)
    {
        return a;
    }

    public string Test2(string a)
    {
        return a;
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim varialble As String = "aspsnippets.com"

    Dim a As ClassA = New ClassA()
    Dim message As String = a.Test1(varialble)

    Dim b As ClassB = New ClassB()
    Dim output As String = b.Test2(message)

    Response.Write(String.Format("Welcome message from ClassB is <b>{0}</b>", output))
End Sub

Public Interface Test
    Function Test1(ByVal a As String) As String
    Function Test2(ByVal a As String) As String
End Interface

Public Class ClassA
    Implements Test

    Public Function Test1(ByVal a As String) As String Implements Test.Test1
        Return a
    End Function

    Public Function Test2(ByVal a As String) As String Implements Test.Test2
        Return a
    End Function
End Class

Public Class ClassB
    Implements Test

    Public Function Test1(ByVal a As String) As String Implements Test.Test1
        Return a
    End Function

    Public Function Test2(ByVal a As String) As String Implements Test.Test2
        Return a
    End Function
End Class

Output

Welcome message from ClassB is aspsnippets.com