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

  • Home
  • SEARCH
  • 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 7676601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:14:12+00:00 2026-05-31T17:14:12+00:00

I did create a XDateTime class that is able to handle inaccurate date and

  • 0

I did create a XDateTime class that is able to handle inaccurate date and time.

This class has all the CType operators required to convert to and from a string and it has been fully tested during the last months.

    Public Shared Widening Operator CType(ByVal xDateTime As FrameworkBL.XDateTime) As String
    Dim retrunValue As String = Nothing

    If xDateTime Is Nothing Then
        retrunValue = Nothing
    Else
        retrunValue = xDateTime.StringValue
    End If

    Return retrunValue
End Operator

Public Shared Narrowing Operator CType(ByVal value As String) As FrameworkBL.XDateTime
    Dim returnValue As FrameworkBL.XDateTime = Nothing


    If String.IsNullOrEmpty(value) Then
        returnValue = Nothing
    Else
        returnValue = New FrameworkBL.XDateTime(value)
    End If

    Return returnValue

End Operator

However, when a ByRef object parameter return a string, my CType operators seems to be ignored and a cast Exception is raised.

    Private Sub Test()
    Dim myXDateTime As FrameworkBL.XDateTime

    myXDateTime = "200101010000007" 'Ok
    Me.Temp1(myXDateTime) 'Ok
    Me.Temp2(myXDateTime) 'Ok
    Me.Temp3(myXDateTime) 'Unable to cast object of type 'System.String' to type 'FrameworkBL.XDateTime'

End Sub

Private Sub Temp1(ByRef myObject As String)
    myObject = "200201010000007"
End Sub

Private Sub Temp2(ByRef myObject As XDateTime)
    myObject = "200301010000007"
End Sub

Private Sub Temp3(ByRef myObject As Object)
    myObject = "200401010000007"
End Sub

This kind of problem is documented by Microsoft but I couldn’t find a working solution to fix this problem. Am I in a dead end or is there an option that would allow me to keep my ByRef Object parameter??

  • 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-31T17:14:13+00:00Added an answer on May 31, 2026 at 5:14 pm

    The solution is not to write this code. In fact, your code shouldn’t even compile, and never mind the ByRef. You simply cannot assign a string to an Object, and rightly so. You should enable Option Strict On in your project settings.

    This is the correct and expected behaviour.

    Apart from that, your operator implementations can (and should) be drastically shortened by taking advantage of the VB If operator. This makes the redundant initialisation and assignments go away:

    Public Shared Widening Operator CType(ByVal xDateTime As XDateTime) As String
        Return If(xDateTime Is Nothing, Nothing, xDateTime.StringValue)
    End Operator
    

    Same for the other way round. That said, I don’t think conversion operators should ever work on Nothing values. This just makes Nothing creep into code where it shouldn’t.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code which I did not originally create that uses _beginthreadex and
I did this: git branch --track stats_page to create a separate branch to try
How can I create tabs in matlab gui? I did something like this a
I'm working on building a WebForm that has a set of Date of Birth
I did this example using the pluralsight video but the date picker is just
Using this answer as a guide, I set out to create a select_year that
I did heroku create and pushed a working rails app to the server. Everything
I create a new Button object but did not specify the command option upon
How can I create a const boost matrix? The following did not work: const
I am needing to create a web service, so the first thing I did

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.