Pregunta

I have tried going through similar questions and answers about this topic but I can't seem to find (or understand) a solution.

On Form 1 I have three text boxes that will receive user input. In a separate class I have a method that needs to access the input from those text boxes.

Form1.cs consists of just the text boxes and some buttons. The method in the other class is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RealEstateApp
{
    public class RealEstateApp : Form1
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

            const double RENTAL_AMOUNT = 1000.00;

            RealEstateInvestment invest1 = new
                RealEstateInvestment(2004, 150000, "65th Street");
            invest1.MonthlyExpense = GetExpenses();
            invest1.IncomeFromRent = RENTAL_AMOUNT;
            invest1.DetermineMonthlyEarnings());
            RealEstateInvestment invest2 = new
                RealEstateInvestment("72 Westchester Dr.", 229000);

            invest2.MonthlyExpense = 900;
            invest2.IncomeFromRent = 1500.00;
        }

        public double GetExpenses()
        {
            double insurance;
            double taxes;
            double utilities;
            string inValue;

            inValue = txtBoxInsurance.Text; //Console.ReadLine(); (get user input from textbox)
            insurance = double.Parse(inValue);
            inValue = txtBoxTax.Text; //Console.ReadLine(); (get user input from textbox)
            taxes = double.Parse(inValue);
            inValue = txtBoxUtilities.Text; //Console.ReadLine(); (get user input from textbox)
            utilities = double.Parse(inValue);
            return (insurance / 12 + taxes / 12 + utilities);
        }

    }
}

As I am sure you experienced c# guys out there have already noticed, I am met with an error 'An object reference is required for the non-static field, method, or property.'

my static void Main() is in the second class if that changes anything.

EDIT

I have put the whole class code above now to give a better idea of what's going on.

Cheers,

Matt

¿Fue útil?

Solución 2

I am guessing you are getting error at inValue = txtBoxInsurance.Text;

This is because your GetExpenses() method is in static class .Whereas txtboxInsurance is private and non static property of class Form1. You cannot access non static method property from static method.

Instead What you can do is

public static  class YourStaticClass

static string _insurance;
static string _tax;
static string _utilities;

       public static double GetExpenses()
    {
        double insurance;
        double taxes;
        double utilities;
        string inValue;

        //Console.Write("Yearly Insurance: ");
        inValue = _insurance; //Console.ReadLine(); (get user input from textbox)
        insurance = double.Parse(inValue);
        //Console.Write("Yearly Tax: ");
        inValue = _tax; //Console.ReadLine(); (get user input from textbox)
        taxes = double.Parse(inValue);
        //Console.Write("Monthly Utilities: ");
        inValue = _utilities; //Console.ReadLine(); (get user input from textbox)
        utilities = double.Parse(inValue);
        return (insurance / 12 + taxes / 12 + utilities);
    }

And

public  partial class Form1 : Form 

 YourStaticClass._insurance=txtBoxInsurance.Text;
 YourStaticClass._tax=txtBoxTax.Text;
 YourStaticClass._utilities=txtBoxUtilities.Text;

I think this should work

Otros consejos

Pass the values of textboxes to function instead of textboxes it will decouple your class from the form class and you can use it by passing the values from any other class.

Definition

public static double GetExpenses(int pinsurance, int ptaxes, int putilities)
{
         return (insurance / 12 + taxes / 12 + utilities);
}

Call

Use Int32.TryParse to handle the exception for non int values in the textbox.

int insurance;
bool result = Int32.TryParse(txtBoxInsurance.Text, out insurance);
//Similarly you have to parse taxes and  utilities
GetExpenses(insurance, taxes, utilities);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top