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 805669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:07:05+00:00 2026-05-15T00:07:05+00:00

Using the following code: Function GetSetting(Of T)(ByVal SettingName As String, ByRef DefaultVal As T)

  • 0

Using the following code:

Function GetSetting(Of T)(ByVal SettingName As String, ByRef DefaultVal As T) As T
    Return If(Configuration.ContainsKey(SettingName), CType(Configuration(SettingName), T), DefaultVal)
End Function

Yields the following error:

Value of type 'String' cannot be converted to 'T'.

Any way I could specify that in all cases, the conversion will indeed be possible (I’m basically getting integers, booleans, doubles and strings).

Edit: There seem to be three solutions now:

  • Using the `ValueAs` function provided by AMissico
  • Casting to an `object`, then to `T`, with a check for null values
  • Using a `DirectCast` over Convert.ChangeType

Which would you suggest?

Edit 2:
Would this code work?

Function GetSetting(Of T)(ByVal SettingName As String, Optional ByRef DefaultVal As T = Nothing) As T
    Return If(Configuration.ContainsKey(SettingName), ConvertTo(Of T)(Configuration(SettingName)), DefaultVal)
End Function

Function ConvertTo(Of T)(ByVal Str As String) As T
    Return If(Str Is Nothing Or Str = "", Nothing, CType(CObj(Str), T))
End Function

Edit 3: [AMJ] Working Code

Function GetSetting(Of T)(ByVal SettingName As String) As T
    Return GetSetting(Of T)(SettingName, Nothing)
End Function
Function GetSetting(Of T)(ByVal SettingName As String, ByVal DefaultVal As T) As T
    Dim sValue As String = Configuration(SettingName)
    If Len(sValue) = 0 Then
        Return DefaultVal
    Else
        Return CType(CObj(sValue), T)
    End If
End Function

Quick Test Method

Public Sub DoIt()

    Me.Configuration.Add("KeyN", Nothing)
    Me.Configuration.Add("KeyE", String.Empty) '""
    Me.Configuration.Add("Key1", "99")
    Me.Configuration.Add("Key2", "1/1/2000")
    Me.Configuration.Add("Key3", "True")
    Me.Configuration.Add("Key4", "0")

    Dim o As Object 'using object in order to see what type is returned by methods

    o = Value(Of Integer)("KeyN", 10) '10
    o = Value(Of Integer)("KeyE", 10) '10
    o = Value(Of Integer)("Key1", 10) '99

    o = Value(Of Date)("KeyN", #11/11/2010#)
    o = Value(Of Date)("KeyE", #11/11/2010#)
    o = Value(Of Date)("Key2", #11/11/2010#)

    o = GetSetting(Of Integer)("KeyN", 10) '10
    o = GetSetting(Of Integer)("KeyE", 10) '10
    o = GetSetting(Of Integer)("Key1", 10) '99

    o = GetSetting(Of Date)("KeyN", #11/11/2010#)
    o = GetSetting(Of Date)("KeyE", #11/11/2010#)
    o = GetSetting(Of Date)("Key2", #11/11/2010#)

    Stop
End Sub
  • 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-15T00:07:05+00:00Added an answer on May 15, 2026 at 12:07 am

    The Value(Of T) and ValueAs methods support nullable-types. I used Microsoft .NET 2.0 source code as a reference.

    This is well-tested and production ready code.

    There is no error handling in these “library” functions. It is the responsibility of the caller to handle any conversion errors that occur. The only conversion errors generated are obvious errors, such as trying to convert the string “abc” to Integer.


    Public Sub DoIt()
        Dim o As Object
        o = Value(Of Integer)("foo", 10)
        o = Value(Of DateTime)("xxx", #1/1/2000#)
        o = Value(Of Boolean?)("nop", True)
        Stop
    End Sub
    
    Public Function GatherTag(ByVal tag As String) As String
        If tag = "foo" Then
            Return "99"
        Else
            Return String.Empty
        End If
    End Function
    
    ''' <summary>
    ''' Provides strongly-typed access to the tag values. The method also supports nullable types.
    ''' </summary>
    ''' <typeparam name="T">A generic parameter that specifies the return type.</typeparam>
    ''' <param name="tag">The ExifTool Tag Name,</param>
    ''' <returns>The value, of type T, of the tag.</returns>
    Public Function Value(Of T)(ByVal tag As String, ByVal defaultValue As T) As T
        Return DirectCast(ValueAs(GetType(T), tag, defaultValue), T)
    End Function
    
    ''' <summary>
    ''' Returns the tag's value as the specified type. The method also supports nullable types.
    ''' </summary>
    ''' <param name="type">The type to return the tag value as.</param>
    ''' <param name="tag">The ExifTool Tag Name,</param>
    ''' <returns>The value of the tag as the type requested.</returns>
    Public Function ValueAs(ByVal type As System.Type, ByVal tag As String, ByVal defaultValue As Object) As Object
        Dim oResult As Object = Nothing
    
        Dim oTag As String = GatherTag(tag)
    
        If Len(oTag) = 0 Then
    
            'use specified default value
    
            oResult = defaultValue
    
        Else
    
            'is requested type a generic type?
    
            If type.IsGenericType AndAlso type.GetGenericTypeDefinition Is GetType(Nullable(Of )) Then
    
                Dim oUnderlyingType As Type = Nullable.GetUnderlyingType(type)
    
                Dim oConstructed As Type = type.GetGenericTypeDefinition.MakeGenericType(oUnderlyingType)
    
                Dim oValue As Object
    
                oValue = System.Convert.ChangeType(oTag, oUnderlyingType)
    
                If oValue IsNot Nothing Then
                    oResult = Activator.CreateInstance(oConstructed, oValue)
                End If
    
            Else
    
                'non-generic type
    
                oResult = System.Convert.ChangeType(oTag, type)
    
            End If
    
        End If
    
        Return oResult
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 449k
  • Answers 449k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you want to interpret $replace, you should not use… May 15, 2026 at 8:06 pm
  • Editorial Team
    Editorial Team added an answer first download a blank cursor, you can get it from… May 15, 2026 at 8:06 pm
  • Editorial Team
    Editorial Team added an answer Try this: webview.backgroundColor = [UIColor scrollViewTexturedBackgroundColor]; May 15, 2026 at 8:06 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.