only static data members can be initialized inside a ref class or value type

This is a discussion on only static data members can be initialized inside a ref class or value type within the C++ Programming forums, part of the General Programming Boards category; Hi guys, Do any of you know how to correct the error when using bool OkToClose = false;?? The error ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    40

    only static data members can be initialized inside a ref class or value type

    Hi guys,

    Do any of you know how to correct the error when using bool OkToClose = false;??

    The error I have got is:

    Error: error C3845: 'MyApplication::Form1::OkToClose': only static data members can be initialized inside a ref class or value type


    Code:
    #pragma once
    
    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    
    namespace Form1 {
    
    	/// <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;
    			}
    		}
    	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->SuspendLayout();
    			// 
    			// Form1
    			// 
    			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
    			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    			this->ClientSize = System::Drawing::Size(292, 266);
    			this->Controls->Add(this->comboBox1);
    			this->Controls->Add(this->button1);
    			this->Name = L"Form1";
    			this->Text = L"Form1";
    			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
    			this->ResumeLayout(false);
    
    		}
    #pragma endregion
    
    bool OkToClose = false;
    };
    }
    Any advice would be much appreciate.

    Thanks,
    Mark
    Last edited by mark103; 04-29-2011 at 07:15 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,529
    The message is self-explanatory. Your code declares OkToClose as a non-static member of the class Form1. Such class members cannot be initialised at the point they are declared.

    If you want a solution, it would have helped if you had actually specified what your programming language is. If you force people to guess on such things, the likelihood of getting useful advice goes down. Your code looks ostensibly like C++, but will never get past a vanilla C++ compiler. From that, I suspect your code is either C# or some form of managed C++, so I will only give you a generic hint of the solution - you'll have to bridge the gaps.....

    If the member has to be static (i.e. only one instance of OkToClose for all instances of Form1) then declare it as static. If it is not static (which is the case in your code) take of the "= false" initialisation and do an appropriate initialisation in the constructor(s) of Form1.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static members in class
    By prongs_386 in forum C++ Programming
    Replies: 11
    Last Post: 01-03-2007, 06:36 AM
  2. static data members in structure
    By bhagwat_maimt in forum C++ Programming
    Replies: 9
    Last Post: 11-06-2006, 11:47 AM
  3. static data members
    By ichijoji in forum C++ Programming
    Replies: 2
    Last Post: 07-05-2006, 06:18 PM
  4. static class members
    By JaWiB in forum C++ Programming
    Replies: 10
    Last Post: 10-31-2003, 07:48 PM
  5. static and non-static data members
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-16-2002, 11:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21