I often find myself doing this:
class MyClass
{
public MyClass(int x)
{
this.x = x;
}
private int x;
...
}
Every time I add a new private member variable for configuration, I need to add it to the constructor's parameter list, to the constructor body, and to the class as a member. Is there a good programming pattern for avoiding the extra typing?
Generally speaking, If you instantiate a class with a bunch of private members that you have to pass into the constructor, you're doing something problematic already.
MyClass myClass = new MyClass(x, y, z, 7, 'c', someOtherClass)
If appropriate, you can encapsulate related fields into a struct or a different class like so
class MyClass
{
public MyClass(Coordinates coords)
{
this.coords = coords;
}
private Coordinates coords;
}
public struct Coordinates
{
public int X{get; set;}
public int Y{get; set;}
public int z{get; set;}
}
and then you can instanciate it with
MyClass myClass = new MyClass(new Coordinates() { X = 1, Y = 2, Z = 3 });
Without a particular implementation, It's kinda hard to determine the optimal solution, but if you don't actually have to set the fields from outside your class, you can do something like
class MyClass
{
public MyClass()
{
}
private int x = 2;
...
}
or
class MyClass
{
public MyClass()
{
this.x = 2;
}
private int x;
...
}
I find that I can abuse inheritance to accomplish my goal. I set up a "Loader" subclass that has the sole purpose in life of plugging in the dependencies of the base class. Then we can work with the base class and forget about the loader.
Then again, this has the horrible side-effect of preventing use of these protected member variables in the base constructor -- we need to use a .Start() function or something like that instead. So, this is a pretty bad solution, although saving some keystrokes.
public class MyClass
{
protected int param1;
protected int param2;
public void DoStuff()
{
Console.WriteLine(param1 + param2);
}
}
public class MyClassLoader : MyClass
{
public MyClassLoader()
{
param1 = 1;
param2 = 2;
}
}
class Program
{
static void Main(string[] args)
{
MyClass myObj = new MyClassLoader();
myObj.DoStuff();
Console.WriteLine("Press any key to quit.");
Console.ReadKey();
}
}
Related
I am learning about writing constructors and properties in c# and was asked to write a console app and class to operate a beverage machine. I wrote part of the class code but ran into an issue. One of the many blocks of code asks for a constructor method that starts the SodaCanCount at 5 bottles and sets the CustBalance field to zero. I don't know what this constructor should look like. I am specifically talking about the private sodaVandorClass(), right under the two private fields.
I wrote what I could so far and I have no errors however the SodaVendorClass does not look right.
namespace VendorClass
{
public class SodaVendorClass
{
// members
// fields
//Customer balance is $0 until the customer inserts a dollar
//All customer entries are one dollar increments and a soda costs one dollar.
private int CustBalance = 0;
//a machine holds 10 cans of soda
private int SodaCanCount = 5;
//A soda costs 1 dollar
//private int sodaCost = 1;
public int _SodaCanCount
{
get
{
return SodaCanCount;
}
}
public int _CustBalance
{
get
{
return CustBalance;
}
}
public int BuySoda(int pCustBalance, int SodaCanCount)
{
return SodaCanCount;
}
public void AcceptCash(int CustBalance)
{
CustBalance++;
}
public int GiveRefund(int pCustBalance)
{
return CustBalance;
}
}
I only want to see an example of a constructor that sets default values for my private class fields. Any help will be appreciated.
You can define a public constructor like below but probably you don't need one if you enable your properties to set values too
public SodaVendorClass()
{
this.CustBalance = 0;
this.SodaCanCount = 0;
}
You can make your properties writable too. Notice below are auto properties and in such case you don't need those private backing fields explicitly.
public int SodaCanCount
{
get; set;
}
public int CustBalance
{
get; set;
}
You can instantiate your type saying (using Object Initializer construct)
SodaVendorClass sc = new SodaVendorClass
{
SodaCanCount = 10,
CustBalance = 500,
};
A constructor for this class could look like this:
public SodaVendorClass () {
}
That would be an empty constructor that does nothing.
To set the two values you want, you can add some paramters:
public SodaVendorClass (int customerBalance, int sodaCount) {
this.CustBalance = customerBalance;
this.SodaCanCount = sodaCount;
}
To create an instance of this class with 5 soda cans and a customer balance of 0, you would call the constructor in the code like this:
var vendor = new SodaVendorClass(0, 5);
namespace VendorClass
{
public class SodaVendorClass
{
private int CustBalance;
private int SodaCanCount;
//...
public SodaVendorClass() // default constuctor
{
CustBalance = 0;
SodaCanCount = 5;
}
//...
}
}
Default constructor is called when you are creating object like this:
SodaVendorClass obj = new SodaVendorClass();
So obj._SodaCanCount is 5 and obj._CustBalance is 0
Also you can define constructor with parameters.
public SodaVendorClass(int balance, int count)
{
CustBalance = balance;
SodaCanCount = count;
}
and create call this constructor.
SodaVendorClass obj = new SodaVendorClass(0, 5);
A constructor is being used while creating a object like "Class obj=new Calss()". If you don define a constructor in your class a default constructor will be provided implicitly.User defined Constructor usually used for initializing value for class properties. Unlike function constructor does not have any return type at all not even void. All the answers are good.
public class SodaVendorClass{
private int CustBalance = 0;
//a machine holds 10 cans of soda
private int SodaCanCount = 5;
//A soda costs 1 dollar
//private int sodaCost = 1;
public int _SodaCanCount
{
get
{
return SodaCanCount;
}
}
public int _CustBalance
{
get
{
return CustBalance;
}
}
public SodaVendorClass(int cancount, int sodacost){
SodaCanCount=cancount;
sodaCost=sodacost;
}
}
//creating a object of Sodavendorclass
Sodavendorclass obj=new Sodavendorclass(0,0); //Provided value for class property
Notice that at the time of object creation, provided for Property. This is one of the way you can use constructor.
When I assign dog.Action as in the example:
public partial class MainWindow : Window
{
public Action<object> Action { get; set; }
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Dog dog = new Dog();
Dog dog2 = new Dog();
dog.Action = x => Action(dog2);
}
}
public class Dog
{
public int width = 5;
public Action<object> Action;
}
As you see in this example Action = null because it is not assigned.
dog.Action after assignment is Method = Void Button_Click(System.Object)
Can someone explain to me this behavior?
I can simplify your question to:
public class C
{
public void M()
{
int x = 3;
Action action = () => Console.WriteLine(x);
// 'action' now points to method called something like "<M>b__0"
}
}
If we put this code into the wonderful sharplab, we can see what the compiler generated:
public class C
{
[CompilerGenerated]
private sealed class <>c__DisplayClass0_0
{
public int x;
internal void <M>b__0()
{
Console.WriteLine(x);
}
}
public void M()
{
<>c__DisplayClass0_0 <>c__DisplayClass0_ = new <>c__DisplayClass0_0();
<>c__DisplayClass0_.x = 3;
Action action = <>c__DisplayClass0_.<M>b__0;
}
}
Because action captures the variable x, the compiler has had to create a whole new class called <>c__DisplayClass0_0, which has the field x on it. This class has a method called <M>b__0(), which uses the value of the field x in the call to Console.WriteLine.
In the method M(), the field <>c__DisplayClass0_0.x is used instead of the local variable x.
The delegate action points to the method <M>b__0() on this instance of <>c__DisplayClass0_0.
So lets say I have two classes. Class A and Class B like this:
Class A
{
B classB;
public A
{
classB = new B();
}
public void funcIHaveToUseInClassB()
{
}
}
Class B
{
A classA;
public B
{
classA = new A();
}
public void funcIHaveToUseInClassA()
{
}
}
As you can see both classes contain functions that need to be used in the other class. Class A has a function that class B has to use and the other way around. No I can't just put the functions in the other class because they heavily rely on the class they are currently in. So how would I go about doing this? With my method I create an infinite loop and get a stack overflow exception. I hope someone can help me out, thanks in advance.
EDIT:
People are asking me why I need these 2 classes to rely on each other so here it is: Class A manages everything that has to do with a WebBrowser control and class B Manages everything that has to do with a certain page in my program. Class A is being used by multiple pages, which is the reason it needs to be a seperate class. Class A sometimes needs to push info to class B. Class B sometimes needs info from the WebBrowser control class A is managing and that is why it calls a function.
Make classB and classA into public properties and initialize them from another class instead of constructor.
class A
{
public B classB { get; set; }
public void funcIHaveToUseInClassB()
{
}
public void anotherF()
{
classB.funcIHaveToUseInClassA();
}
}
class B
{
public A classA { get; set; }
public void funcIHaveToUseInClassA()
{
}
public void anotherF()
{
classA.funcIHaveToUseInClassB();
}
}
static void main()
{
// entry point
var a = new A();
var b = new B();
a.classB = b;
b.classA = a;
// do what ever you want with a and b
}
You need to pass an instance of one of your classes to the constructor of the other class.
Try this:
Class A
{
B classB;
public A()
{
classB = new B(this);
}
public void funcIHaveToUseInClassB()
{
}
}
Class B
{
A classA;
public B(A arg)
{
classA = arg;
}
public void funcIHaveToUseInClassA()
{
}
}
Update
Or just pass in the instance as a parameter to the methods like in Matt Jacobsen's answer.
Create a private and public accessor, and instantiate the property only when the private object is null, like so:
class A
{
private B _b;
public B b {
get {
if (_b == null) _b = new B();
return _b;
}
}
// Constructor can now be empty
public A()
{
}
}
Pass your reference from B/A in to A/B each time you need to use it. You don't need the constructors.
Class A
{
public void funcIHaveToUseFromClassB(B classB)
{
}
}
Class B
{
public void funcIHaveToUseFromClassA(A classA)
{
}
}
this is probably very simple, but I have always just made one big class and never tried make clean code. Now I am trying and experiencing errors..
So, this is the idea:
class1
{
method1 { value 1; value 2 }
method2 { value 3; value 4 }
method3 { uses method4 from class2 }
}
class2
{
method4 { uses values 1-4 from class1 }
}
I am doing it by calling: class1 c1 = new class1() in method4 and class2 c2 = new class2 in method3.
So this is what happens:
method1, method2 produce values 1-4
method3 calls class2 c2 = new class2
I get into class2, then into method4 and get null/0 values instead of what I made in first step.
Instead of creating a new instance of class1 in method4 you should pass the current class1 instance (accessible through this inside method3) as a parameter to this method to get the same result.
You need to be more specific...
class Class1
{
Class2 _class2;
public Class1(Class2 class2)
{
_class2 = class2;
}
public void method3()
{
//call _class2.method4()
}
}
class Class2
{
Class1 _class1;
public Class2(Class1 class1)
{
_class1 = class1;
}
public void Method4()
{
//call _class1.MethodWhatever()
}
}
So when you need to access variables within a class you can obviously do this simply via the 'public' modifier, however the below example is not best practice but we will get onto that shortly...
public class MyTestClass
{
public int MyAge;
}
This is a field - fields should really be private, and we should use a property to expose the field. However if you did do this, then you can access that like so:
var foo = new MyTestClass();
var hisAge = foo.MyAge;
Of course based on your requirements maybe you don't want the user to access the variable directly, but rather get a value after some computation has been done on other variables.
You can do this like so:
public class MyTestClass
{
private int _gamesPlayed;
private int _gamesLost;
public int NumberOfWins { get { return _gamesPlayed - _gamesLost; } }
}
NumberOfWins is a Property. It computes the values of two of our fields and reutrns it. See how we have the private modifier, these can't been seen outside of the scope of that class. NumberOfWins can be accessed the same way as MyAge in the previous example.
To be honest, it sounds like you are rather using pseudo-code or are a beginner.
I recommend checking out the following articles for a bit more information on what I have stated.
Modifiers - C# Reference
Properties C# Programming Guide
Difference between a Field and a Property in C#
it is really unclear what you want to achieve and how class1 is linked to class2. If your class2 is ONLY useful for the first class then (and only then) you could use nested classes...
class OuterClass {
string value1;
string value2;
string value3;
// ...
class InnerClass
{
OuterClass o_;
public InnerClass(OuterClass o)
{
o_ = o;
}
public string GetOuterString()
{
return o_.value1 + o.value2 + o.value3; //...
}
}
void SomeFunction()
{
InnerClass i = new InnerClass(this);
i.GetOuterString();
}
}
This would create a clear binding from the inner class (class 2) to the outer one. It is not easier though.
Edit: OK, after your edit I see a whole different story..
Well, here is some code for you. I'm not sure it it's what you require. It might help you get started, though. You can try running it here: https://dotnetfiddle.net/#
This is Class1. It exposes some of its data via properties.
public class Class1
{
// these are properties
public int Value1 { get; set; }
public int Value2 { get; set; }
public int Value3 { get; set; }
public int Value4 { get; set; }
public void Method1()
{
Value1 = 1;
Value2 = 2;
}
public void Method2()
{
Value3 = 3;
Value4 = 4;
}
public void Method3()
{
// uses method4 from class2
var c = new Class2();
c.Method4();
}
}
This is Class2. It calls methods from Class1 and accesses its properties.
public class Class2
{
public void Method4()
{
//uses values 1-4 from class1
var c = new Class1();
c.Method1();
c.Method2();
Console.WriteLine(c.Value1);
Console.WriteLine(c.Value2);
Console.WriteLine(c.Value3);
Console.WriteLine(c.Value4);
}
}
This uses both closes and shows the result:
using System;
public class Program
{
public static void Main()
{
var c1 = new Class1();
c1.Method3();
}
}
I seem to remember some kind of short hand way to initialize fields of a class sent to a constructor, something like:
Class A {
int n;
public A(int N) : n(N) {}
}
Any clues?
There is easy way to initialize class fields after constructor like this:
public class A
{
public int N;
public string S;
public A() {}
}
class B
{
void foo()
{
A a = new A() { N = 1, S = "string" }
}
}
That would be C++, but you tagged your question C#. C# has no notion of initialization lists, you simply assign your fields in the constructor. You can however chain constructors or call a base class constructor in a similar manner
// call base class constructor before your own executes
public class B : A
{
public B(int whatever)
: base(something)
{
// more code here
}
}
// call secondary constructor
public class B : A
{
private int _something;
public B() : this(10) { }
public B(int whatever)
{
_something = whatever;
}
}