When i put a string^ object into a String::Concat object and hit compile, VS 2005 Pro .NET(WinForms") gave me this error:
------ Build started: Project: 7, Configuration: Debug Win32 ------
Compiling...
7.cpp
c:\documents and settings\jonathan baldwin\my documents\mycpp\projec\7\7\Form1.h(146) : error C2065: 'str1' : undeclared identifier
Build log was saved at "file://c:\Documents and Settings\Jonathan Baldwin\My Documents\MyCPP\PROJEC\7\7\Debug\BuildLog.htm"
7 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Here is my source code: (I know it's messy)
#pragma
once#include
<iostream>namespace
My7 {using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace std;
/// <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();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::CheckBox^ checkBox1;
private: System::Windows::Forms::TextBox^ str1input;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
protected:
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->checkBox1 = (gcnew System::Windows::Forms::CheckBox());
this->str1input = (gcnew System::Windows::Forms::TextBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// checkBox1
//
this->checkBox1->AutoSize = true;
this->checkBox1->Location = System::Drawing::Point(32, 14);
this->checkBox1->Margin = System::Windows::Forms::Padding(4, 5, 4, 5);
this->checkBox1->Name = L"checkBox1";
this->checkBox1->Size = System::Drawing::Size(180, 24);
this->checkBox1->TabIndex = 0;
this->checkBox1->Text = L"See if Explorer Exists";
this->checkBox1->UseVisualStyleBackColor = true;
this->checkBox1->Click += gcnew System::EventHandler(this, &Form1::checkBox1_Click);
//
// str1input
//
this->str1input->Location = System::Drawing::Point(32, 104);
this->str1input->Name = L"str1input";
this->str1input->Size = System::Drawing::Size(158, 26);
this->str1input->TabIndex = 1;
this->str1input->WordWrap = false;
//
// button1
//
this->button1->Location = System::Drawing::Point(196, 106);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(125, 24);
this->button1->TabIndex = 2;
this->button1->Text = L"Submit";
this->button1->UseCompatibleTextRendering = true;
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(35, 146);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(154, 24);
this->button2->TabIndex = 3;
this->button2->Text = L"OK";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(9, 20);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(438, 409);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Controls->Add(this->str1input);
this->Controls->Add(this->checkBox1);
this->Margin = System::Windows::Forms::Padding(4, 5, 4, 5);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma
endregionprivate: System::Void checkBox1_Click(System::Object^ sender, System::EventArgs^ e) {
if(System::IO::File::Exists("C:/Windows/Explorer.EXE"))
{
// command
MessageBox::Show(String::Concat("'C:/windows/explorer.exe'\nExists"));
}
}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
String^ str1 = "str1default";
;}
private
: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {String^ str1=str1input->Text
;}
private
: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {/*line 146-Source of Error*/ MessageBox::Show(String::Concat("You Typed:", str1))
;}
};
}
Help Me Please
FLAMETHROWER

error C2065 System::String^ PROBLEM HELP!!
aagar_2003
I don't know if you were referring to this problem when you talked about references but I just want to say that the problem you had, had nothing to do with references. The problem, as I mentioned in my other post, had to do with a variable's scope, i.e. the "space where the variable exists".
Microsoft has a lot of articles/tutorial/reference pages but sometimes it can be a bit hard to find. When you have problems with c++, just "ask" google, you'll find an answer to almost everything, or you can ask here :)
Laserbeak43
Thanks
But if MS could explain MSDN references better (so that c++ users that use it as a better c for writing command console apps could migrate easier) a lot of costly forum traffic could have been avoided. The awnser was in there, but their syntax explanation sucked the big one.
sorry for the rant:)
Flame Thrower
BostonZZZ
the problem is exactly that, the compiler doesn't know str1 at that place. This happens because the other variables named str1 that you declared are only available in the scope of those methods, i.e. the compiler only knows about them inside those methods. To fix, try declaring the variable as a member of the class just like the components.
public ref class Form1 : public System::Windows::Forms::Form
{
private: String^ str1;
public:
Form1(void)
// ........private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
str1 = "str1default";
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
str1=str1input->Text;
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
MessageBox::Show(String::Concat("You Typed:", str1));
}
};
sparrow2006