public class Gameplay {
public static int count = 0;
AClass aClass = new AClass(ref count);
}
public class AClass {
public int count { get; set; }
public AClass(ref int c) {
this.count = c;
}
public void printCount() {
Console.WriteLine(this.count.ToString());
}
}
I have two classes like the ones above. Suppose the count variable of Gameplay increases in another class that does Gameplay.count++. I want AClass.count to always be equal to Gameplay.count.
Now, surely I could just use Gameplay.count inside AClass but if I have different counts to pass in different instances of AClass it's better to have the reference to the count you want to access. The above example always writes 0 even if Gameplay count has increased.
What would you expect this code to do?
int c = 0;
int count = c;
c++;
c and count are both references, yet mutating c somehow does not change the value of count.
It's exactly the same situation in your code.
In your code, this line:
this.count = c;
does not copy the reference to c. It copies the value at the time of the assignment to the value-type backing field for count.
You can pass a lambda expression to the constructor if you need AClass to be able to access the value of some arbitrary integer.
public class Gameplay {
public static int count = 0;
AClass aClass = new AClass( () => count );
}
public class AClass {
public int count { get { return this.getCount(); } }
private readonly Func<int> getCount;
public AClass(Func<int> getCount) {
this.getCount = getCount;
}
public void printCount() {
Console.WriteLine(this.count.ToString());
}
}
In your constructor:
public AClass(ref int c) {
this.count = c;
}
... even though the parameter c is passed by reference, as soon as you assign it to this.count, the value gets copied. So any changes to c or Gameplay.count will not be reflected in this.count.
That's going to be challenging, because integers are value types.
You might try using a class that has a Count property, and pass that in instead. If you have multiple objects with Count properties, you could declare an interface with a property of Count (let's call it IHasCount) and have AClass take an instance of IHasCount as its parameter.
Your design begins to sound like it uses the Singleton pattern, which is discouraged nowadays. You might want to read up on that.
Wrap the count into another class
public class CounterHolder
{
public int count = 0;
}
public class Gameplay
{
public static CounterHolder counterHolder = new CounterHolder();
AClass aClass = new AClass(counterHolder);
public void DoSomething()
{
// Something
counterHolder.count++;
aClass.printCount();
}
}
public class AClass
{
private CounterHolder _counterHolder;
public AClass(CounterHolder counterHolder)
{
_counterHolder = counterHolder;
}
public void printCount()
{
Console.WriteLine(_counterHolder.count.ToString());
}
}
Related
I have two constructors which feed values to readonly fields.
public class Sample
{
public Sample(string theIntAsString)
{
int i = int.Parse(theIntAsString);
_intField = i;
}
public Sample(int theInt) => _intField = theInt;
public int IntProperty => _intField;
private readonly int _intField;
}
One constructor receives the values directly, and the other does some calculation and obtains the values, then sets the fields.
Now here's the catch:
I don't want to duplicate the
setting code. In this case, just one
field is set but of course there may
well be more than one.
To make the fields readonly, I need
to set them from the constructor, so
I can't "extract" the shared code to
a utility function.
I don't know how to call one
constructor from another.
Any ideas?
Like this:
public Sample(string str) : this(int.Parse(str)) { }
If what you want can't be achieved satisfactorily without having the initialization in its own method (e.g. because you want to do too much before the initialization code, or wrap it in a try-finally, or whatever) you can have any or all constructors pass the readonly variables by reference to an initialization routine, which will then be able to manipulate them at will.
public class Sample
{
private readonly int _intField;
public int IntProperty => _intField;
private void setupStuff(ref int intField, int newValue) => intField = newValue;
public Sample(string theIntAsString)
{
int i = int.Parse(theIntAsString);
setupStuff(ref _intField,i);
}
public Sample(int theInt) => setupStuff(ref _intField, theInt);
}
Before the body of the constructor, use either:
: base (parameters)
: this (parameters)
Example:
public class People: User
{
public People (int EmpID) : base (EmpID)
{
// Add more statements here.
}
}
I am improving upon supercat's answer. I guess the following can also be done:
class Sample
{
private readonly int _intField;
public int IntProperty
{
get { return _intField; }
}
void setupStuff(ref int intField, int newValue)
{
//Do some stuff here based upon the necessary initialized variables.
intField = newValue;
}
public Sample(string theIntAsString, bool? doStuff = true)
{
//Initialization of some necessary variables.
//==========================================
int i = int.Parse(theIntAsString);
// ................
// .......................
//==========================================
if (!doStuff.HasValue || doStuff.Value == true)
setupStuff(ref _intField,i);
}
public Sample(int theInt): this(theInt, false) //"false" param to avoid setupStuff() being called two times
{
setupStuff(ref _intField, theInt);
}
}
Here is an example that calls another constructor, then checks on the property it has set.
public SomeClass(int i)
{
I = i;
}
public SomeClass(SomeOtherClass soc)
: this(soc.J)
{
if (I==0)
{
I = DoSomethingHere();
}
}
Yeah, you can call other method before of the call base or this!
public class MyException : Exception
{
public MyException(int number) : base(ConvertToString(number))
{
}
private static string ConvertToString(int number)
{
return number.toString()
}
}
Constructor chaining i.e you can use "Base" for Is a relationship and "This" you can use for same class, when you want call multiple Constructor in single call.
class BaseClass
{
public BaseClass():this(10)
{
}
public BaseClass(int val)
{
}
}
class Program
{
static void Main(string[] args)
{
new BaseClass();
ReadLine();
}
}
When you inherit a class from a base class, you can invoke the base class constructor by instantiating the derived class
class sample
{
public int x;
public sample(int value)
{
x = value;
}
}
class der : sample
{
public int a;
public int b;
public der(int value1,int value2) : base(50)
{
a = value1;
b = value2;
}
}
class run
{
public static void Main(string[] args)
{
der obj = new der(10,20);
System.Console.WriteLine(obj.x);
System.Console.WriteLine(obj.a);
System.Console.WriteLine(obj.b);
}
}
Output of the sample program is
50 10 20
You can also use this keyword to invoke a constructor from another constructor
class sample
{
public int x;
public sample(int value)
{
x = value;
}
public sample(sample obj) : this(obj.x)
{
}
}
class run
{
public static void Main(string[] args)
{
sample s = new sample(20);
sample ss = new sample(s);
System.Console.WriteLine(ss.x);
}
}
The output of this sample program is
20
Error handling and making your code reusable is key. I added string to int validation and it is possible to add other types if needed. Solving this problem with a more reusable solution could be this:
public class Sample
{
public Sample(object inputToInt)
{
_intField = objectToInt(inputToInt);
}
public int IntProperty => _intField;
private readonly int _intField;
}
public static int objectToInt(object inputToInt)
{
switch (inputToInt)
{
case int inputInt:
return inputInt;
break;
case string inputString:
if (!int.TryParse(inputString, out int parsedInt))
{
throw new InvalidParameterException($"The input {inputString} could not be parsed to int");
}
return parsedInt;
default:
throw new InvalidParameterException($"Constructor do not support {inputToInt.GetType().Name}");
break;
}
}
Please, please, and pretty please do not try this at home, or work, or anywhere really.
This is a way solve to a very very specific problem, and I hope you will not have that.
I'm posting this since it is technically an answer, and another perspective to look at it.
I repeat, do not use it under any condition. Code is to run with LINQPad.
void Main()
{
(new A(1)).Dump();
(new B(2, -1)).Dump();
var b2 = new B(2, -1);
b2.Increment();
b2.Dump();
}
class A
{
public readonly int I = 0;
public A(int i)
{
I = i;
}
}
class B: A
{
public int J;
public B(int i, int j): base(i)
{
J = j;
}
public B(int i, bool wtf): base(i)
{
}
public void Increment()
{
int i = I + 1;
var t = typeof(B).BaseType;
var ctor = t.GetConstructors().First();
ctor.Invoke(this, new object[] { i });
}
}
Since constructor is a method, you can call it with reflection. Now you either think with portals, or visualize a picture of a can of worms. sorry about this.
In my case, I had a main constructor that used an OracleDataReader as an argument, but I wanted to use different query to create the instance:
I had this code:
public Subscriber(OracleDataReader contractReader)
{
this.contract = Convert.ToString(contractReader["contract"]);
this.customerGroup = Convert.ToString(contractReader["customerGroup"]);
this.subGroup = Convert.ToString(contractReader["customerSubGroup"]);
this.pricingPlan= Convert.ToString(contractReader["pricingPlan"]);
this.items = new Dictionary<string, Member>();
this.status = 0;
}
So I created the following constructor:
public Subscriber(string contract, string customerGroup) : this(getSubReader(contract, customerGroup))
{ }
and this method:
private static OracleDataReader getSubReader(string contract, string customerGroup)
{
cmdSubscriber.Parameters[":contract"].Value = contract + "%";
cmdSubscriber.Parameters[":customerGroup"].Value = customerGroup+ "%";
return cmdSubscriber.ExecuteReader();
}
notes: a statically defined cmdSubscriber is defined elsewhere in the code; My main constructor has been simplified for this illustration.
In case you need to run something before calling another constructor not after.
public class Sample
{
static int preprocess(string theIntAsString)
{
return preprocess(int.Parse(theIntAsString));
}
static int preprocess(int theIntNeedRounding)
{
return theIntNeedRounding/100;
}
public Sample(string theIntAsString)
{
_intField = preprocess(theIntAsString)
}
public Sample(int theIntNeedRounding)
{
_intField = preprocess(theIntNeedRounding)
}
public int IntProperty => _intField;
private readonly int _intField;
}
And ValueTuple can be very helpful if you need to set more than one field.
NOTE: most of the solutions above does not work for structs.
Unfortunately initializing struct fields in a method called by a constructor is not recognized by the compiler and will lead to 2 errors:
in the constructor: Field xxxx must be fully assigned...
in the method, if you have readonly fields: a read-only field cannot be assigned except in a constructor.
These can be really frustrating for example when you just need to do simple check to decide on which constructor to orient your call to.
I have been taught in school about C#. We did some basic stuff like loops, if etc.
Now we do more about OOP. Teacher said us something about auto-implemented-property and I find this feature as great. But I am curious how can I set value of property via method.
When we didn't know auto-implemented-property. We always did a method to set or get value of class.
But when I use auto-implemented-property I do not see any methods to get or set value of class instance. So how can I set the value of some property of class when I can set the value only via constructor. I want to know that, because when property is private I can set it only via constructor, which is not a problem, but what I can do when I want to set value via Console.Readline(); ?
namespace _001_dedicnost
{
class Car
{
int Size { get; set; }
}
class Program
{
static void Main(string[] args)
{
Car car1 = new Car(5);
// but the following line wont work
car1.Set(51);
}
}
}
Your class Car have PRIVATE property Size, so u cant't have access to it from your code, only from class CAR
If u want to set value to this property, u have to declare it PUBLIC:
class Car
{
public int Size { get; set; }
}
static void Main(string[] args)
{
Car car1 = new Car();
car1.Size = 1;
}
When you put the property on the left-hand side of an expression, the set method is automatically called on it with the right-hand side of the expression as the value.
So car1.Size = 51 is like calling the expanded setter for the Size property with value being 51.
This
public class Point {
public int X { get; set; } = 0;
}
is equivalent to the following declaration:
public class Point {
private int __x = 0;
public int X { get { return __x; } set { __x = value; } }
}
This means you have "couple of 'methods' under c sharp compilator which called using '=' sign"
Point p = new Point();
p.X = 10; //c# compiler would call something like p.__set_X(10)
int i = p.X; //c# compiler would call something like int i = p.__get_X();
Read more about auto-properties https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/classes#automatically-implemented-properties
Btw I dont recommend to use it - it breaks readability and refactobility of code ;(
If the class is a simple anemic model (without logic), set the property as public, and it will work.
If you want to control the invariants (business rules), you'd want to have a public Size { get; private set; } with a public void SetSize(int size) { /* ... */ } which contains your business rules.
Here are three 'patterns' normally used in C#:
// Anemic domain model (simple entity)
public class Car
{
public int Size { get; set;}
}
// Domain model with business rules
public class Car
{
public int Size { get; private set; }
public void SetSize (int size)
{
// check to make sure size is within constraints
if (size < 0 || size > 100)
throw new ArgumentException(nameof(size));
Size = size;
}
}
// Value object
public class Car
{
public Car (int size)
{
// check constraints of size
Size = size;
}
public int Size { get; }
}
First of all, take a look at my below code:
class A
{
public static int Flag()
{
return 0;// set initial value=0
}
B b= new B();
public void afunc()
{
b.bfunc();
}
}
And class B recieves and sends static variable:
class B
{
A a= new A();
int flag= a.Flag();
public void bfunc()
{
if(flag==0)
{
flag=1;//???? is this wrong???
//do some thing
}
}
}
Class A send to B a static variable with initial value=0; then class A call bfunc from class B. In bfunc() I set flag=1. I'm a new to C#. Can you share me how class A recieves back flag=1 sended by class B. I mean which syntax?
a few things are wrong here
Flag is a method on A, so you cannot change its "value"
Flag is static therefore it does not have an instance which is what I think you want
I suspect you want Flag to be a property of A
public int Flag{get;set;}
You are making new instances of A and B, which may be correct for you but be weary this means you are not referencing existing instances
You have two options
A
this.Flag = b.bFunc();
public int bFunc()
.... return 1;
B
public void bFunc()
... a.Flag = 1;
If you really want static variable then
public static int Flag = 0;
A.Flag = x
Were is no static variable here, you only have a static function int Flag(). To get value of a flag in class A, you must return this value from function bfunc() like this:
public int bfunc()
{
if(flag==0)
{
flag=1;
return flag;
}
}
I don't know if I understood you properly because there are many things wrong with your code. Flag should be a property instead of a method so you can store your value. The way you used it was just tossing out a zero.
First, your two classes. Keep in mind that usually properties should be used as accesssors to private fields, but let's do it the simplest way.
class A
{
public static int Flag = 0;
}
class B
{
public void bfunc()
{
if (A.Flag == 0)
{
A.Flag = 1;
}
}
}
Then use them as follows to change Flag's value.
B bObject = new B();
bObject.bfunc();
// A.Flag is now 1.
Note that bfunc() will change Flag's value to 1 only if it was 0 before.
I'm trying to create a class which takes value a as a parameters in it's constructor.
It has a private member variable which stores this value. The value should not be changed afterwards.
Here's what I have, it works but I don't think it's the best solution out there:
internal class Foo
{
private int a;
public int A
{
get
{
return this.a;
}
}
public Foo(int a)
{
this.a = a;
}
}
So this way you can not access a from outside of the class, and A-property only has a get method. However, you can still change a from inside the class, and using a property which only returns one variable and nothing else feels stupid.
Am I doing this right, or is there a way to improve my code/more proper way to do this?
Additionally declare your private field readonly and you're there!
public class Foo
{
public Foo(int bar)
{
this.bar = bar;
}
public int Bar
{
get
{
return bar;
}
}
private readonly int bar;
}
“In C# 6 and later, you can initialize auto-implemented properties similarly to fields”. Just like you can initialize a readonly field in a constructor, you can initialize a get-only auto-implemented property in a constructor. Thus, the following now compiles:
public class Class1
{
public int A { get; }
public Class1(int a)
{
A = a;
}
}
…and the following yields an error:
public class Class1
{
public int A { get; }
public Class1(int a)
{
A = a;
}
public void Mutate()
{
// Class1.cs(11,9,11,10): error CS0200: Property or indexer 'Class1.A' cannot be assigned to -- it is read only
A++;
}
}
I like it—you get the terseness of field initialization with the interface/OOP-friendliness of properties.
internal class Foo
{
private readonly int _a;
public int A
{
get
{
return _a;
}
}
public Foo(int a)
{
_a = a;
}
}
This should do it.
I have a "meter" class. One property of "meter" is another class called "production".
I need to access to a property of meter class (power rating) from production class by reference. The powerRating is not known at the instantiation of Meter.
How can I do that?
public class Meter
{
private int _powerRating = 0;
private Production _production;
public Meter()
{
_production = new Production();
}
}
Store a reference to the meter instance as a member in Production:
public class Production {
//The other members, properties etc...
private Meter m;
Production(Meter m) {
this.m = m;
}
}
And then in the Meter-class:
public class Meter
{
private int _powerRating = 0;
private Production _production;
public Meter()
{
_production = new Production(this);
}
}
Also note that you need to implement an accessor method/property so that the Production class can actually access the powerRating member of the Meter class.
I wouldn't reference the parent directly in the child objects. In my opinion the childs shouldn't know anything about the parents. This will limits the flexibility!
I would solve this with events/handlers.
public class Meter
{
private int _powerRating = 0;
private Production _production;
public Meter()
{
_production = new Production();
_production.OnRequestPowerRating += new Func<int>(delegate { return _powerRating; });
_production.DoSomething();
}
}
public class Production
{
protected int RequestPowerRating()
{
if (OnRequestPowerRating == null)
throw new Exception("OnRequestPowerRating handler is not assigned");
return OnRequestPowerRating();
}
public void DoSomething()
{
int powerRating = RequestPowerRating();
Debug.WriteLine("The parents powerrating is :" + powerRating);
}
public Func<int> OnRequestPowerRating;
}
In this case I solved it with the Func<> generic, but can be done with 'normal' functions.
This why the child(Production) is totally independent from it's parent(Meter).
But! If there are too many events/handlers or you just want to pass a parent object, i would solve it with an interface:
public interface IMeter
{
int PowerRating { get; }
}
public class Meter : IMeter
{
private int _powerRating = 0;
private Production _production;
public Meter()
{
_production = new Production(this);
_production.DoSomething();
}
public int PowerRating { get { return _powerRating; } }
}
public class Production
{
private IMeter _meter;
public Production(IMeter meter)
{
_meter = meter;
}
public void DoSomething()
{
Debug.WriteLine("The parents powerrating is :" + _meter.PowerRating);
}
}
This looks pretty much the same as the solution mentions, but the interface could be defined in another assembly and can be implemented by more than 1 class.
Regards,
Jeroen van Langen.
You would need to add a property to your Production class and set it to point back at its parent, this doesn't exist by default.
Why not change the constructor on Production to let you pass in a reference at construction time:
public class Meter
{
private int _powerRating = 0;
private Production _production;
public Meter()
{
_production = new Production(this);
}
}
In the Production constructor you can assign this to a private field or a property. Then Production will always have access to is parent.
You could maybe add a method to your Production object called 'SetPowerRating(int)' which sets a property in Production, and call this in your Meter object before using the property in the Production object?
I would give the parent an ID, and store the parentID in the child object, so that you can pull information about the parent as needed without creating a parent-owns-child/child-owns-parent loop.
something like this:
public int PowerRating
{
get { return base.PowerRating; } // if power inherits from meter...
}