0

I have a total of three classes. Two of them have constructors that have balance attributes that are of type decimal. What I'm wanting to do is take these two objects and call them within another class' constructor and add their values, but the IDE is refusing to do so for some reason.

Code is like this:

class Class1{

    private decimal value; 

    Public Class1(decimal value){
         this.value = value;
    }

    public decimal Value{get; set;}
}

class Class2{

    private decimal value;

    Public Class2(decimal value){
         this.value = value;
    }

    public decimal Value{get; set;}

}

class Class3{

    public Class1 class1;
    public Class2 class2;
    private decimal value;

    public Class3(Class1 class1, Class2 class2){
    this.value = class1.Value + class2.Value; 
    }

}

Keeps saying it's just impossible. Not at work right now, so I don't have error info that's more specific.

  • 2
    You are missing field private decimal value; in your class Class3. And also they are all 0 since you assign value to field value and class 3 is using Properties Value – Michał K. Jul 19 '19 at 08:27
  • Paste your error code ? – TimChang Jul 19 '19 at 08:30
  • @Michal: I forgot to put that here but had that in the code, and it keeps referring to the properties "value" for each of the class objects in Class3's constructor as unable to be added because you can't add values of type object even though GetType refers to them as decimal. –  Jul 19 '19 at 08:35
  • Look at my answer please and check if it works for you. @TimChang is right paste errors next time. – Michał K. Jul 19 '19 at 08:39
  • Can't because I'm not at work. Thanks for the offerings so far. –  Jul 19 '19 at 08:43
  • @Michal: The error is as follows: Cannot implicitly convert from type Class1 to decimal. –  Jul 19 '19 at 08:56
  • @AndrewRamshaw Please, take a look at my answer and see if that works for you. – Matt Jul 21 '19 at 15:14
  • @AndrewRamshaw Please mark the answer that helped you (if any of provided answers did) and close the question. – Matt Aug 28 '19 at 17:26

2 Answers2

1

First of all, change

Public

to

public

Secondly, if you use {get;set;} (check for more info), you don't need to have private properties. In your case, public decimal Value is never used, you only use private decimal value.

Then, when calling Class3 constructor, you are accessing only public decimal Value which is always 0.

I suggest following:

https://dotnetfiddle.net/nspSGs

class Class1{
    public decimal Value{get;}

    public Class1(decimal value){
        this.Value = value;
    }  
}

class Class2{
    public decimal Value{get; set;}

    public Class2(decimal value){
        this.Value = value;
    }
}

class Class3{
    public Class1 class1;
    public Class2 class2;
    public decimal Value { get; };

    public Class3(Class1 class1, Class2 class2){
        this.Value = class1.Value + class2.Value; 
    }   
}
Matt
  • 1,245
  • 2
  • 17
  • 32
0

Solution to your error

 class Class1
{
    public Class1(decimal value)
    {
        this.Value = value;
    }

    public decimal Value { get; }
}

class Class2
{

    public Class2(decimal value)
    {
        this.Value = value;
    }

    public decimal Value { get; }

}

class Class3
{
    private decimal value;

    public Class3(Class1 class1, Class2 class2)
    {
        this.value = class1.Value + class2.Value;
    }

}
Michał K.
  • 121
  • 7