Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3969050
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:55:01+00:00 2026-05-20T03:55:01+00:00

I have implemented my own class that inherits from the SettingsProvider class. If the

  • 0

I have implemented my own class that inherits from the SettingsProvider class. If the value I am retrieving has not been set by the user, i.e. it is the first time the program starts, I attempt to grab the default value. The problem I am seeing is when the default value is retrieved from the SettingsProperty it comes back as a string. I have tried to add some code that converts the item but I am running into problems when converting System.Drawing.Colors and am receiving the error “Invalid cast from ‘System.String’ to ‘System.Drawing.Color’.”.

Here is an example of the code I am using to get the default value:

 private object GetDefaultValue(SettingsProperty setting)
  {
         if (setting.PropertyType.IsEnum)
             return Enum.Parse(setting.PropertyType, setting.DefaultValue.ToString());

        // Return the default value if it is set
        if (setting.DefaultValue != null)
           return Convert.ChangeType(setting.DefaultValue, setting.PropertyType);
        else // If there is no default value return the default object
           return Activator.CreateInstance(setting.PropertyType);
   }

How can I properly convert the default value to the correct type?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T03:55:02+00:00Added an answer on May 20, 2026 at 3:55 am

    Ok. So I found the answer. Instead of using Convert.ChangeType you need to actually create a TypeConverter instance against the settings.PropertyType. This code Works:

    private object GetDefaultValue(SettingsProperty setting)
      {
             if (setting.PropertyType.IsEnum)
                 return Enum.Parse(setting.PropertyType, setting.DefaultValue.ToString());
    
            // Return the default value if it is set
            // Return the default value if it is set
             if (setting.DefaultValue != null)
             {
                 System.ComponentModel.TypeConverter tc = System.ComponentModel.TypeDescriptor.GetConverter(setting.PropertyType);
                 return tc.ConvertFromString(setting.DefaultValue.ToString());
             }
             else // If there is no default value return the default object
             {
                 return Activator.CreateInstance(setting.PropertyType);
             }
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I have a GUI that inherits from the JFrame class and has its
I have created a BasicMembershipProvider class that inherits from MembershipProvider, when implemented, the ValidateUser
I have a parent class in rails that inherits from ActiveRecord::Base. I'm trying to
I'd like to have a BaseViewController class (that inherits from UIViewController ) for my
Symfony2 and FOS User Bundle issue... I have implemented my own login form in
I have implemented my own form field as IsEditor<LeafValueEditor<String>> that I'd like to use
So let's say I have two classes that inherit a base class that has
We have a small wrapper class that uses ResourceManager to load string resources from
Suppose I have a class that downloads a news feed from the web public
I have a class that exposes an auto implemented property Enabled Public MustInherit Class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.