Accessing Abstract Class Methods C#,ASP.net - c#

I have the following classes in my program and now I want to access the method M2() present in the class Y. I tried to access it by creating the object of class Z and then casting it with variable of class X and calling x.M2(10,5) but instead of class Y it is still invoking the method M2() present in the class X. Thanks.
public partial class Abstract_Class : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Z z = new Z();
int r1 = z.M2(10, 20); //gives output -20
X x = z;
int r2 = x.M2(10,5); //gives output 10 while I want it to print 15
}
}
public class W
{
public virtual int M2(int x, int y)
{
return x - y;
}
}
public abstract class X : W
{
public abstract void M1();
public override int M2(int x, int y)
{
return 2*(x-y);
}
}
public abstract class Y : X
{
public sealed override int M2(int x, int y)
{
return 3 * (x - y);
}
}
public class Z : X
{
public override void M1()
{
}
}

You would need to create an instance of Y. Since it's abstract, you would have to create some subclass of it.
public class SubY : Y
{
}
Then in your code write something like:
var suby = new SubY();
int r2 = suby.M2(10, 5); //15

Related

How to automatically update a variable member of a class?

I defined a class in C# that has a variable member (for example x1). How can I link x to a variable outside of class (for example x2) such that anytime that x2 changes the variable x1 automatically gets updated?
class Point
{
int x1;
}
void Main()
{
int x2;
Point p = new Point();
P.x1=x2;
}
The problem is that int is not a reference type, hence, it's always copied by value.
To trivially accomplish what you seek, simply wrap the value in a reference type:
public class X
{
public int value;
}
public class Point
{
public X x1;
}
void Main()
{
X x2 = new X();
Point p = new Point();
p.x1 = x2;
x2.value = 50;
Console.WriteLine(p.x1.value); //Prints out 50
}
as Matias said, since int values are not reference types, you can't update both simultaneously
Another way to solve the problem is to wrap your value into a class and use a
setter property
class Point
{
Point(Wrapped wrapped) {
_wrapped = wrapped;
}
private Wrapped _wrapped;
private int _x1;
public int x1 {
get { return _x1; }
set {
_x1 = value;
_wrapped.x2 = value;
}
}
}
class Wrapped {
int x2;
}
Now your main method will look like
void Main()
{
var wrapped = new Wrapped();
Point p = new Point(wrapped);
P.x1= 3;
Assert.AreEquals(p.x1, wrapped.x2); // true - both are equals to 3
}
disadvantage is that now both objects are coupled.

Calling delegate not reachable?

using System;
namespace _1._75_Using_a_delegate
{
public class Program
{
public delegate int Calculate(int x, int y);
public int Add(int x, int y) { return x + y; }
public int Multiply(int x, int y) { return x * y; }
public void UseDelegate()
{
Calculate calc = Add;
Console.WriteLine(calc(3, 4)); //Displays 7
calc = Multiply;
Console.WriteLine(calc(3, 4));//Displays 12
}
public static void Main()
{
//call and execute UseDelegate()
}
}
}
This should output the above results of 7 and 12.
The delegate function is not directly callable from main in the current state.
Why can't the delegate be seen from main?
Is it necessary to create a class?
How should the delegate function be called?
You cannot call the non-static method from static Method so you have to implement another class like
internal class Check
{
public delegate int Calculate(int x, int y);
public int Add(int x, int y)
{
return x + y;
}
public int Multiply(int x, int y)
{
return x * y;
}
public void UseDelegate()
{
Calculate calc = Add;
Console.WriteLine(calc(3, 4)); //Displays 7
calc = Multiply;
Console.WriteLine(calc(3, 4));//Displays 12
}
}
your call it from your Main Method like
private static void Main(string[] args)
{
new Check().UseDelegate();
}
You don't call the method at all, and you can't now since the Main method is static and your methods are not.
I would recommend to split your code off to a second class, which is easier to call. (Instead of making all methods static)
public class Assignment
{ /* all code except the Main method goes here */ }
Then, in your Main method, instantiate an instance of the Assignment class and call UseDelegate:
public static void Main()
{
Assignment a = new Assignment();
a.UseDelegate();
Console.ReadKey(); // to prevent the console from closing immediate
}

