locked
Extending Class Error. RRS feed

  • Question

  • User1115106291 posted

    Hi Folks,

    I’m extending a class for the very first time!
    It’s exiting, but I’m running into some difficulties….

    I’m trying to extend the ImageButton control to hold descriptive information about the Image, such as a name property and a price property.

    I’ve built the extended control, but when I run it and attempt to retrieve the price property I get an error
    Input string was not in a correct format. “ on the “Get” of the Price property…

    Exception Details: System.FormatException: Input string was not in a correct format.

    Source Error: <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

    <o:p> </o:p>

    Line 48: <o:p></o:p>Line 49:     Public Property Price() As Single<o:p></o:p>Line 50:         Get<o:p></o:p>Line 51:             Price = PriceValue<o:p></o:p>Line 52:         End Get<o:p></o:p>
    <o:p> </o:p>

    This is the property in the class:

    Private PriceValue As Single<o:p></o:p><o:p> </o:p>    Public Property Price() As Single<o:p></o:p>        Get<o:p></o:p>            Price = PriceValue<o:p></o:p>        End Get<o:p></o:p>        Set(ByVal Price As Single)<o:p></o:p>            PriceValue = Price<o:p></o:p>        End Set<o:p></o:p>    End Property

    <o:p></o:p>

    I don’t understand why this error is caused..any idea?

    Regards,
    - Joel


     

    Wednesday, January 21, 2009 4:42 PM

Answers

  • User955742345 posted
    Thanks for your continued help.

    The problem happen during a postback :

    For the moment, what I’m trying to accomplish, for testing reasons, is to display my new propertie values in a label when the extended ImageButton is Clicked.

    This is the .aspx code for my custom ImageButton Control:

    <cc1:MenuItem2 ID="MenuItem1" runat="server" ImageUrl="karma.png" Name="Karma" NameFR="KarmaFR" Price="9.99" />


    This is the codebehind vb code:

    Protected Sub MenuItem1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles MenuItem1.Click

    Label1.Text = MenuItem1.Name + MenuItem1.NameFR + "" + MenuItem1.Price.ToString

          

    End Sub

    If I try to simply display only the“MenuItem1.Name + MenuItem1.FR”  the code runs without a problem...

    This code works for me.  I even tried turning on Option Strict, in case that was causing a problem.  I'll post the entire code I used:

     Extended ImageButton (exactly the same as yours):

    Imports System
    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Text
    Imports System.Web
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    
    
    
    Namespace TestControls
    
    
    
    
        Public Class MenuItem2
            Inherits System.Web.UI.WebControls.ImageButton
    
    #Region "Properties"
    
            Public Property Name() As String
                Get
                    Dim s As String = Convert.ToString(ViewState("Name"))
                    If s Is Nothing Or s = "" Then
                        Return String.Empty
                    Else
    
                        Return s
                    End If
                End Get
                Set(ByVal value As String)
                    ViewState("Name") = value
                End Set
    
            End Property
    
            Public Property NameFR() As String
                Get
                    Dim s As String = Convert.ToString(ViewState("NameFR"))
                    If s Is Nothing Or s = "" Then
                        Return String.Empty
                    Else
    
                        Return s
                    End If
                End Get
                Set(ByVal value As String)
                    ViewState("NameFR") = value
                End Set
    
            End Property
    
            Private PriceValue As Single
    
            Public Property Price() As Single
                Get
                    Dim vPrice As Single = PriceValue
                    Return vPrice
                End Get
                Set(ByVal Price As Single)
                    PriceValue = Price
                End Set
    
            End Property
    
    
    #End Region
    
    
    
        End Class
    
    End Namespace

     

    My aspx page, called TestCustomImageButton.aspx:

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="TestCustomImageButton.aspx.vb" Inherits="TestCustomImageButton" %>
    <%@ Register TagPrefix="custom" Namespace="TestControls" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <custom:MenuItem2  ID="MenuItem1" runat="server" ImageUrl="karma.png" Name="Karma" NameFR="KarmaFR" Price="9.99" /><br />
            <asp:Label ID="Label1" runat="server" Text="" />
            
        </div>
        </form>
    </body>
    </html>
    

     

    Code-behind:

    Option Explicit On
    Option Strict On
    
    Partial Class TestCustomImageButton
        Inherits System.Web.UI.Page
    
        Protected Sub MenuItem1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles MenuItem1.Click
            Label1.Text = MenuItem1.Name + MenuItem1.NameFR + "" + MenuItem1.Price.ToString
        End Sub
    End Class
    

     

    Note: this was done in 3.5 Framework, also tested in 2.0

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 23, 2009 4:34 PM

