1

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

7
  • 1
    remove static keyword... Commented Apr 24, 2014 at 4:24
  • If I do that then I get an error further up in my code when I call the GetExpenses() method. Commented Apr 24, 2014 at 4:25
  • this method in other class and textboxes in someother clasS? Commented Apr 24, 2014 at 4:27
  • looks like you are calling non-static property from static method.You need to create instance of Form1 Commented Apr 24, 2014 at 4:30
  • @Matt one of the way would be to remove static keyword and move GetExpense() method in Form1.cs Commented Apr 24, 2014 at 4:37

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply. Not quite sure I understand how to implement this change.
0

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

6 Comments

That has helped get rid of the errors regarding GetExpenses as long as I dont put in public static class YourStaticClass and YourStaticClass._insurance=txtBoxInsurance.Text; YourStaticClass._tax=txtBoxTax.Text; YourStaticClass._utilities=txtBoxUtilities.Text;
YourStaticClass is the name of the class in which you have created GetExpenses() method
Where abouts do I put YourStaticClass._insurance=txtBoxInsurance.Text; YourStaticClass._tax=txtBoxTax.Text; YourStaticClass._utilities=txtBoxUtilities.Text; ? I keep getting 'is a field but it used like a type' errors
under Public Form1(){}
I need to make _insurance etc public then.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.