Home > front end >  How can i convert the date type of the object to another?
How can i convert the date type of the object to another?

Time:10-24

This is my code I wrote a comment under the mistake. I am not allowed to do it in another way it should be two classes and it should be done in this way. If someone can help me i would appreciate this Thank u

using System;
using MathLibrary;

namespace MathLibraryApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Vector v = new Vector();
            Vector v1 = new Vector(4, 8, 12);
            Vector v2 = new Vector(8,16,24);
            Vector[] vectors = { v1, v2 };
            Console.WriteLine(v.Add(vectors));
        }
    }
}
using System;
namespace MathLibrary
{
    public class PointVectorBase
    {
        public PointVectorBase(double x=0 , double y=0 , double z=0 )
        {
            this.X = x;this.Y = y;this.Z = z;
        }
        protected virtual PointVectorBase CalculateSum(params Vector[] addends)
        {
            for (int i = 0; i < addends.Length; i  )
            {
                this.X = this.X   addends[i].X;
                this.Y = this.Y   addends[i].Y;
                this.Z = this.Z   addends[i].Z;
            }
            return this;
        }
    }
    public class Vector : PointVectorBase
    {

        public Vector(double x = 0, double y = 0, double z = 0) : base(x, y, z){ }
    

        public Vector Add(params Vector[] addends)
        {
           return this.CalculateSum(addends) ; 
    //Cannot implicitly convert type MathLibrary.PointVectorBase to MathLibrary.Vector. An explicit conversion exists (are you missing a cast?)
        }
    }        
}

CodePudding user response:

Your method CalculateSum returns value type PointVectorBase. Method Add in Vector class should return Vector.

Due to inheritance you can cast result of a CalculateSum to a Vector so it would be return this.CalculateSum(addends) as Vector;

CodePudding user response:

You can either cast the result like this:

public Vector Add(params Vector[] addends)
{
     return this.CalculateSum(addends) As Vector;
}

This is dangerous though. Not all base vectors are vectors so you could have a null return. Same way as an animal is not always a cat in the public class cat: animal example.

Creating the implicit conversion is safer, though not always possible: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/user-defined-conversion-operators

CodePudding user response:

In this case I wouldn't go for inheritance. You are only extending the base class with methods.

The problem on your CalculateSum is that you're returning this as a result. Which is a strange pattern. Either go for a void method which alters the current instance or return a new instance (so leave the current instance unmodified). I would go for the latter.

If your question is about inheritance, this is not a good example you gave.

But if you want an other way:

In your example I would go for extension methods. Also this is a nice case to use structs. By writing extension methods, you can extend the Vector3 with extra methods..

using System;

namespace MathLibrary
{
    public struct Vector3
    {
        public double X;
        public double Y;
        public double Z;

        public Vector3(double x=0 , double y=0 , double z=0 )
        {
            this.X = x;
            this.Y = y;
            this.Z = z;
        }

        public Vector3 CalculateSum(params Vector3[] addends)
        {
            var result = new Vector3();
            for (int i = 0; i < addends.Length; i  )
            {
                result.X = result.X   addends[i].X;
                result.Y = result.Y   addends[i].Y;
                result.Z = result.Z   addends[i].Z;
            }
            return result;
        }
    }

    public static class VectorExtensions
    {

        public static Vector3 Add(this Vector3 vector, params Vector3[] addends)
        {
           return vector.CalculateSum(addends); 

           // the add should actually add to the current vector,
           // which makes it less readable.. calculate sum and add is almost the same.
           return vector.CalculateSum( 
               new Vector3 [] { vector }
                   .Concat(addends)
                   .ToArray() );
        }
    }        
}

The more your code has a functional approach the less strange things will happen.

  • Related