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.
Related
I am preparing for an exam and I have to examine various codes. One is about delegates in C# - I'm failing to see what it does, since I don't know if you can put functions from two different classes in one delegate.
Here's the code:
namespace konzolnaApplikacijaDelegateVoidMain {
public delegate int MyDelegate(int x);
class Program
{
public int number;
public Program (int x)
{
number = x;
}
public int Add(int x)
{
return x + 10;
}
public int Substract(int x)
{
return x - 10;
}
public int Multiply(int x)
{
return x * 2;
}
static void Main(string[] args)
{
MyDelegate delegate;
Program first = new Program(20);
Program second = new Program(50);
delegate = first.Add;
delegate += second.Add;
delegate -= first.Substract;
delegate += second.Multiply;
delegate += first.Add;
delegate(first.number);
delegate(second.number);
Console.Write("{0}", first.number + second.number);
}
}
}
Delegates are quite simple. Consider the following implementation of a delegate.
namespace DelegateExamples
{
class Program
{
//Declare a integer delegate to handle the functions in class A and B
public delegate int MathOps(int a, int b);
static void Main(string[] args)
{
MathOps multiply = ClassA.Multiply;
MathOps add = ClassB.Add;
int resultA = multiply(30, 30);
int resultB = add(1000, 500);
Console.WriteLine("Results: " + resultA + " " + resultB);
Console.ReadKey();
}
}
public class ClassA
{
public static int Multiply(int a, int b)
{
return a * b;
}
}
public class ClassB
{
public static int Add(int a, int b)
{
return a + b;
}
}
}
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.
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
}
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
I'm learning how to properly apply OO principles in C#. I came across a little problem and I can't figure out how to solve it. I run into the following problem:
The current situation:
public abstract class Foo
{
protected Foo()
{
//does nothing
}
}
public class Bar : Foo
{
public BarX ( int a, int b, int c) : base()
{
this.a = a;
this.b = b;
this.c = c;
}
public doStuff()
{
//does stuff
}
}
public class BarY : Foo
{
public Bar( int a, int b, int c) : base()
{
this.a = a;
this.b = b;
this.c = c;
}
public doStuff()
{
//does stuff
}
}
The point is that I hafe different types of Foo. In this case it would be circles and rectangles. I want them to have the same constructors as each type has the same attributes. They only have a different doStuff() method. I have tried many combinations but every time I try to move the arguments to the base class's constructor it tells me that 'some class does not contain a constructor that takes 0 arguments' (or 3 arguments) depending how I move around my code.
My question is how I can move the assignment of a, b and c's values to the abstract class's constructor?
The following will work:
public abstract class Foo
{
protected Foo(int a, int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
public abstract void doStuff();
}
public class Bar : Foo
{
public Bar(int a, int b, int c) : base(a, b, c)
{
}
public override void doStuff()
{
//does stuff
}
}
public class BarY : Foo
{
public BarY(int a, int b, int c) : base(a, b, c)
{
}
public override void doStuff()
{
//does stuff
}
}
That's because BarY and BarX are calling a default constructor (base()) which doesn't exist in your base class when you only have a parameterised constructor. You need to pass the arguments through as well (base(a, b, c)):
public abstract class Foo
{
protected Foo(int a, int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
public abstract void doStuff();
}
public class Bar : Foo
{
public BarX (int a, int b, int c) : base(a, b, c)
{
}
public override void doStuff()
{
//does stuff
}
}
public class BarY : Foo
{
public Bar(int a, int b, int c) : base(a, b, c)
{
}
public override void doStuff()
{
//does stuff
}
}