Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7849319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:28:42+00:00 2026-06-02T18:28:42+00:00

I have instantiated an object from another class in order to use properties from

  • 0

I have instantiated an object from another class in order to use properties from that class. Everything works fine within the button event, however, outside of the button event I get an error telling me my instantiated object is being used as a type. If I take this very same code and cut and paste it into the button event, I do not receive an error message. I do not understand what is happening and why. The object is instantiated whether it is inside or outside of the button event so why doesn’t it work outside of the button event? I need those two label fields auto-filled from another form as soon as the form opens, not when the button is clicked.

Here is my code:

public partial class MeasurementsForm : Form
{
    private MeasurementsBOL busObject = new MeasurementsBOL();

    //autofill bodyfat and body weight from nutrition form when form opens
    busObject.BodyFatB4 = double.Parse(lblBodyFatB4FromNutrition.Text);
    busObject.BodyWeightB4 = double.Parse(lblWeightB4FromNutrition.Text);

    //default constructor
    public MeasurementsForm()
    {
        InitializeComponent();
        busObject.InitializeConnection();
    }        

    //event handler for B4 input data
    private void btnEnterMeasurementsB4_Click(object sender, EventArgs e)
    {


        //convert input data and assign to variables
        busObject.ChestMeasurementB4 = double.Parse(txtChestB4.Text);
        busObject.WaistMeasurementB4 = double.Parse(txtWaistB4.Text);
        busObject.HipsMeasurementB4 = double.Parse(txtHipsB4.Text);
        busObject.RightThighB4 = double.Parse(txtRightThighB4.Text);
        busObject.LeftThighB4 = double.Parse(txtLeftThighB4.Text);
        busObject.RightArmB4 = double.Parse(txtRightArmB4.Text);
        busObject.LeftArmB4 = double.Parse(txtLeftArmB4.Text);

        //call method to save input data
        busObject.SaveB4Data();

        //clear text boxes of data
        this.txtChestB4.Clear();
        this.txtWaistB4.Clear();
        this.txtHipsB4.Clear();
        this.txtRightThighB4.Clear();
        this.txtLeftThighB4.Clear();
        this.txtRightArmB4.Clear();
        this.txtLeftArmB4.Clear();

        //close form
        this.Close();
    }

Here are my two properties from the MeasurementsBOL class. Although I don’t show it, the object has been instantiated:

//properties for variables
public double BodyFatB4 
{
    get { return bodyFatB4; }
    set { bodyFatB4 = nutritionObject.BodyFatStart;}
}

public double BodyWeightB4 
{ 
    get { return bodyWeightB4; }
    set { bodyWeightB4 = nutritionObject.BodyWeight; }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:28:44+00:00Added an answer on June 2, 2026 at 6:28 pm

    This code isn’t in any method, constructor etc:

    private MeasurementsBOL busObject = new MeasurementsBOL();
    
    //autofill bodyfat and body weight from nutrition form when form opens
    busObject.BodyFatB4 = double.Parse(lblBodyFatB4FromNutrition.Text);
    busObject.BodyWeightB4 = double.Parse(lblWeightB4FromNutrition.Text);
    

    It’s fine to have a variable declaration, but you can’t just add extra statements like that. Fortunately, you can use an object initializer:

    private MeasurementsBOL busObject = new MeasurementsBOL()
    {
        BodyFatB4 = double.Parse(lblBodyFatB4FromNutrition.Text),
        BodyWeightB4 = double.Parse(lblWeightB4FromNutrition.Text)
    };
    

    Basically, a type can only contain members such as field declarations, constructor declarations, property declarations, method declarations etc. It can’t contain just statements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose that we have previously instantiated three objects A, B, C from class D
I have some object that is instantiated in code behind, for instance, the XAML
I have a Class A that when it is instantiated and saved for the
I have a class that is a singleton. This singleton is instantiated using GCD
I'm creating a base class Node that essentially wraps instances of another class from
I have a model that, when it instantiates an object, also creates another object
We have a Rails app that instantiates a SWF object 16 times (it has
I have an object that I instantiate - it's quite nifty as it also
I have a Winforms application that dynamically instantiates external form objects for use in
I have a data object which has a base class with three derived classes,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.