14 Replies - 779 Views - Last Post: 05 June 2010 - 09:15 AM Rate Topic: -----

#1 Guest_JB*


Reputation:

Variables + Windows Forms Applications

Posted 03 June 2010 - 02:42 PM

I can't figure out where to store variable to be used thoughout the entire program.


#pragma once
#include <math.h>
#include <ctype.h>


namespace JB {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;



	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{

	public:
		Form1(void)
		{

			InitializeComponent();

		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}




Here are the first 45 lines of my code and I tried adding my variables before namespace (line 5) and after InitializeComponent(); (line 33) amongst other places and nothing has worked.

Is This A Good Question/Topic? 0

Replies To: Variables + Windows Forms Applications

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1075
  • View blog
  • Posts: 4,536
  • Joined: 09-June 09

Re: Variables + Windows Forms Applications

Posted 03 June 2010 - 02:46 PM

I dont know .NET, but im sure the principle about constructors is the same
   public ref class Form1 : public System::Windows::Forms::Form 
        { 
		//declare Your variables HERE
		
		int x;
 
        public: 
                Form1(void) 
                { 
			//Initalize variables here
			x = 20;
 
                        InitializeComponent(); 
 
                } 
 
        protected: 
                /// <summary> 
                /// Clean up any resources being used. 
                /// </summary> 
                ~Form1() 
                { 
                        if (components) 
                        { 
                                delete components; 
                        } 
                }



EDIT: I was wrong

This post has been edited by ImaSexy: 03 June 2010 - 07:27 PM

Was This Post Helpful? 0
  • +
  • -

#3 Guest_JB*


Reputation:

Re: Variables + Windows Forms Applications

Posted 03 June 2010 - 04:43 PM

When I try that I get the following error message:

only static data members can be initialized inside a ref class or value type
Was This Post Helpful? 0

#4 Guest_JB*


Reputation:

Re: Variables + Windows Forms Applications

Posted 03 June 2010 - 05:47 PM

This is driving me nuts!!! I pasted the entire code of what I have so far. I can't get me code to recognize the variables.

#pragma once
#include <math.h>
#include <ctype.h>


namespace JB {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;



	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{

	public:
		Form1(void)
		{

			InitializeComponent();
// I am trying to place the variables here so they may be used throughout the entire program.
			double loanBalance = 0.0;
			double interestPaid = 0.0;
			double principlePaid = 0.0;

			double loanLength[3];
			double interestRate[3];
			
			loanLength[0]=7;
			loanLength[1]=15;
			loanLength[2]=30;
			
			interestRate[0]=5.35;
			interestRate[1]=5.5;
			interestRate[2]=5.75;
			
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::MaskedTextBox^  loanAmount;
	private: System::Windows::Forms::Button^  mortgageOne;
	private: System::Windows::Forms::Button^  mortgageTwo;
	private: System::Windows::Forms::Button^  mortgageThree;
	protected: 



	private: System::Windows::Forms::Label^  lblLoanAmount;
	private: System::Windows::Forms::TextBox^  monthlyPayment;
	private: System::Windows::Forms::Label^  lblMonthlyPayment;
	private: System::Windows::Forms::RichTextBox^  richTextBox1;
	private: System::Windows::Forms::Button^  buttonreset;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->loanAmount = (gcnew System::Windows::Forms::MaskedTextBox());
			this->mortgageOne = (gcnew System::Windows::Forms::Button());
			this->mortgageTwo = (gcnew System::Windows::Forms::Button());
			this->mortgageThree = (gcnew System::Windows::Forms::Button());
			this->lblLoanAmount = (gcnew System::Windows::Forms::Label());
			this->monthlyPayment = (gcnew System::Windows::Forms::TextBox());
			this->lblMonthlyPayment = (gcnew System::Windows::Forms::Label());
			this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
			this->buttonreset = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// loanAmount
			// 
			this->loanAmount->HidePromptOnLeave = true;
			this->loanAmount->Location = System::Drawing::Point(123, 24);
			this->loanAmount->Mask = L"999999.99";
			this->loanAmount->Name = L"loanAmount";
			this->loanAmount->Size = System::Drawing::Size(100, 20);
			this->loanAmount->TabIndex = 2;
			// 
			// mortgageOne
			// 
			this->mortgageOne->Location = System::Drawing::Point(237, 18);
			this->mortgageOne->Name = L"mortgageOne";
			this->mortgageOne->Size = System::Drawing::Size(153, 23);
			this->mortgageOne->TabIndex = 3;
			this->mortgageOne->Text = L"7 Years at 5.35%";
			this->mortgageOne->UseVisualStyleBackColor = true;
			this->mortgageOne->Click += gcnew System::EventHandler(this, &Form1::mortgageOne_Click);
			// 
			// mortgageTwo
			// 
			this->mortgageTwo->Location = System::Drawing::Point(237, 47);
			this->mortgageTwo->Name = L"mortgageTwo";
			this->mortgageTwo->Size = System::Drawing::Size(153, 23);
			this->mortgageTwo->TabIndex = 4;
			this->mortgageTwo->Text = L"15 Years at 5.5%";
			this->mortgageTwo->UseVisualStyleBackColor = true;
			// 
			// mortgageThree
			// 
			this->mortgageThree->Location = System::Drawing::Point(237, 76);
			this->mortgageThree->Name = L"mortgageThree";
			this->mortgageThree->Size = System::Drawing::Size(153, 23);
			this->mortgageThree->TabIndex = 5;
			this->mortgageThree->Text = L"30 Years at 5.75%";
			this->mortgageThree->UseVisualStyleBackColor = true;
			// 
			// lblLoanAmount
			// 
			this->lblLoanAmount->AutoSize = true;
			this->lblLoanAmount->Font = (gcnew System::Drawing::Font(L"Calibri", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->lblLoanAmount->ForeColor = System::Drawing::Color::DarkSlateBlue;
			this->lblLoanAmount->Location = System::Drawing::Point(12, 26);
			this->lblLoanAmount->Name = L"lblLoanAmount";
			this->lblLoanAmount->Size = System::Drawing::Size(85, 15);
			this->lblLoanAmount->TabIndex = 6;
			this->lblLoanAmount->Text = L"Loan Amount:";
			// 
			// monthlyPayment
			// 
			this->monthlyPayment->Location = System::Drawing::Point(123, 78);
			this->monthlyPayment->Name = L"monthlyPayment";
			this->monthlyPayment->ReadOnly = true;
			this->monthlyPayment->Size = System::Drawing::Size(100, 20);
			this->monthlyPayment->TabIndex = 7;
			// 
			// lblMonthlyPayment
			// 
			this->lblMonthlyPayment->AutoSize = true;
			this->lblMonthlyPayment->Font = (gcnew System::Drawing::Font(L"Calibri", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->lblMonthlyPayment->ForeColor = System::Drawing::Color::DarkSlateBlue;
			this->lblMonthlyPayment->Location = System::Drawing::Point(12, 81);
			this->lblMonthlyPayment->Name = L"lblMonthlyPayment";
			this->lblMonthlyPayment->Size = System::Drawing::Size(109, 15);
			this->lblMonthlyPayment->TabIndex = 9;
			this->lblMonthlyPayment->Text = L"Monthly Payment:";
			// 
			// richTextBox1
			// 
			this->richTextBox1->Location = System::Drawing::Point(15, 116);
			this->richTextBox1->Name = L"richTextBox1";
			this->richTextBox1->Size = System::Drawing::Size(375, 138);
			this->richTextBox1->TabIndex = 10;
			this->richTextBox1->Text = L"";
			// 
			// buttonreset
			// 
			this->buttonreset->Location = System::Drawing::Point(98, 260);
			this->buttonreset->Name = L"buttonreset";
			this->buttonreset->Size = System::Drawing::Size(213, 23);
			this->buttonreset->TabIndex = 11;
			this->buttonreset->Text = L"Reset";
			this->buttonreset->UseVisualStyleBackColor = true;
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(409, 295);
			this->Controls->Add(this->buttonreset);
			this->Controls->Add(this->richTextBox1);
			this->Controls->Add(this->lblMonthlyPayment);
			this->Controls->Add(this->monthlyPayment);
			this->Controls->Add(this->lblLoanAmount);
			this->Controls->Add(this->mortgageThree);
			this->Controls->Add(this->mortgageTwo);
			this->Controls->Add(this->mortgageOne);
			this->Controls->Add(this->loanAmount);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->ResumeLayout(false);
			this->PerformLayout();



		}
#pragma endregion
	private: System::Void mortgageOne_Click(System::Object^  sender, System::EventArgs^  e) {
// If I place the variables here it works.  But I need to create a few more buttons and I would have to rewrite the variables each time......also defeating the purpose of an array
				 double vLoanAmount = Convert::ToDouble(loanAmount->Text);			


				 double vMonthlyPayment = (vLoanAmount*((interestRate[0]/100)/12)) /		
					 (1-1 / pow((1 + (interestRate[0]/100) / 12), loanLength[0]*12)); 				 
				 
				 monthlyPayment->Text = Convert::ToString(vMonthlyPayment);			// Output payment 
				 monthlyPayment->Text = vMonthlyPayment.ToString("F2");				// Converts output (2 decimals)
				 

			 }
};
}




Was This Post Helpful? 0

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1075
  • View blog
  • Posts: 4,536
  • Joined: 09-June 09

Re: Variables + Windows Forms Applications

Posted 03 June 2010 - 07:24 PM

declare your variables here

     public ref class Form1 : public System::Windows::Forms::Form
        {

       int myVar; //this is private

        public:
                Form1(void)
                {




and initialize them in the initialize function
void InitializeComponent(void)
{
    myVar = 10;


This post has been edited by ImaSexy: 03 June 2010 - 07:27 PM

Was This Post Helpful? 0
  • +
  • -

#6 Guest_JB*


Reputation:

Re: Variables + Windows Forms Applications

Posted 04 June 2010 - 09:07 AM

I tried your latest suggestion and I couldn't get that to work either.
Was This Post Helpful? 0

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Variables + Windows Forms Applications

Posted 04 June 2010 - 09:15 AM

Post your current code and the exact errors you receive
Was This Post Helpful? 0
  • +
  • -

#8 Guest_JB*


Reputation:

Re: Variables + Windows Forms Applications

Posted 04 June 2010 - 11:26 AM

Here is my entire code. Which works fine; but that is because I rewrote the variables for every click action. The variables are the same but when I try to place them to cover the entire program I get an error.


#pragma once
#include <math.h>
#include <ctype.h>


namespace JB {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;



	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{

	public:
		Form1(void)
		{

			InitializeComponent();

		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::MaskedTextBox^  loanAmount;
	private: System::Windows::Forms::Button^  mortgageOne;
	private: System::Windows::Forms::Button^  mortgageTwo;
	private: System::Windows::Forms::Button^  mortgageThree;
	protected: 



	private: System::Windows::Forms::Label^  lblLoanAmount;
	private: System::Windows::Forms::TextBox^  monthlyPayment;
	private: System::Windows::Forms::Label^  lblMonthlyPayment;
	private: System::Windows::Forms::RichTextBox^  paymentTable;

	private: System::Windows::Forms::Button^  buttonreset;
	private: System::Windows::Forms::Label^  lblPaymentNo;
	private: System::Windows::Forms::Label^  lblBalance;
	private: System::Windows::Forms::Label^  lblInterestPaid;


	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->loanAmount = (gcnew System::Windows::Forms::MaskedTextBox());
			this->mortgageOne = (gcnew System::Windows::Forms::Button());
			this->mortgageTwo = (gcnew System::Windows::Forms::Button());
			this->mortgageThree = (gcnew System::Windows::Forms::Button());
			this->lblLoanAmount = (gcnew System::Windows::Forms::Label());
			this->monthlyPayment = (gcnew System::Windows::Forms::TextBox());
			this->lblMonthlyPayment = (gcnew System::Windows::Forms::Label());
			this->paymentTable = (gcnew System::Windows::Forms::RichTextBox());
			this->buttonreset = (gcnew System::Windows::Forms::Button());
			this->lblPaymentNo = (gcnew System::Windows::Forms::Label());
			this->lblBalance = (gcnew System::Windows::Forms::Label());
			this->lblInterestPaid = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// loanAmount
			// 
			this->loanAmount->HidePromptOnLeave = true;
			this->loanAmount->Location = System::Drawing::Point(123, 24);
			this->loanAmount->Mask = L"999999.99";
			this->loanAmount->Name = L"loanAmount";
			this->loanAmount->Size = System::Drawing::Size(100, 20);
			this->loanAmount->TabIndex = 2;
			// 
			// mortgageOne
			// 
			this->mortgageOne->Location = System::Drawing::Point(237, 18);
			this->mortgageOne->Name = L"mortgageOne";
			this->mortgageOne->Size = System::Drawing::Size(153, 23);
			this->mortgageOne->TabIndex = 3;
			this->mortgageOne->Text = L"7 Years at 5.35%";
			this->mortgageOne->UseVisualStyleBackColor = true;
			this->mortgageOne->Click += gcnew System::EventHandler(this, &Form1::mortgageOne_Click);
			// 
			// mortgageTwo
			// 
			this->mortgageTwo->Location = System::Drawing::Point(237, 47);
			this->mortgageTwo->Name = L"mortgageTwo";
			this->mortgageTwo->Size = System::Drawing::Size(153, 23);
			this->mortgageTwo->TabIndex = 4;
			this->mortgageTwo->Text = L"15 Years at 5.5%";
			this->mortgageTwo->UseVisualStyleBackColor = true;
			this->mortgageTwo->Click += gcnew System::EventHandler(this, &Form1::mortgageTwo_Click);
			// 
			// mortgageThree
			// 
			this->mortgageThree->Location = System::Drawing::Point(237, 76);
			this->mortgageThree->Name = L"mortgageThree";
			this->mortgageThree->Size = System::Drawing::Size(153, 23);
			this->mortgageThree->TabIndex = 5;
			this->mortgageThree->Text = L"30 Years at 5.75%";
			this->mortgageThree->UseVisualStyleBackColor = true;
			this->mortgageThree->Click += gcnew System::EventHandler(this, &Form1::mortgageThree_Click);
			// 
			// lblLoanAmount
			// 
			this->lblLoanAmount->AutoSize = true;
			this->lblLoanAmount->Font = (gcnew System::Drawing::Font(L"Calibri", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->lblLoanAmount->ForeColor = System::Drawing::Color::DarkSlateBlue;
			this->lblLoanAmount->Location = System::Drawing::Point(12, 26);
			this->lblLoanAmount->Name = L"lblLoanAmount";
			this->lblLoanAmount->Size = System::Drawing::Size(85, 15);
			this->lblLoanAmount->TabIndex = 6;
			this->lblLoanAmount->Text = L"Loan Amount:";
			// 
			// monthlyPayment
			// 
			this->monthlyPayment->Location = System::Drawing::Point(123, 78);
			this->monthlyPayment->Name = L"monthlyPayment";
			this->monthlyPayment->ReadOnly = true;
			this->monthlyPayment->Size = System::Drawing::Size(100, 20);
			this->monthlyPayment->TabIndex = 7;
			// 
			// lblMonthlyPayment
			// 
			this->lblMonthlyPayment->AutoSize = true;
			this->lblMonthlyPayment->Font = (gcnew System::Drawing::Font(L"Calibri", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->lblMonthlyPayment->ForeColor = System::Drawing::Color::DarkSlateBlue;
			this->lblMonthlyPayment->Location = System::Drawing::Point(12, 81);
			this->lblMonthlyPayment->Name = L"lblMonthlyPayment";
			this->lblMonthlyPayment->Size = System::Drawing::Size(109, 15);
			this->lblMonthlyPayment->TabIndex = 9;
			this->lblMonthlyPayment->Text = L"Monthly Payment:";
			// 
			// paymentTable
			// 
			this->paymentTable->Location = System::Drawing::Point(15, 132);
			this->paymentTable->Name = L"paymentTable";
			this->paymentTable->ReadOnly = true;
			this->paymentTable->ScrollBars = System::Windows::Forms::RichTextBoxScrollBars::Vertical;
			this->paymentTable->Size = System::Drawing::Size(375, 147);
			this->paymentTable->TabIndex = 10;
			this->paymentTable->Text = L"";
			// 
			// buttonreset
			// 
			this->buttonreset->Location = System::Drawing::Point(95, 285);
			this->buttonreset->Name = L"buttonreset";
			this->buttonreset->Size = System::Drawing::Size(213, 23);
			this->buttonreset->TabIndex = 11;
			this->buttonreset->Text = L"Reset";
			this->buttonreset->UseVisualStyleBackColor = true;
			this->buttonreset->Click += gcnew System::EventHandler(this, &Form1::buttonreset_Click);
			// 
			// lblPaymentNo
			// 
			this->lblPaymentNo->AutoSize = true;
			this->lblPaymentNo->Location = System::Drawing::Point(12, 116);
			this->lblPaymentNo->Name = L"lblPaymentNo";
			this->lblPaymentNo->Size = System::Drawing::Size(58, 13);
			this->lblPaymentNo->TabIndex = 12;
			this->lblPaymentNo->Text = L"Payment #";
			// 
			// lblBalance
			// 
			this->lblBalance->AutoSize = true;
			this->lblBalance->Location = System::Drawing::Point(110, 116);
			this->lblBalance->Name = L"lblBalance";
			this->lblBalance->Size = System::Drawing::Size(99, 13);
			this->lblBalance->TabIndex = 13;
			this->lblBalance->Text = L"Remaining Balance";
			// 
			// lblInterestPaid
			// 
			this->lblInterestPaid->AutoSize = true;
			this->lblInterestPaid->Location = System::Drawing::Point(254, 116);
			this->lblInterestPaid->Name = L"lblInterestPaid";
			this->lblInterestPaid->Size = System::Drawing::Size(66, 13);
			this->lblInterestPaid->TabIndex = 14;
			this->lblInterestPaid->Text = L"Interest Paid";
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(409, 320);
			this->Controls->Add(this->lblInterestPaid);
			this->Controls->Add(this->lblBalance);
			this->Controls->Add(this->lblPaymentNo);
			this->Controls->Add(this->buttonreset);
			this->Controls->Add(this->paymentTable);
			this->Controls->Add(this->lblMonthlyPayment);
			this->Controls->Add(this->monthlyPayment);
			this->Controls->Add(this->lblLoanAmount);
			this->Controls->Add(this->mortgageThree);
			this->Controls->Add(this->mortgageTwo);
			this->Controls->Add(this->mortgageOne);
			this->Controls->Add(this->loanAmount);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion

	private: System::Void mortgageOne_Click(System::Object^  sender, System::EventArgs^  e) {

		    double vLoanAmount = Convert::ToDouble(loanAmount->Text);			// XXXXXXXXXXXXXXX
			double loanBalance = 0.0; 
			double interestPaid = 0.0; 
			double principlePaid = 0.0; 
			
			double loanLength[3];
			double interestRate[3];
			
			loanLength[0]=7;
			loanLength[1]=15;
			loanLength[2]=30;
			
			interestRate[0]=5.35;
			interestRate[1]=5.5;
			interestRate[2]=5.75;	


				 double vMonthlyPayment = (vLoanAmount*((interestRate[0]/100)/12)) /		
					 (1-1 / pow((1 + (interestRate[0]/100) / 12), loanLength[0]*12)); 				 
				 
				 monthlyPayment->Text = Convert::ToString(vMonthlyPayment);			// Output payment 
				 monthlyPayment->Text = vMonthlyPayment.ToString("F2");				// Converts output (2 decimals)
				 

				 for (int x = 1; x <=(loanLength[0]*12); x++)
				 {
					 loanBalance = vLoanAmount;
					 
					 interestPaid = ((interestRate[0] / 12) * loanBalance / 100);  
					 principlePaid = (vMonthlyPayment - interestPaid);
					 vLoanAmount = (loanBalance - principlePaid);

					 
					 paymentTable->AppendText(x + "\t" "\t" + "$" + (vLoanAmount.ToString("F2")) + " \t\t " + "$" + (interestPaid.ToString("F2"))+"\n");

				 if (loanBalance <= 0) 
				 { 
					 paymentTable->AppendText("\tRemaining balance = $0.00");
				 }
				 }
			 }

private: System::Void mortgageTwo_Click(System::Object^  sender, System::EventArgs^  e) {
		    double vLoanAmount = Convert::ToDouble(loanAmount->Text);			// XXXXXXXXXXXXXXX
			double loanBalance = 0.0; 
			double interestPaid = 0.0; 
			double principlePaid = 0.0; 
			
			double loanLength[3];
			double interestRate[3];
			
			loanLength[0]=7;
			loanLength[1]=15;
			loanLength[2]=30;
			
			interestRate[0]=5.35;
			interestRate[1]=5.5;
			interestRate[2]=5.75;	


				 double vMonthlyPayment = (vLoanAmount*((interestRate[1]/100)/12)) /		
					 (1-1 / pow((1 + (interestRate[1]/100) / 12), loanLength[1]*12)); 				 
				 
				 monthlyPayment->Text = Convert::ToString(vMonthlyPayment);			// Output payment 
				 monthlyPayment->Text = vMonthlyPayment.ToString("F2");				// Converts output (2 decimals)
				 

				 for (int x = 1; x <=(loanLength[1]*12); x++)
				 {
					 loanBalance = vLoanAmount;
					 
					 interestPaid = ((interestRate[1] / 12) * loanBalance / 100);  
					 principlePaid = (vMonthlyPayment - interestPaid);
					 vLoanAmount = (loanBalance - principlePaid);

					 
					 paymentTable->AppendText(x + "\t" "\t" + "$" + (vLoanAmount.ToString("F2")) + " \t\t " + "$" + (interestPaid.ToString("F2"))+"\n");

				 if (loanBalance <= 0) 
				 { 
					 paymentTable->AppendText("\tRemaining balance = $0.00");
				 }
				 }
		 }
private: System::Void mortgageThree_Click(System::Object^  sender, System::EventArgs^  e) {
		    double vLoanAmount = Convert::ToDouble(loanAmount->Text);			
			double loanBalance = 0.0; 
			double interestPaid = 0.0;
			double principlePaid = 0.0; 
			
			double loanLength[3];
			double interestRate[3];
			
			loanLength[0]=7;
			loanLength[1]=15;
			loanLength[2]=30;
			
			interestRate[0]=5.35;
			interestRate[1]=5.5;
			interestRate[2]=5.75;	


				 double vMonthlyPayment = (vLoanAmount*((interestRate[2]/100)/12)) /		
					 (1-1 / pow((1 + (interestRate[2]/100) / 12), loanLength[2]*12)); 				 
				 
				 monthlyPayment->Text = Convert::ToString(vMonthlyPayment);			// Output payment 
				 monthlyPayment->Text = vMonthlyPayment.ToString("F2");				// Converts output (2 decimals)
				 

				 for (int x = 1; x <=(loanLength[2]*12); x++)
				 {
					 loanBalance = vLoanAmount;
					 
					 interestPaid = ((interestRate[2] / 12) * loanBalance / 100);  
					 principlePaid = (vMonthlyPayment - interestPaid);
					 vLoanAmount = (loanBalance - principlePaid);

					 
					 paymentTable->AppendText(x + "\t" "\t" + "$" + (vLoanAmount.ToString("F2")) + " \t\t " + "$" + (interestPaid.ToString("F2"))+"\n");

				 if (loanBalance <= 0) 
				 { 
					 paymentTable->AppendText("\tRemaining balance = $0.00");
				 }
				 }
		 }
private: System::Void buttonreset_Click(System::Object^  sender, System::EventArgs^  e) {
			 loanAmount->Clear();
			 monthlyPayment->Clear();
			 paymentTable->Clear();
		 }
};
}





Was This Post Helpful? 0

#9 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Variables + Windows Forms Applications

Posted 04 June 2010 - 09:03 PM

View PostJB, on 05 June 2010 - 03:26 AM, said:

The variables are the same but when I try to place them to cover the entire program I get an error.


Can you post an example of the code that gives you the errors along with the errors.
I can't guess what it is you are trying to do and what might be wrong with how you are trying to do it.
Was This Post Helpful? 0
  • +
  • -

#10 Guest_JB*


Reputation:

Re: Variables + Windows Forms Applications

Posted 05 June 2010 - 05:55 AM

I am trying to declare the variables once for the entire program because all the variables for each button click are the same. In the program that I posted that works I just reworte the variables in each button click, which is redundant and defeats the purpose of using arrays (arrays are required.) Below is the program that doesn't work where I declared the variables multiple times.



/**
* Author:Gemayel Sermon
* SermonWk3.cpp
* Date: 06/04/10
* Write the program as an object-oriented C++ program that calculates and displays the mortgage
* payment amount from user input of the amount of the mortgage.
* Select from a menu of mortgage loans:
* - 7 year at 5.35%
* - 15 year at 5.5%
* - 30 year at 5.75%. 
* Use an array for the different loans. List the loan balance and interest paid for each payment 
* over the term of the loan.
**/


#pragma once
#include <math.h>
#include <ctype.h>


namespace JB {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;



	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{

	public:
		Form1(void)
		{

			InitializeComponent();
			double loanBalance = 0.0;		// Variables 				
			double interestPaid = 0.0;		// for mortgage
			double principlePaid = 0.0;		// calculation
			
			double loanLength[3];			// Array variables
			double interestRate[3];			// for loan term &
											// loan length
			loanLength[0]=7;
			loanLength[1]=15;
			loanLength[2]=30;
			
			interestRate[0]=5.35;
			interestRate[1]=5.5;
			interestRate[2]=5.75;

		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::MaskedTextBox^  loanAmount;
	private: System::Windows::Forms::Button^  mortgageOne;
	private: System::Windows::Forms::Button^  mortgageTwo;
	private: System::Windows::Forms::Button^  mortgageThree;
	protected: 



	private: System::Windows::Forms::Label^  lblLoanAmount;
	private: System::Windows::Forms::TextBox^  monthlyPayment;
	private: System::Windows::Forms::Label^  lblMonthlyPayment;
	private: System::Windows::Forms::RichTextBox^  paymentTable;

	private: System::Windows::Forms::Button^  buttonreset;
	private: System::Windows::Forms::Label^  lblPaymentNo;
	private: System::Windows::Forms::Label^  lblBalance;
	private: System::Windows::Forms::Label^  lblInterestPaid;


	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
			this->loanAmount = (gcnew System::Windows::Forms::MaskedTextBox());
			this->mortgageOne = (gcnew System::Windows::Forms::Button());
			this->mortgageTwo = (gcnew System::Windows::Forms::Button());
			this->mortgageThree = (gcnew System::Windows::Forms::Button());
			this->lblLoanAmount = (gcnew System::Windows::Forms::Label());
			this->monthlyPayment = (gcnew System::Windows::Forms::TextBox());
			this->lblMonthlyPayment = (gcnew System::Windows::Forms::Label());
			this->paymentTable = (gcnew System::Windows::Forms::RichTextBox());
			this->buttonreset = (gcnew System::Windows::Forms::Button());
			this->lblPaymentNo = (gcnew System::Windows::Forms::Label());
			this->lblBalance = (gcnew System::Windows::Forms::Label());
			this->lblInterestPaid = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// loanAmount
			// 
			this->loanAmount->HidePromptOnLeave = true;
			this->loanAmount->Location = System::Drawing::Point(123, 24);
			this->loanAmount->Mask = L"999999.99";
			this->loanAmount->Name = L"loanAmount";
			this->loanAmount->Size = System::Drawing::Size(100, 20);
			this->loanAmount->TabIndex = 2;
			// 
			// mortgageOne
			// 
			this->mortgageOne->BackColor = System::Drawing::Color::Silver;
			this->mortgageOne->Location = System::Drawing::Point(237, 18);
			this->mortgageOne->Name = L"mortgageOne";
			this->mortgageOne->Size = System::Drawing::Size(153, 23);
			this->mortgageOne->TabIndex = 3;
			this->mortgageOne->Text = L"7 Years at 5.35%";
			this->mortgageOne->UseVisualStyleBackColor = false;
			this->mortgageOne->Click += gcnew System::EventHandler(this, &Form1::mortgageOne_Click);
			// 
			// mortgageTwo
			// 
			this->mortgageTwo->BackColor = System::Drawing::Color::Silver;
			this->mortgageTwo->Location = System::Drawing::Point(237, 47);
			this->mortgageTwo->Name = L"mortgageTwo";
			this->mortgageTwo->Size = System::Drawing::Size(153, 23);
			this->mortgageTwo->TabIndex = 4;
			this->mortgageTwo->Text = L"15 Years at 5.5%";
			this->mortgageTwo->UseVisualStyleBackColor = false;
			this->mortgageTwo->Click += gcnew System::EventHandler(this, &Form1::mortgageTwo_Click);
			// 
			// mortgageThree
			// 
			this->mortgageThree->BackColor = System::Drawing::Color::Silver;
			this->mortgageThree->Location = System::Drawing::Point(237, 76);
			this->mortgageThree->Name = L"mortgageThree";
			this->mortgageThree->Size = System::Drawing::Size(153, 23);
			this->mortgageThree->TabIndex = 5;
			this->mortgageThree->Text = L"30 Years at 5.75%";
			this->mortgageThree->UseVisualStyleBackColor = false;
			this->mortgageThree->Click += gcnew System::EventHandler(this, &Form1::mortgageThree_Click);
			// 
			// lblLoanAmount
			// 
			this->lblLoanAmount->AutoSize = true;
			this->lblLoanAmount->BackColor = System::Drawing::SystemColors::ActiveCaptionText;
			this->lblLoanAmount->Font = (gcnew System::Drawing::Font(L"Calibri", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->lblLoanAmount->ForeColor = System::Drawing::Color::White;
			this->lblLoanAmount->Location = System::Drawing::Point(12, 26);
			this->lblLoanAmount->Name = L"lblLoanAmount";
			this->lblLoanAmount->Size = System::Drawing::Size(85, 15);
			this->lblLoanAmount->TabIndex = 6;
			this->lblLoanAmount->Text = L"Loan Amount:";
			// 
			// monthlyPayment
			// 
			this->monthlyPayment->BackColor = System::Drawing::SystemColors::Window;
			this->monthlyPayment->Location = System::Drawing::Point(123, 78);
			this->monthlyPayment->Name = L"monthlyPayment";
			this->monthlyPayment->ReadOnly = true;
			this->monthlyPayment->Size = System::Drawing::Size(100, 20);
			this->monthlyPayment->TabIndex = 7;
			// 
			// lblMonthlyPayment
			// 
			this->lblMonthlyPayment->AutoSize = true;
			this->lblMonthlyPayment->BackColor = System::Drawing::SystemColors::ActiveCaptionText;
			this->lblMonthlyPayment->Font = (gcnew System::Drawing::Font(L"Calibri", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->lblMonthlyPayment->ForeColor = System::Drawing::Color::White;
			this->lblMonthlyPayment->Location = System::Drawing::Point(12, 81);
			this->lblMonthlyPayment->Name = L"lblMonthlyPayment";
			this->lblMonthlyPayment->Size = System::Drawing::Size(109, 15);
			this->lblMonthlyPayment->TabIndex = 9;
			this->lblMonthlyPayment->Text = L"Monthly Payment:";
			// 
			// paymentTable
			// 
			this->paymentTable->BackColor = System::Drawing::SystemColors::Window;
			this->paymentTable->Location = System::Drawing::Point(15, 132);
			this->paymentTable->Name = L"paymentTable";
			this->paymentTable->ReadOnly = true;
			this->paymentTable->ScrollBars = System::Windows::Forms::RichTextBoxScrollBars::Vertical;
			this->paymentTable->Size = System::Drawing::Size(375, 147);
			this->paymentTable->TabIndex = 10;
			this->paymentTable->Text = L"";
			// 
			// buttonreset
			// 
			this->buttonreset->BackColor = System::Drawing::Color::Silver;
			this->buttonreset->Location = System::Drawing::Point(98, 289);
			this->buttonreset->Name = L"buttonreset";
			this->buttonreset->Size = System::Drawing::Size(213, 23);
			this->buttonreset->TabIndex = 11;
			this->buttonreset->Text = L"Reset";
			this->buttonreset->UseVisualStyleBackColor = false;
			this->buttonreset->Click += gcnew System::EventHandler(this, &Form1::buttonreset_Click);
			// 
			// lblPaymentNo
			// 
			this->lblPaymentNo->AutoSize = true;
			this->lblPaymentNo->BackColor = System::Drawing::SystemColors::ActiveCaptionText;
			this->lblPaymentNo->Font = (gcnew System::Drawing::Font(L"Calibri", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->lblPaymentNo->ForeColor = System::Drawing::Color::White;
			this->lblPaymentNo->Location = System::Drawing::Point(12, 116);
			this->lblPaymentNo->Name = L"lblPaymentNo";
			this->lblPaymentNo->Size = System::Drawing::Size(65, 15);
			this->lblPaymentNo->TabIndex = 12;
			this->lblPaymentNo->Text = L"Payment #";
			// 
			// lblBalance
			// 
			this->lblBalance->AutoSize = true;
			this->lblBalance->BackColor = System::Drawing::SystemColors::ActiveCaptionText;
			this->lblBalance->Font = (gcnew System::Drawing::Font(L"Calibri", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->lblBalance->ForeColor = System::Drawing::Color::White;
			this->lblBalance->Location = System::Drawing::Point(110, 116);
			this->lblBalance->Name = L"lblBalance";
			this->lblBalance->Size = System::Drawing::Size(108, 15);
			this->lblBalance->TabIndex = 13;
			this->lblBalance->Text = L"Remaining Balance";
			// 
			// lblInterestPaid
			// 
			this->lblInterestPaid->AutoSize = true;
			this->lblInterestPaid->BackColor = System::Drawing::SystemColors::ActiveCaptionText;
			this->lblInterestPaid->Font = (gcnew System::Drawing::Font(L"Calibri", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->lblInterestPaid->ForeColor = System::Drawing::Color::White;
			this->lblInterestPaid->Location = System::Drawing::Point(254, 116);
			this->lblInterestPaid->Name = L"lblInterestPaid";
			this->lblInterestPaid->Size = System::Drawing::Size(77, 15);
			this->lblInterestPaid->TabIndex = 14;
			this->lblInterestPaid->Text = L"Interest Paid";
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->BackgroundImage = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"$this.BackgroundImage")));
			this->ClientSize = System::Drawing::Size(409, 320);
			this->Controls->Add(this->lblInterestPaid);
			this->Controls->Add(this->lblBalance);
			this->Controls->Add(this->lblPaymentNo);
			this->Controls->Add(this->buttonreset);
			this->Controls->Add(this->paymentTable);
			this->Controls->Add(this->lblMonthlyPayment);
			this->Controls->Add(this->monthlyPayment);
			this->Controls->Add(this->lblLoanAmount);
			this->Controls->Add(this->mortgageThree);
			this->Controls->Add(this->mortgageTwo);
			this->Controls->Add(this->mortgageOne);
			this->Controls->Add(this->loanAmount);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Fixed3D;
			this->MaximizeBox = false;
			this->Name = L"Form1";
			this->Text = L"McBride Financial Services";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
			// 
			// Calculates loan @ 7 year at 5.35%
			// 
private: System::Void mortgageOne_Click(System::Object^  sender, System::EventArgs^  e) {
			double vLoanAmount = Convert::ToDouble(loanAmount->Text);			// Captures value for loan amount			
	

				 double vMonthlyPayment = (vLoanAmount*((interestRate[0]/100)/12)) /			// Formula to calculate	
					 (1-1 / pow((1 + (interestRate[0]/100) / 12), loanLength[0]*12));			// mortgage payment				 
				 
				 monthlyPayment->Text = Convert::ToString(vMonthlyPayment);			// Outputs Monthly Payment 
				 monthlyPayment->Text = vMonthlyPayment.ToString("F2");				// Converts output (2 decimals)
				 

				 for (int x = 1; x <=(loanLength[0]*12); x++)		// Loop to generate monthly individual payments
				 {
					 loanBalance = vLoanAmount;										// Variables to calculate 
																					// loan balance and interest paid
					 interestPaid = ((interestRate[0] / 12) * loanBalance / 100);  
					 principlePaid = (vMonthlyPayment - interestPaid);
					 vLoanAmount = (loanBalance - principlePaid);
					 //
					 // Payment Table Output
					 //
					 paymentTable->AppendText(x + "\t" "\t" + "$" + (vLoanAmount.ToString("F2")) + " \t\t " + "$" + (interestPaid.ToString("F2"))+"\n");

				 if (loanBalance <= 0) 
				 { 
					 paymentTable->AppendText("\tRemaining balance = $0.00");  // Avoids negative number responses
				 }
				 }
			 }
			// 
			// Calculates loan @ 15 year at 5.5%  Same format as obove with formula modified for appropriate result.
			// 
private: System::Void mortgageTwo_Click(System::Object^  sender, System::EventArgs^  e) {
		    double vLoanAmount = Convert::ToDouble(loanAmount->Text);				


				 double vMonthlyPayment = (vLoanAmount*((interestRate[1]/100)/12)) /		// Array value now = 1
					 (1-1 / pow((1 + (interestRate[1]/100) / 12), loanLength[1]*12)); 				 
				 
				 monthlyPayment->Text = Convert::ToString(vMonthlyPayment);		
				 monthlyPayment->Text = vMonthlyPayment.ToString("F2");				
				 

				 for (int x = 1; x <=(loanLength[1]*12); x++)
				 {
					 loanBalance = vLoanAmount;
					 
					 interestPaid = ((interestRate[1] / 12) * loanBalance / 100);  
					 principlePaid = (vMonthlyPayment - interestPaid);
					 vLoanAmount = (loanBalance - principlePaid);

					 
					 paymentTable->AppendText(x + "\t" "\t" + "$" + (vLoanAmount.ToString("F2")) + " \t\t " + "$" + (interestPaid.ToString("F2"))+"\n");

				 if (loanBalance <= 0) 
				 { 
					 paymentTable->AppendText("\tRemaining balance = $0.00");
				 }
				 }
		 }
			// 
			// Calculates loan @ 30 year at 5.75%  Same format as obove with formula modified for appropriate result.
			// 
private: System::Void mortgageThree_Click(System::Object^  sender, System::EventArgs^  e) {
		    double vLoanAmount = Convert::ToDouble(loanAmount->Text);			


				 double vMonthlyPayment = (vLoanAmount*((interestRate[2]/100)/12)) /		// Array value now at 2
					 (1-1 / pow((1 + (interestRate[2]/100) / 12), loanLength[2]*12)); 				 
				 
				 monthlyPayment->Text = Convert::ToString(vMonthlyPayment);		
				 monthlyPayment->Text = vMonthlyPayment.ToString("F2");				
				 

				 for (int x = 1; x <=(loanLength[2]*12); x++)
				 {
					 loanBalance = vLoanAmount;
					 
					 interestPaid = ((interestRate[2] / 12) * loanBalance / 100);  
					 principlePaid = (vMonthlyPayment - interestPaid);
					 vLoanAmount = (loanBalance - principlePaid);

					 
					 paymentTable->AppendText(x + "\t" "\t" + "$" + (vLoanAmount.ToString("F2")) + " \t\t " + "$" + (interestPaid.ToString("F2"))+"\n");

				 if (loanBalance <= 0) 
				 { 
					 paymentTable->AppendText("\tRemaining balance = $0.00");
				 }
				 }
		 }
			// 
			// Reset Button to Clear Form
			// 
private: System::Void buttonreset_Click(System::Object^  sender, System::EventArgs^  e) {
			 loanAmount->Clear();
			 monthlyPayment->Clear();
			 paymentTable->Clear();
		 }
};
}








the error I am getting is undeclared indentifier.
Was This Post Helpful? 0

#11 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Variables + Windows Forms Applications

Posted 05 June 2010 - 06:24 AM

You really aren't describing your problem very clearly so I am guessing that what you are asking is about variable scope.

Following on from that guess it seems you are asking why you need to declare the variables in each function?
Is that right?

Well you pretty much have three options for using a variable in a function:
1 - Use global variables which is bad idea that you should not employ.
2 - Declare the variable in the function.
3 - Pass in the variable to the function. In your context that would seem to be best achieved by passing in an object that has the variables declared as part of the class (but I haven't looked at your code carefully so maybe that's an obviously bad idea to you - if it is then ignore it).

Is there a fourth idea that you are trying to implement that I am missing?
What us it?

If you are trying to implement one of those three ideas which one is it?
What trouble are you having with that idea?
Was This Post Helpful? 0
  • +
  • -

#12 Guest_JB*


Reputation:

Re: Variables + Windows Forms Applications

Posted 05 June 2010 - 06:29 AM

I declared the varibles in the function but and that worked, but I don't like that method because I have to continously repeat the variables. So I was attempting to declare the variables once at the beginning of the code and use the variables throughout without repeating them.
Was This Post Helpful? 0

#13 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Variables + Windows Forms Applications

Posted 05 June 2010 - 06:37 AM

Did you not understand my question?

Do you have a grasp on what "scope" means?

You keep repeating that you want to declare the variables once but you aren't suggesting any idea that includes that single declaration and also displays an understanding of what scope means.

Unless you can suggest what you want to do in a way that respects scope I am out of ideas about how to help you further.
Was This Post Helpful? 0
  • +
  • -

#14 jamiebahamas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-May 10

Re: Variables + Windows Forms Applications

Posted 05 June 2010 - 07:09 AM

Sounds like Global Variables will solve your problem. I see earlier that you were advised against them. While they are not best in most situations, they do have their place. This should solve your problem.
Was This Post Helpful? 0
  • +
  • -

#15 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Variables + Windows Forms Applications

Posted 05 June 2010 - 09:15 AM

Why are you replying to yourself, as if you were not the OP???

Closed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1