Tomáš Zato
Tomáš Zato

Reputation: 53193

"Type is not defined" when I want to add my custom class as settings type

I want to add this class as setting's type:

using System.Collections.Generic;
using System.Configuration;

namespace MY_PROJECT.SUB_PROJECT
{
    [SettingsSerializeAs(SettingsSerializeAs.Xml)]
    public class Configs: List<ConfigData>
    {
        Configs(int capacity): base(capacity) { }

        public string GroupName { get; set; }
    }
}

So what I did:

  1. Select Browse... in the type dropbox:
    enter image description here
  2. I cannot see the MY_PROJECT namespace anywhere: enter image description here
  3. So I typed the full type manually: enter image description here
  4. The result is an error:

    Type 'MY_PROJECT.SUB_PROJECT.Configs' is not defined.
    

    enter image description here

I also tried SUB_PROJECT.Configs and Configs alone. Nothing helped. Why does my class not show in the browser?

Upvotes: 2

Views: 981

Answers (3)

spinjector
spinjector

Reputation: 3515

I had this problem as well. I was doing exactly the same: creating a custom class to use in Application Settings. In my case, I followed the steps outlined in this very informative article: http://www.blackwasp.co.uk/CustomAppSettings.aspx

I should note the article is written for C#, and I painstakingly converted it to VB until it worked. I had to solve the Type...is not defined error the hard way: relentlessly experimenting until I got it to work.

I will describe my first solution, one which was not mentioned in the article, probably because it's for C# instead of VB, and that is: put the custom class or classes each in their own .vb files. For instance: Employee.vb and Room.vb. This is the only way I could make it work perfectly with no errors. After doing this and rebuilding the solution, I was then able to add my custom class as an Application Setting, but of course only by manually typing the full name TestProject.Employee in the Select a Type dialog.

However, following the article I linked above, if I put all the class definitions in the Module1.vb file with Sub Main(), the Select a Type dialog cannot find them, and I receive the Type...is not defined error.

And the cause of this error seems to be shortcomings in the code & design of the Applications Settings system and Settings page of the Project Properties dialog. I say this because of the solution I found: I hacked my classes into the settings the hard way.

What I mean by that is I initially created the setting with the name DefaultEmployee and type of String. Then I used the Find In Files dialog to find all instances of DefaultEmployee and replaced the appropriate instances of String with TestProject.Employee.

The files I made replacements in are: App.config, Settings.Designer.vb, and Settings.settings.

And it worked..! Sort of. I should say the code ran fine and it did what was expected. But...the Application Settings dialog didn't like it. After I made the changes, there are various errors from the Project Properties/Settings system every time I opened it. But as I said, it still works.

Thus...my only conclusion is the coding of the Settings system is not designed to handle this situation, and if you wish to have the most reliable & error-free experience, it's best to put each of the custom classes in their own .vb class file.

On the other hand, if you wish to become very adventurous, you could create your own Applications Settings system, as the author of this article did. I have not read all of this yet, but scanning through it seems very interesting: https://weblog.west-wind.com/posts/2012/dec/28/building-a-better-net-application-configuration-class-revisited

Upvotes: 0

Osama
Osama

Reputation: 383

I just had this issue and it was due to an Inconsistent accessibility error. Make sure that any 'required' type/field is globally accessible (public?). For the OP's case, making the constructor public solves the issue:

[SettingsSerializeAs(SettingsSerializeAs.Xml)]
public class Configs: List<ConfigData>
{ 
    public Configs(int capacity): base(capacity) { }
  //  ^^
    public string GroupName { get; set; }
}

In my case this was the problem:

internal struct NativeType
{
    //...
}

[SettingsSerializeAs(SettingsSerializeAs.Xml)]
public class NativeTypeWrapper
{
    public NativeType type; // This will not work because NativeType 
                            // is less accessible than NativeTypeWrapper...
}

Upvotes: 0

Keith
Keith

Reputation: 98

In order to pull something in as a reference you need to have it compiled as a dll file. In Visual Studio they refer to this as a "Library" which is really just a class without a main function. Other option is to just leave it in the same namespace and pull the class into whatever else your working on.

Upvotes: 0

Related Questions