CType not working for Button

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

I have the following:

sub submitQuestion_click(Sender as Object, e as EventArgs)
Dim submitButton as Button = CType(sender,Button)

This gives me an error that says:

" System.InvalidCastException: Specified cast is not valid. "

Why is that?

Sender is a button.

Tom
 
tshad said:
I have the following:

sub submitQuestion_click(Sender as Object, e as EventArgs)
Dim submitButton as Button = CType(sender,Button)

This gives me an error that says:

" System.InvalidCastException: Specified cast is not valid. "

Why is that?

Sender is a button.

Tom

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
 
Mythran said:
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.

I did that and got the result:

sender type = System.Web.UI.WebControls.Button

Tom
 
Try this:

Dim submitButton = CType(sender, Button)

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
 
Yes, but you are not really creating a new button, you are just copying
an existing button.

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
 
I do not think so, you are creating a button that references the
original button which also takes the original button's type.

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(DataList1.SelectedIndex).FindControl("DataGrid1"),DataGrid)

to

Dim oGrid =
CType(DataList1.Items(DataList1.SelectedIndex).FindControl("DataGrid1"),DataGrid)

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

Thanks,

Tom
 
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.
 
tshad said:
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(DataList1.SelectedIndex).FindControl("DataGrid1"),DataGrid)

to

Dim oGrid =
CType(DataList1.Items(DataList1.SelectedIndex).FindControl("DataGrid1"),DataGrid)

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

Thanks,

Tom

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.OleDbConnection()

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
 
Mythran said:
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.OleDbConnection()

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.
That was what I was looking for and makes sense.

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

Thanks,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top