All replies

  • User955742345 posted

    It's good practice to end the old vb ways.  Don't assign values to a method or property name!  Use Return instead.

     

    Public Property Price() As Single
      Get
        Return PriceValue
      End Get
      ' ... set code
    End Property
     
    Wednesday, January 21, 2009 5:00 PM
  • User1115106291 posted

    Thanks for your reply!

    I tried it with your changed in code (using Return)...
    but I still get the same error...
    I do have other properties on the page using Strings and they work fine...

    I think the problem comes from the use of the 'Single' datatype for the Price property...

    Regarding the method I used (old vb ways!) I took the example straight out of MSDN...?!  - http://msdn.microsoft.com/en-us/library/8yx6f707.aspx
    Is the example outdated?

    Thanks for any insight,
    - Joel

    Wednesday, January 21, 2009 5:15 PM
  • User955742345 posted

    Yes, I do consider that the 'old' way, because you don't see any other OOP languages that do property and method value assignments like that.  And with the advancement of VB into its .NET versions, it has fully embraced OOP standards.  It just doesn't enforce those standards.

     

    When does the problem occur?  During a postback read of the property?  Or during a load, postback or non postback?  Post all of your relevant code, if possible. (VB & aspx, if used).  It seems more like an issue of retrieval.

    Thursday, January 22, 2009 11:10 AM
  • User1115106291 posted
    Thanks for your continued help.

    The problem happen during a postback :

    For the moment, what I’m trying to accomplish, for testing reasons, is to display my new propertie values in a label when the extended ImageButton is Clicked.

    This is the .aspx code for my custom ImageButton Control:

    <cc1:MenuItem2 ID="MenuItem1" runat="server" ImageUrl="karma.png" Name="Karma" NameFR="KarmaFR" Price="9.99" />


    This is the codebehind vb code:

    Protected Sub MenuItem1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles MenuItem1.Click

    Label1.Text = MenuItem1.Name + MenuItem1.NameFR + "" + MenuItem1.Price.ToString

          

    End Sub

    If I try to simply display only the“MenuItem1.Name + MenuItem1.FR”  the code runs without a problem...


    Also as this is the code for the my extended ImageButton Class (The Price Property comes last..):

    <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> 
    Imports System
    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Text
    Imports System.Web
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    
    
    Public Class MenuItem2
        Inherits System.Web.UI.WebControls.ImageButton
    
    #Region "Properties"
    
        Public Property Name() As String
            Get
                Dim s As String = Convert.ToString(ViewState("Name"))
                If s Is Nothing Or s = "" Then
                    Return String.Empty
                Else
    
                    Return s
                End If
            End Get
            Set(ByVal value As String)
                ViewState("Name") = value
            End Set
    
        End Property
    
        Public Property NameFR() As String
            Get
                Dim s As String = Convert.ToString(ViewState("NameFR"))
                If s Is Nothing Or s = "" Then
                    Return String.Empty
                Else
    
                    Return s
                End If
            End Get
            Set(ByVal value As String)
                ViewState("NameFR") = value
            End Set
    
        End Property
    
        Private PriceValue As Single
    
        Public Property Price() As Single
            Get
                Dim vPrice As Single = PriceValue
                Return vPrice
            End Get
            Set(ByVal Price As Single)
                PriceValue = Price
            End Set
    
        End Property
    
    
    #End Region
    
    
    
    End Class
    
    
    
      </o:p>
    <o:p> </o:p><o:p> </o:p><o:p> </o:p><o:p> </o:p>

     

    Friday, January 23, 2009 9:17 AM
  • User1868789274 posted

    have to tried casting the string to the Type Single?:
    DirectCast(Price, Single)

    Friday, January 23, 2009 3:24 PM
  • User955742345 posted
    Thanks for your continued help.

    The problem happen during a postback :

    For the moment, what I’m trying to accomplish, for testing reasons, is to display my new propertie values in a label when the extended ImageButton is Clicked.

    This is the .aspx code for my custom ImageButton Control:

    <cc1:MenuItem2 ID="MenuItem1" runat="server" ImageUrl="karma.png" Name="Karma" NameFR="KarmaFR" Price="9.99" />


    This is the codebehind vb code:

    Protected Sub MenuItem1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles MenuItem1.Click

    Label1.Text = MenuItem1.Name + MenuItem1.NameFR + "" + MenuItem1.Price.ToString

          

    End Sub

    If I try to simply display only the“MenuItem1.Name + MenuItem1.FR”  the code runs without a problem...

    This code works for me.  I even tried turning on Option Strict, in case that was causing a problem.  I'll post the entire code I used:

     Extended ImageButton (exactly the same as yours):

    Imports System
    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Text
    Imports System.Web
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    
    
    
    Namespace TestControls
    
    
    
    
        Public Class MenuItem2
            Inherits System.Web.UI.WebControls.ImageButton
    
    #Region "Properties"
    
            Public Property Name() As String
                Get
                    Dim s As String = Convert.ToString(ViewState("Name"))
                    If s Is Nothing Or s = "" Then
                        Return String.Empty
                    Else
    
                        Return s
                    End If
                End Get
                Set(ByVal value As String)
                    ViewState("Name") = value
                End Set
    
            End Property
    
            Public Property NameFR() As String
                Get
                    Dim s As String = Convert.ToString(ViewState("NameFR"))
                    If s Is Nothing Or s = "" Then
                        Return String.Empty
                    Else
    
                        Return s
                    End If
                End Get
                Set(ByVal value As String)
                    ViewState("NameFR") = value
                End Set
    
            End Property
    
            Private PriceValue As Single
    
            Public Property Price() As Single
                Get
                    Dim vPrice As Single = PriceValue
                    Return vPrice
                End Get
                Set(ByVal Price As Single)
                    PriceValue = Price
                End Set
    
            End Property
    
    
    #End Region
    
    
    
        End Class
    
    End Namespace

     

    My aspx page, called TestCustomImageButton.aspx:

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="TestCustomImageButton.aspx.vb" Inherits="TestCustomImageButton" %>
    <%@ Register TagPrefix="custom" Namespace="TestControls" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <custom:MenuItem2  ID="MenuItem1" runat="server" ImageUrl="karma.png" Name="Karma" NameFR="KarmaFR" Price="9.99" /><br />
            <asp:Label ID="Label1" runat="server" Text="" />
            
        </div>
        </form>
    </body>
    </html>
    

     

    Code-behind:

    Option Explicit On
    Option Strict On
    
    Partial Class TestCustomImageButton
        Inherits System.Web.UI.Page
    
        Protected Sub MenuItem1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles MenuItem1.Click
            Label1.Text = MenuItem1.Name + MenuItem1.NameFR + "" + MenuItem1.Price.ToString
        End Sub
    End Class
    

     

    Note: this was done in 3.5 Framework, also tested in 2.0

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 23, 2009 4:34 PM
  • User1115106291 posted

    Thanks for the support!

    I copied the code you used above...which is supposedly the same as I had before..and it worked!.
    So I guess there were some Gremlins in my computer...

    I now ran into another problem with this custom Image Button,

    I am trying to find an instance of my “MenuItem” control in code behind, using the following code:  
     
    Dim MyMenu As MenuItem = CType(Page.FindControl(“MenuItem1”), MenuItem)

    I’m getting the following error:

    Compiler Error Message: BC30311: Value of type 'System.Web.UI.Control' cannot be converted to 'System.Web.UI.WebControls.MenuItem'.


    However, the above code works with a regular ImageButton…

    Dim MyMenu As ImageButton

    MyMenu = CType(Page.FindControl(“MenuItem1"), ImageButton)

    How come I cannot do this with my custom ImageButton?


    Thanks for any guidance,

    - Joel

    Tuesday, January 27, 2009 2:38 PM
  • User955742345 posted

    You need to look at the namespace in the error: "System.Web.UI.WebControls.MenuItem" 

    That is not the MenuItem you created; that is a built-in type in the .NET framework.  You need to fully qualify your custom control, e.g.: CType(Page.FindControl("MenuItem1"), MyNamespace.MenuItem)

    Using the namespace from the code I posted [but keeping your class name of "MenuItem"], it would be:

     

    Dim MyMenu As MenuItem = CType(Page.FindControl(“MenuItem1”), _
      TestControls.MenuItem)
    
     
    Friday, January 30, 2009 2:18 AM