简体   繁体   中英

cant create a form instance in another class file of the same project in c#

I have created a new form in the existing class library project. The form uses the same name space. I have a existing class file. I can not create the instance of the new form.

Form1 frm=new Form1();

The above code is not working in the existing class, but If I add a new class then It is working fine.

I need to access the form in the existing class. Please help me to access this form.

Thanks in Advance

正如@ reza-aghaei已经指出的那样,您的错误可能是以下事实,您忘记了在语句末尾添加():

Form1 frm=new Form1();

Usually Visual Studio is smart enough to suggest what could be Wrong - perhaps its ReSharper that tells about mismatch in visibility of types in different parts of projects or other projects - as in your case the form is in a class library.

Just check that your form is not internal to the class library. you would see it on the Form1 class definition. if it is internal or private make it public.

if for some reason you can not make it public atleast make it visible to the consuming assembly using the InternalsVisibleToAttribute Class : https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute(v=vs.110).aspx

Thank you Guys.

I have added the reference in the class library again as

using "Project folder Name";

This potential fix is shown by VS2015 Intellisense.

This is strange behavior of code, as the code is working fine in the new classes with out adding the any reference. But in the existing class it working after adding the reference only.

Thank you every one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM