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

  1. #1
    Registered User
    Join Date
    Apr 2011

    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

    #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
    			//TODO: Add the constructor code here
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    			if (components)
    				delete components;
    		/// <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)
    			// Form1
    			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
    			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    			this->ClientSize = System::Drawing::Size(292, 266);
    			this->Name = L"Form1";
    			this->Text = L"Form1";
    			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
    #pragma endregion
    bool OkToClose = false;
    Any advice would be much appreciate.

    Last edited by mark103; 04-29-2011 at 06:15 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    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 or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, 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, 05: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, 10:06 AM