inherited variable of class2 is different from class1 (C#)

I just made a class Shapes and an other 2 classes ('Triangle' & 'Square') which inherit from 'Shapes'.
public class Shapes
{
private int sides;
}
public class Triangle : Shapes
{
public void init()
{
int sides = 3;
throw new System.NotImplementedException();
}
}
public class Square : Shapes
{
public void init()
{
int sides = 4;
throw new System.NotImplementedException();
}
}
Code is designed using Classdiagram
Question: How should I call the class so that it shows how many sides does a shape has?
Thanks
You need a protected member sides which is used within the init-section of every shape:
public class Shapes
{
protected readonly int sides;
public int NumberOfSides { get { return sides; } }
}
public class Triangle : Shapes
{
public Triangle()
{
this.sides = 3;
}
}
public class Square : Shapes
{
public Square()
{
this.sides = 4;
}
}
As Farhad Jabiyev mentioned using constructors is the usual way to initialize a new instance (see my code above)
Now when you call Shape#NumberOfSides you get 3 for Triangle and 4 for Square:
Shape square = new Square();
int number = square.NumberOfSides();
You need to add a property on the class that has an accessor like this
public class Shapes
{
private int sides;
public int NumberOfSides { get { return sides; } }
}
Then you can go mySquare.NumberOfSides

how to impliment constructor in inheritence in c#

I am very beginner in c#. I created base class and derived class but i do not understand behavior of constructor in derived class it gives error "does not contain constructor that take 0 arguments" how to use it in derived class
class A
{
public int x, y, z;
public A(int i, int j)
{
x = i;
y = j;
}
public void add(int i,int j)
{
z=x + y;
Console.WriteLine(z);
}
}
class B : A
{
public B (int k, int l)
{
x=k;
y=l;
}
public void multi(int k,int l)
{
z = x * y;
Console.WriteLine(z);
}
}
Usage:
class Program
{
static void Main(string[] args)
{
A ad = new A(5,6);
B m = new B(8, 9);
}
}
Since B inherits A, it has to include a call to A's constructor, called the base contructor.
class B : A
{
public B (int k, int l)
: base(k, l)
{
}
}
This calls the code in A's constructor, populating x and y with the values in k and l.

A field is fixed value for each class,field is not dependent instances ...?

In below example, i defined number field. This field will work as i wanted but it is not enough efficient to provide my expectations.
number value is fixed value for each class,number is not dependent instances and number support polymorphism. How can i do that ? Or is there another solution for not use unneccesary number field for instances ?
abstract class Main
{
public int number;
public virtual void dostuff(){
int x = number;
}
}
class Derived:Main
{
public ovverride void dostuff(){
int x = number;
}
}
You could just make the number a property and initialise is in each class constructor:
abstract class Main
{
public int number{get; private set;}
public void dostuff(){
int x = number;
}
}
class Derived:Main
{
public Derived()
{
number = 5; // Specific value for each derived class
}
public void dostuff(){
int x = number;
}
}
Looks like I got the wrong end of the stick -- you want to be able to set it statically per class type, which has already been answered.
You could make the property static and then add it to each class:
abstract class Main
{
public static int number;
public virtual void dostuff(){
int x = Main.number;
}
}
class Derived : Main
{
public static int number;
public overide void dostuff(){
int x = Derived.number;
}
}
Edit: I am a bit confused by your comments about polymorhism so i have added some more examples.
Main obj = new Derived();
obj.doStuff(); //This will use Derived.number; as doStuff is and overidden virtual method.
However if you do the following:
abstract class Main
{
public static int number;
public void dostuff(){
int x = Main.number;
}
}
class Derived : Main
{
public static int number;
public new void dostuff(){
int x = Derived.number;
}
}
Then you get different behaviour as below:
Main obj = new Derived();
obj.doStuff() // Will use Main.number
Derived obj2 = (Derived)obj;
obj2.doStuff() // Will use Derived.number
If you want some other kind of behaviour i havn't defined here please exaplin because i do not understand what you want.

Categories