473,323 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

CType not working for Button

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
Nov 19 '05 #1
11 1608

"tshad" <ts**********@ftsolutions.com> wrote in message
news:OB**************@TK2MSFTNGP14.phx.gbl...
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
Nov 19 '05 #2
Try this:

Dim submitButton = CType(sender, Button)
Jason Bentley

Nov 19 '05 #3
"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:e5**************@TK2MSFTNGP14.phx.gbl...

"tshad" <ts**********@ftsolutions.com> wrote in message
news:OB**************@TK2MSFTNGP14.phx.gbl...
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.


I did that and got the result:

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

Tom
Mythran

Nov 19 '05 #4

<im****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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

Jason Bentley

Nov 19 '05 #5
Yes, but you are not really creating a new button, you are just copying
an existing button.

Jason Bentley
http://geekswithblogs.net/jbentley

Nov 19 '05 #6
<im****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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
Jason Bentley
http://geekswithblogs.net/jbentley

Nov 19 '05 #7
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
http://geekswithblogs.net/jbentley

Nov 19 '05 #8
<im****@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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).Fin dControl("DataGrid1"),DataGrid)

to

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

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

Thanks,

Tom


Jason Bentley
http://geekswithblogs.net/jbentley

Nov 19 '05 #9
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.

Nov 19 '05 #10

"tshad" <ts**********@ftsolutions.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
<im****@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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).Fin dControl("DataGrid1"),DataGrid)

to

Dim oGrid =
CType(DataList1.Items(DataList1.SelectedIndex).Fin dControl("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
Nov 19 '05 #11
"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:uE**************@tk2msftngp13.phx.gbl...

"tshad" <ts**********@ftsolutions.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
<im****@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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).Fin dControl("DataGrid1"),DataGrid)

to

Dim oGrid =
CType(DataList1.Items(DataList1.SelectedIndex).Fin dControl("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.

That was what I was looking for and makes sense.

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

Thanks,

Tom
Mythran

Nov 19 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Joeri | last post by:
Hi, I'm struggling with this issue, maybe someone has already solved ik before. Here's the problem I Have : clsA witch is a base class clsB inherits from clsA clsC inherits from clsA clsD...
10
by: Douglas Buchanan | last post by:
I am using the following code instead of a very lengthly select case statement. (I have a lot of lookup tables in a settings form that are selected from a ListBox. The data adapters are given a...
8
by: lawrence | last post by:
I'm a beginner with Javascript and especially cross-browser Javascript. I got this working in IE, but not in Netscape 7. It seems like, in Netscape, every time I click on a button, the focus shifts...
13
by: Carl Howarth | last post by:
Hello there, I am currently in the process of 'getting strict' with some of our aspx.vb files. I have a function that is called either when a datarepeater is bound or when a button on the...
2
by: Marcus Kwok | last post by:
I was following the tutorial given at: http://msdn.microsoft.com/netframework/programming/winforms/?pull=/library/en-us/dndotnet/html/mdiappsmenus.asp except writing Managed C++ instead of...
4
by: AXH | last post by:
Hi All, I have to following problem. Is it possible type convert a stringvalue to a type value. Please look below for the example dim regvalue as string= "123456" dim regtype as string =...
6
by: Don | last post by:
Using VS 2005 and VB.NET. In the sample code below, GetFirst returns an Item Object, but it's wrapped in the System.__ComObject type. When I try to use CType to get a MailItem object, I get the...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.