CType not working for Button

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tshad

    CType not working for Button

    I have the following:

    sub submitQuestion_ click(Sender as Object, e as EventArgs)
    Dim submitButton as Button = CType(sender,Bu tton)

    This gives me an error that says:

    " System.InvalidC astException: Specified cast is not valid. "

    Why is that?

    Sender is a button.

    Tom


  • Mythran

    #2
    Re: CType not working for Button


    "tshad" <tscheiderich@f tsolutions.com> wrote in message
    news:OBFckYmJFH A.3500@TK2MSFTN GP14.phx.gbl...[color=blue]
    >I have the following:
    >
    > sub submitQuestion_ click(Sender as Object, e as EventArgs)
    > Dim submitButton as Button = CType(sender,Bu tton)
    >
    > This gives me an error that says:
    >
    > " System.InvalidC astException: Specified cast is not valid. "
    >
    > Why is that?
    >
    > Sender is a button.
    >
    > Tom
    >[/color]

    Try this and if you still can't figure it out, post the results...

    Sub submitQuestion_ Click(ByVal Sender As Object, ByVal e As EventArgs)
    Response.Write( Sender.GetType( ).ToString())
    End Sub

    Then run the application and click the button.

    If this doesn't help, remember to post what is printed.

    Mythran


    Comment

    • imsinc@gmail.com

      #3
      Re: CType not working for Button

      Try this:

      Dim submitButton = CType(sender, Button)


      Jason Bentley

      Comment

      • tshad

        #4
        Re: CType not working for Button

        "Mythran" <kip_potter@hot mail.comREMOVET RAIL> wrote in message
        news:e56j5fmJFH A.2132@TK2MSFTN GP14.phx.gbl...[color=blue]
        >
        > "tshad" <tscheiderich@f tsolutions.com> wrote in message
        > news:OBFckYmJFH A.3500@TK2MSFTN GP14.phx.gbl...[color=green]
        >>I have the following:
        >>
        >> sub submitQuestion_ click(Sender as Object, e as EventArgs)
        >> Dim submitButton as Button = CType(sender,Bu tton)
        >>
        >> This gives me an error that says:
        >>
        >> " System.InvalidC astException: Specified cast is not valid. "
        >>
        >> Why is that?
        >>
        >> Sender is a button.
        >>
        >> Tom
        >>[/color]
        >
        > Try this and if you still can't figure it out, post the results...
        >
        > Sub submitQuestion_ Click(ByVal Sender As Object, ByVal e As EventArgs)
        > Response.Write( Sender.GetType( ).ToString())
        > End Sub
        >
        > Then run the application and click the button.
        >
        > If this doesn't help, remember to post what is printed.[/color]

        I did that and got the result:

        sender type = System.Web.UI.W ebControls.Butt on

        Tom[color=blue]
        >
        > Mythran
        >
        >[/color]


        Comment

        • tshad

          #5
          Re: CType not working for Button


          <imsinc@gmail.c om> wrote in message
          news:1110565987 .065914.266310@ o13g2000cwo.goo glegroups.com.. .[color=blue]
          > Try this:
          >
          > Dim submitButton = CType(sender, Button)[/color]

          Actually, it seems both ways work.

          I don't know why my old line wasn't working. I just copied the line back in
          from my original post and it work. Not sure what happened.

          You're way works also, BTW.

          Why would that work, I thought you had to have the "as" when dimming a
          variable?

          Thanks,

          Tom[color=blue]
          >
          >
          > Jason Bentley
          >[/color]


          Comment

          • imsinc@gmail.com

            #6
            Re: CType not working for Button

            Yes, but you are not really creating a new button, you are just copying
            an existing button.

            Jason Bentley


            Comment

            • tshad

              #7
              Re: CType not working for Button

              <imsinc@gmail.c om> wrote in message
              news:1110567753 .646593.114010@ o13g2000cwo.goo glegroups.com.. .[color=blue]
              > Yes, but you are not really creating a new button, you are just copying
              > an existing button.[/color]

              But aren't you just making a new instance of that button?

              And I thought you always had to give it a type (which is obviously not the
              case here as it does work) if you Dimmed it.

              Tom[color=blue]
              >
              > Jason Bentley
              > http://geekswithblogs.net/jbentley
              >[/color]


              Comment

              • imsinc@gmail.com

                #8
                Re: CType not working for Button

                I do not think so, you are creating a button that references the
                original button which also takes the original button's type.

                Jason Bentley


                Comment

                • tshad

                  #9
                  Re: CType not working for Button

                  <imsinc@gmail.c om> wrote in message
                  news:1110570444 .614649.48920@o 13g2000cwo.goog legroups.com...[color=blue]
                  >I do not think so, you are creating a button that references the
                  > original button which also takes the original button's type.[/color]

                  So if I am creating a variable and assigning it a value, does that mean I
                  don't need to use the "as", such as:

                  Dim ktr = 135

                  or

                  Dim name = "Franklin"

                  or

                  Can I change:

                  Dim oGrid as DataGrid =
                  CType(DataList1 .Items(DataList 1.SelectedIndex ).FindControl(" DataGrid1"),Dat aGrid)

                  to

                  Dim oGrid =
                  CType(DataList1 .Items(DataList 1.SelectedIndex ).FindControl(" DataGrid1"),Dat aGrid)

                  Not that I would, just trying to see what you can and cannot do and why.

                  Thanks,

                  Tom

                  [color=blue]
                  >
                  > Jason Bentley
                  > http://geekswithblogs.net/jbentley
                  >[/color]


                  Comment

                  • imsinc@gmail.com

                    #10
                    Re: CType not working for Button

                    Tom, I don't see a problem with most of those but your samples above
                    were not exactly what we were doing before. We were using CType. The
                    oGrid may be a little different. I don't see any reason why it will not
                    work if you use Dim oGrid = CType(DataGrid1 , DataGrid). Keep in mind,
                    though, you are referencing an existing object, not creating a new. C#
                    is my first language, also, and you should consult a knowledgable
                    VB.NET person before using it in production.

                    Comment

                    • Mythran

                      #11
                      Re: CType not working for Button


                      "tshad" <tscheiderich@f tsolutions.com> wrote in message
                      news:%23NRInVnJ FHA.2956@TK2MSF TNGP12.phx.gbl. ..[color=blue]
                      > <imsinc@gmail.c om> wrote in message
                      > news:1110570444 .614649.48920@o 13g2000cwo.goog legroups.com...[color=green]
                      >>I do not think so, you are creating a button that references the
                      >> original button which also takes the original button's type.[/color]
                      >
                      > So if I am creating a variable and assigning it a value, does that mean I
                      > don't need to use the "as", such as:
                      >
                      > Dim ktr = 135
                      >
                      > or
                      >
                      > Dim name = "Franklin"
                      >
                      > or
                      >
                      > Can I change:
                      >
                      > Dim oGrid as DataGrid =
                      > CType(DataList1 .Items(DataList 1.SelectedIndex ).FindControl(" DataGrid1"),Dat aGrid)
                      >
                      > to
                      >
                      > Dim oGrid =
                      > CType(DataList1 .Items(DataList 1.SelectedIndex ).FindControl(" DataGrid1"),Dat aGrid)
                      >
                      > Not that I would, just trying to see what you can and cannot do and why.
                      >
                      > Thanks,
                      >
                      > Tom
                      >
                      >[/color]

                      Ok, to clear things up for you guys.

                      Dim value1 As MyObject = New MyObject()
                      Dim value2 As MyObject = value1
                      Dim value3 = CType(value1, MyObject)

                      Variable Variable Type Reference/Value Type
                      value1 MyObject MyObject
                      value2 MyObject MyObject
                      value3 Object MyObject

                      value1 can and does reference MyObject. It can ONLY reference a MyObject
                      object.
                      value2 can and does reference MyObject. It can ONLY reference a MyObject
                      object.
                      value3 can and does reference MyObject. It can reference any reference or
                      value.

                      value1 contains a reference that was created and stored in the value1
                      variable.
                      value2 is declared and then set to the same reference that value1
                      references.
                      value3 is declared AS OBJECT and is set to reference the same reference that
                      value1 references.

                      They are all valid, but note that value3 can be contain a reference or value
                      to anything else.

                      So, the following is also valid (beware of doing this, this is not very good
                      technique and is only being shown to you for your knowledge):

                      Dim value1 As MyObject = New MyObject()
                      Dim value3 = value1
                      value3 = "Test"
                      value3 = 1234
                      value3 = New Button()
                      value3 = New DataGrid()
                      value3 = New System.Data.Ole DbConnection()

                      All the lines above can happen in the same routine in the same order and
                      shouldn't fail. That is why it is best to declare all variables with the
                      types that will be used. Can you see the dangers?

                      Dim myInt = 123

                      myInt = Request("myInt" )

                      Dim x As Integer = myInt

                      Note the above, myInt is set to 123 and you'd probably expect myInt to be an
                      integer value. When you call Request (pulling a variable from the
                      querystring in ASP.Net) it returns a string value. What if that string
                      value wasn't a number? This is a logic error anyways, but using it to prove
                      my point. When you set x equal to myInt (now contains a string that isn't a
                      number), the program would throw an exception.

                      Anywho, that is why declaring without the data type works but should be used
                      extremely rarely and with caution.

                      Mythran


                      Comment

                      • tshad

                        #12
                        Re: CType not working for Button

                        "Mythran" <kip_potter@hot mail.comREMOVET RAIL> wrote in message
                        news:uE1ZuRpJFH A.3420@tk2msftn gp13.phx.gbl...[color=blue]
                        >
                        > "tshad" <tscheiderich@f tsolutions.com> wrote in message
                        > news:%23NRInVnJ FHA.2956@TK2MSF TNGP12.phx.gbl. ..[color=green]
                        >> <imsinc@gmail.c om> wrote in message
                        >> news:1110570444 .614649.48920@o 13g2000cwo.goog legroups.com...[color=darkred]
                        >>>I do not think so, you are creating a button that references the
                        >>> original button which also takes the original button's type.[/color]
                        >>
                        >> So if I am creating a variable and assigning it a value, does that mean I
                        >> don't need to use the "as", such as:
                        >>
                        >> Dim ktr = 135
                        >>
                        >> or
                        >>
                        >> Dim name = "Franklin"
                        >>
                        >> or
                        >>
                        >> Can I change:
                        >>
                        >> Dim oGrid as DataGrid =
                        >> CType(DataList1 .Items(DataList 1.SelectedIndex ).FindControl(" DataGrid1"),Dat aGrid)
                        >>
                        >> to
                        >>
                        >> Dim oGrid =
                        >> CType(DataList1 .Items(DataList 1.SelectedIndex ).FindControl(" DataGrid1"),Dat aGrid)
                        >>
                        >> Not that I would, just trying to see what you can and cannot do and why.
                        >>
                        >> Thanks,
                        >>
                        >> Tom
                        >>
                        >>[/color]
                        >
                        > Ok, to clear things up for you guys.
                        >
                        > Dim value1 As MyObject = New MyObject()
                        > Dim value2 As MyObject = value1
                        > Dim value3 = CType(value1, MyObject)
                        >
                        > Variable Variable Type Reference/Value Type
                        > value1 MyObject MyObject
                        > value2 MyObject MyObject
                        > value3 Object MyObject
                        >
                        > value1 can and does reference MyObject. It can ONLY reference a MyObject
                        > object.
                        > value2 can and does reference MyObject. It can ONLY reference a MyObject
                        > object.
                        > value3 can and does reference MyObject. It can reference any reference or
                        > value.
                        >
                        > value1 contains a reference that was created and stored in the value1
                        > variable.
                        > value2 is declared and then set to the same reference that value1
                        > references.
                        > value3 is declared AS OBJECT and is set to reference the same reference
                        > that value1 references.
                        >
                        > They are all valid, but note that value3 can be contain a reference or
                        > value to anything else.
                        >
                        > So, the following is also valid (beware of doing this, this is not very
                        > good technique and is only being shown to you for your knowledge):
                        >
                        > Dim value1 As MyObject = New MyObject()
                        > Dim value3 = value1
                        > value3 = "Test"
                        > value3 = 1234
                        > value3 = New Button()
                        > value3 = New DataGrid()
                        > value3 = New System.Data.Ole DbConnection()
                        >
                        > All the lines above can happen in the same routine in the same order and
                        > shouldn't fail. That is why it is best to declare all variables with the
                        > types that will be used. Can you see the dangers?
                        >
                        > Dim myInt = 123
                        >
                        > myInt = Request("myInt" )
                        >
                        > Dim x As Integer = myInt
                        >
                        > Note the above, myInt is set to 123 and you'd probably expect myInt to be
                        > an integer value. When you call Request (pulling a variable from the
                        > querystring in ASP.Net) it returns a string value. What if that string
                        > value wasn't a number? This is a logic error anyways, but using it to
                        > prove my point. When you set x equal to myInt (now contains a string that
                        > isn't a number), the program would throw an exception.
                        >
                        > Anywho, that is why declaring without the data type works but should be
                        > used extremely rarely and with caution.
                        >[/color]
                        That was what I was looking for and makes sense.

                        I assume that Dim value3 = value1 makes value3 a variant variable type.

                        Thanks,

                        Tom
                        [color=blue]
                        > Mythran
                        >
                        >[/color]


                        Comment

                        Working...