Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
//interface
C#
interface add
{
  int add(int a, int b);
}

C#
class adds : add
{
  public int c;
  public int add(int a, int b)
  {
    c = a + b;
    return c;
  }
}

//defult.cs
C#
adds c1 = new adds();
add val_add = new adds();
val_add.add(15, 15);
Response.Write(c1.c);
Posted
Updated 3-Aug-15 21:31pm
v2
Comments
Sergey Alexandrovich Kryukov 4-Aug-15 3:31am    
What problem, for goodness sake?!
—SA

if you need to use add interface when you want to add. try as below
C#
add c1 = new adds();
c1.add(15, 15); // call interface method 
Console.WriteLine(((adds)c1).c); // get the value 
 
Share this answer
 
v2
Comments
Member 10720577 5-Aug-15 1:41am    
This is not working...
DamithSL 5-Aug-15 4:22am    
What problem?
You are creating two different instances of the adds class:
C#
adds c1 = new adds();
add val_add = new adds();
As a result, they will each have separate c fields, which will contain different values.
Since you only call the add method on one of them, only the val_add instance will have a non-zero value.
If you did this:
C#
adds c1 = new adds();
add val_add = c1;
val_add.add(15, 15);
Response.Write(c1.c);
Then you would get the value you expect.
 
Share this answer
 
Comments
Member 10720577 5-Aug-15 1:41am    
Pls...interface using two values add method
OriginalGriff 5-Aug-15 2:19am    
Please...remember that we can't see your screen, access your HDD, or read your mind.
So a tiny phrase doesn't tell me much about what problem you are still having...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900