knifecentipede
knifecentipede

Reputation: 53

C# - cannot convert from double to [variable name]

I'm implementing LU decomposition in C#. In MainWindow.xaml.cs I read matrix elements from the form, convert them into double, and use the LowerUpper.cs class and its functions (edited out for easier reading) for calculation. This is the problem segment:

using System;
using System.Linq;
using System.Windows;

namespace NM1test
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Compute()
        {
            double A11 = Convert.ToDouble(a11.Text);
            double A12 = Convert.ToDouble(a12.Text);
            double A13 = Convert.ToDouble(a13.Text);

            double A21 = Convert.ToDouble(a21.Text);
            double A22 = Convert.ToDouble(a22.Text);
            double A23 = Convert.ToDouble(a23.Text);

            double A31 = Convert.ToDouble(a31.Text);
            double A32 = Convert.ToDouble(a32.Text);
            double A33 = Convert.ToDouble(a33.Text);

            LowerUpper lu = new LowerUpper(A11, A12, A13, A21, A22, A23, A31, A32, A33);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Compute();
        }
    }
}

enter image description here

I found similar inquiries but they were mostly about mismatched variable types, typos, or using Convert.ToDouble wrong. I'm sure none of that is the case. I don't see why A11 is somehow a type? Those are perfectly normal functional doubles.

I also have an error in LowerUpper.cs when declaring LowerUpper(): "The type or namespace 'A11' could not be found (are you missing a using directive or an assembly reference?)".

using System;
using System.Linq;
using System.Windows;

namespace NM1test
{
    class LowerUpper
    {
        public LowerUpper(A11, A12, A13, A21, A22, A23, A31, A32, A33)
        {

        }
    }
}

I don't have lots of experience with Visual Studio projects so these errors are really confusing. What am I doing wrong?

Upvotes: 0

Views: 346

Answers (1)

Tolik Pylypchuk
Tolik Pylypchuk

Reputation: 680

The reason you have this error is that you forgot to specify the types of the arguments in the LowerUpper constructor. That's why Visual Studio thinks that A11-A33 are types.

Your LowerUpper class should look like this:

using System;
using System.Linq;
using System.Windows;

namespace NM1test
{
    class LowerUpper
    {
        public LowerUpper(double A11, double A12, double A13, double A21, double A22, double A23, double A31, double A32, double A33)
        {
            // initialization code here
        }
    }
}

Upvotes: 2

Related Questions