none
Complier cannot distinguish XElement from String... RRS feed

  • Question

  • Grrr... this is really annoying.  Why can't the compiler tell the difference between a constructor with a single XElement parameter and one with a single String parameter.

    I have a class that has the following constructors...

    Public Class Widget
    
        Sub New(WidgetName As String)
          ' stuff happens
        End Sub
    
        Sub New(WidgetXml As XElement)
          ' stuff happens
        End Sub
    
    End Class
    
    

    The compiler always calls the String constructor even when it's passed an XElement.

    Is this a Visual Studio 2008 bug, or just a dumb compiler?


    WCF should be renamed WTF

    Saturday, June 16, 2012 7:33 PM

Answers

  • Hi, 

    Should not be, Can you show us code that you are using?


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    Sunday, June 17, 2012 10:12 AM
  • Do you pass a XElement or the contents of an Xelement, the first is an object from the type XElement, the second an object from the type string.


    Success
    Cor

    Monday, June 18, 2012 5:44 AM
  • I can't reproduce your issue. My below code calls the right constructor.

    Imports System.Xml.Linq
    
    Module MainModule
    
        Sub Main()
            Dim xe As New XElement(XName.Get("SomeNode"))
            Dim widget As New Widget(xe)
            Console.ReadLine()
        End Sub
    
    End Module
    
    Public Class Widget
    
        Sub New(WidgetName As String)
            Console.WriteLine("String")
    
        End Sub
    
        Sub New(WidgetXml As XElement)
            Console.WriteLine("XElement")
        End Sub
    
    End Class

    Check the same code at your end.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Sunday, June 17, 2012 5:29 AM

All replies

  • I can't reproduce your issue. My below code calls the right constructor.

    Imports System.Xml.Linq
    
    Module MainModule
    
        Sub Main()
            Dim xe As New XElement(XName.Get("SomeNode"))
            Dim widget As New Widget(xe)
            Console.ReadLine()
        End Sub
    
    End Module
    
    Public Class Widget
    
        Sub New(WidgetName As String)
            Console.WriteLine("String")
    
        End Sub
    
        Sub New(WidgetXml As XElement)
            Console.WriteLine("XElement")
        End Sub
    
    End Class

    Check the same code at your end.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Sunday, June 17, 2012 5:29 AM
  • Hi, 

    Should not be, Can you show us code that you are using?


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    Sunday, June 17, 2012 10:12 AM
  • Do you pass a XElement or the contents of an Xelement, the first is an object from the type XElement, the second an object from the type string.


    Success
    Cor

    Monday, June 18, 2012 5:44 AM