1

I have a multi project solution in VB.Net. I have a custom made form, which other forms can inherit. It is in a seperate project called "CustomForm", there are no special graphical effects, it is the same as a generic Windows Form, just handles closing differently. I have a project called "TestProject1" with a form named Form1 in it, which inherits CustomForm. In the third project "TestManager", you can add an instance of Form1 from TestProject1, and set its ownership to TestManager. I have this setup with the following code inside TestManager:

Public Shared Sub CreateForm(ByVal frm As CustomForm.CustomForm)

    frm.Owner = TestManager.TestManager

    frm.Show()
End Sub

However I get the following error: 'Form1' is a type in 'TestProject1' and cannot be used as an expression.

EDIT: More Details:

Form1 has nothing on it at the moment. Imagine TestManager as a desktop, where a form from another project is added to it. TestManager references TestProject1, and used the code: CreateForm(TestProject1.Form1) which utilizes the above method. Now form1 references and inherits CustomForm. This error is displayed the moment I enter the code, so I cannot even build the project.

TheRyan722
  • 1,029
  • 13
  • 37
  • Is that a compile time error? If so, it must be coming from somewhere else where you actually have `Form1` being used. Can you post more relevant code? – Cᴏʀʏ Jul 16 '13 at 21:05
  • Added more information, if its not enough just tell me. – TheRyan722 Jul 16 '13 at 21:11
  • You can't pass `TestProject1.Form1` to `CreateForm()`. You need to create an object of type TestProject.Form1 and pass that object instead. – Mark Lakata Jul 16 '13 at 21:17
  • 1
    This: `CreateForm(TestProject1.Form1)` probably is the issue; `CreateForm` expects an instance, so you'll have to change it to: `CreateForm(New TestProject1.Form1())` – Cᴏʀʏ Jul 16 '13 at 21:17
  • Wow, I feel stupid for overseeing this. I'll submit it as an answer. – TheRyan722 Jul 16 '13 at 21:21
  • While Form1 is certainly a type name and ought not be used to refer to the form object, it actually *is* usable as an object expression. The My.Forms namespace provides that "feature". That stopped working because you declared the Form class in another project. – Hans Passant Jul 16 '13 at 21:59

2 Answers2

4

Due to my own stupidity, I oversaw such a simple error. I did not create an object or instance of the form, which was the issue. Simply had to add 'New' to the line.

CreateForm(New TestProject1.Form1())
TheRyan722
  • 1,029
  • 13
  • 37
2

Just a little help here. An internship at my office was having this issue. The reason: he was overloading the constructor (wich was ok) but he did NOT create a default constructor.

It seems that if you want to use a class of your own without sending parameters, it's ok, but if you want to have more than one constructor and you DO NOT add the default constructor (the one without parameters) then this error will rise.

He was using VS 2010 Pro

Compa
  • 190
  • 1
  • 11