add custom type in settings.settings
Asked Answered
R

4

13

I would like to use configuration file .settings to save this struct:

struct sR22Protocole
{
    Int32 inputkey;
    Int32 outputkey;
    Int32 voltage;
    Int32 Ohm;
    Int32 Correction;
};

In the settings designer, I can add different type but it doesn't show my struct in the browse section. Is there any way that the designer has access to my struct? If no, Is there any way to add it programmaticaly?

Riorsson answered 23/9, 2013 at 19:47 Comment(1)
Does this answer your question? How do I save/serialize a custom class to the settings file? Seems to be a duplicate question.Burgonet
C
10

I believe the class (or struct?) must be able to be serialized to use in the settings file. I followed this blog post when I did this for a default object in an application:

http://www.blackwasp.co.uk/CustomAppSettings.aspx

Colicroot answered 23/9, 2013 at 20:1 Comment(0)
R
18

Your type must have a System.Configuration.SettingsSerializeAsAttribute attribute. An enum parameter of type System.Configuration.SettingsSerializeAs specifies how the value will be serialized, the possible values are:

  • String
  • Xml
  • Binary
  • ProviderSpecific

Since this attribute can only be applied to class types, your own type has to be a class.

Secondly, the type must have a parameterless constructor. This is because a default instance of the type of the setting must be able to be assinged.

If you have just declared your class, the designer won't accept the type unless you have built your solution.

Redeemer answered 14/9, 2016 at 15:51 Comment(0)
C
10

I believe the class (or struct?) must be able to be serialized to use in the settings file. I followed this blog post when I did this for a default object in an application:

http://www.blackwasp.co.uk/CustomAppSettings.aspx

Colicroot answered 23/9, 2013 at 20:1 Comment(0)
L
3

Just go into the browse section and type your struct, class or enum name while prefixing it by the namespace of your type. Then, it would also be added in the dropdown for your next uses.

In your example: YourTypeNamespace.sR22Protocole

Lesseps answered 2/5, 2016 at 13:23 Comment(5)
Can you explain a bit more?Canebrake
What version of Visual Studio are you using? I just tried this on VS2013 Express with an enum and it didn't work. (It gives a "Type 'X' is not defined." error.)Outpost
It worked with VS 2010 Express and a custom enum type.Kinsley
If you are using a new defined, let's say Enum, you should rebuild once to make the type "known". I just tried to add a new Enum as Type. It was unknown and I thought I did something wrong. But I just had to recompile...Milks
@jmbpiano: At that time, I was using VS 2015 Community.Lesseps
U
0

It is actually not needed to add attributes etc. If you make sure that the class is serializable like Parameterless constructor and public properties, it will still work.

Underexpose answered 1/9, 2022 at 22:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.