Specified cast is not valid.

J

John-Arne Lillebø

Hi.

I run into this problem and i could need some help to solve it.
The project is an ASP.NET Web project.

Including code sample of the problem.

Any idea what is causing the error message ?

John-Arne Lillebø


The ASPX page:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'test is the project name.
Dim var_Companies As New test.clsCompanies
Dim var_Company As New test.clsCompany
Dim Index As Integer

var_Company.Name = "Test"

Index = var_Companies.Add(var_Company)
Index = var_Companies.Add(var_Company)
Index = var_Companies.Add(var_Company)
Index = var_Companies.Add(var_Company)

var_Company = Nothing
var_Companies = Nothing
End Sub


The Class1.vb file

Public Class clsCompanies
Private CompanyArray() As clsCompany

Public Sub New()
ReDim CompanyArray(0)
End Sub
Public Function Add(ByVal Obj As clsCompany) As Integer
Dim Index As Integer
Try
Index = clsArray.Add(CompanyArray, Obj)

Catch ex As Exception
System.Diagnostics.Debugger.Break()
Throw ex
Finally
Add = Index
End Try
End Function
End Class

Public Class clsCompany
Private var_Name As String

Public Property Name() As String
Get
Name = var_Name
End Get
Set(ByVal Value As String)
var_Name = Value
End Set
End Property
End Class

Public Class clsArray
Shared Function Add(ByRef objArray() As Object, ByVal Obj As Object) As
Integer
Dim Index As Integer

Index = -1

Try
If objArray.GetUpperBound(0) > 0 Or IsNothing(objArray(0)) =
False Then
ReDim Preserve objArray(objArray.GetUpperBound(0) + 1)
End If

objArray(objArray.GetUpperBound(0)) = Obj

Index = CInt(objArray.GetUpperBound(0))
Catch ex As Exception
System.Diagnostics.Debugger.Break()
Throw ex
Finally

End Try

Return Index
End Function
End Class
 
S

Scott M.

Can you post the entire text of the exception? The message would indicate
what line is running into this problem.
 
J

John-Arne Lillebø

Hi.

Here is the exception.

?ex
{System.InvalidCastException}
[System.InvalidCastException]: {System.InvalidCastException}
HelpLink: Nothing
InnerException: Nothing
Message: "Specified cast is not valid."
Source: "test"
StackTrace: " at test.clsCompanies.Add(clsCompany Obj) in
c:\inetpub\wwwroot\test\Class1.vb:line 10"
TargetSite: {System.Reflection.RuntimeMethodInfo}

John-Arne Lillebø
 
S

Scott M.

Now, can you show us line 10 of c:\inetpub\wwwroot\test\Class1.vb so we can
see just which line of code the compiler is having trouble casting?


John-Arne Lillebø said:
Hi.

Here is the exception.

?ex
{System.InvalidCastException}
[System.InvalidCastException]: {System.InvalidCastException}
HelpLink: Nothing
InnerException: Nothing
Message: "Specified cast is not valid."
Source: "test"
StackTrace: " at test.clsCompanies.Add(clsCompany Obj) in
c:\inetpub\wwwroot\test\Class1.vb:line 10"
TargetSite: {System.Reflection.RuntimeMethodInfo}

John-Arne Lillebø

Scott M. said:
Can you post the entire text of the exception? The message would
indicate what line is running into this problem.
 
S

Scott M.

I'm guessing the problem is in here:

Dim Index As Integer
Try
Index = clsArray.Add(CompanyArray, Obj)

So, a couple of questions for you.. are you working with Option Strict
turned on? If not, you should be. It would alert you to problems like this
with a blue wavy underline before you ever attempt to run your code.

Second, does the Add method of the clsArray class return anything? If so,
what does it return (an array perhaps)? If it does return an array, then
your problem is that you are attempting to assign an Integer to an array and
so the cast would be invalid.

If this is the case, change this:

Dim Index As Integer

to this:

Dim Index As Array

Does this help?


John-Arne Lillebø said:
Hi.

Here is the exception.

?ex
{System.InvalidCastException}
[System.InvalidCastException]: {System.InvalidCastException}
HelpLink: Nothing
InnerException: Nothing
Message: "Specified cast is not valid."
Source: "test"
StackTrace: " at test.clsCompanies.Add(clsCompany Obj) in
c:\inetpub\wwwroot\test\Class1.vb:line 10"
TargetSite: {System.Reflection.RuntimeMethodInfo}

John-Arne Lillebø

Scott M. said:
Can you post the entire text of the exception? The message would
indicate what line is running into this problem.
 
J

John-Arne Lillebø

Hi.

Here are the answers to your previous post:
"Now, can you show us line 10 of c:\inetpub\wwwroot\test\Class1.vb so we
can
see just which line of code the compiler is having trouble casting?"

Line 10: Index = clsArray.Add(CompanyArray, Obj)


And now for the post i replayed on:

Thanks for the tip on the Option Strict.
When i turned this on the compiler tells me this:
Option Strict On disallows implicit conversions from '1-dimensional
array of System.Object' to '1-dimensional array of test.clsCompany'.

Since i now know what the problem is, i need help solving this one also.

What i try to do is to make a general array class. This class only contains
shared functions so no need for any instance of this class inside all my
other classes.
Why i need this is that i have a lot of container classes for other classes
like class Companies are for class Company.
The clsArray should have functions like Add, Find, Remove, Exist, RemoveAll,
ToString etc...
So when i make the other container classes like class Companies all i have
to do is to call all the shared functions in clsArray for the machting
function in class Companies.

How can i write the shared procedure Add so it will support an array of any
class (Company, Person, Car etc).
I now used the following statement:
Shared Function Add(ByRef ObjArray() as Object, ObjToAdd as Object) as
Integer
The return integer should be the index from the array where the new element
(object) was added.

I really hope i do not have to make all of my shared function with a
spesific class type.
Any idea how to solve this ? Perhaps with some Cast statements ?

Please use my example and modify it the way you need to make this work.

John-Arne Lillebø



Scott M. said:
I'm guessing the problem is in here:

Dim Index As Integer
Try
Index = clsArray.Add(CompanyArray, Obj)

So, a couple of questions for you.. are you working with Option Strict
turned on? If not, you should be. It would alert you to problems like
this with a blue wavy underline before you ever attempt to run your code.

Second, does the Add method of the clsArray class return anything? If so,
what does it return (an array perhaps)? If it does return an array, then
your problem is that you are attempting to assign an Integer to an array
and so the cast would be invalid.

If this is the case, change this:

Dim Index As Integer

to this:

Dim Index As Array

Does this help?


John-Arne Lillebø said:
Hi.

Here is the exception.

?ex
{System.InvalidCastException}
[System.InvalidCastException]: {System.InvalidCastException}
HelpLink: Nothing
InnerException: Nothing
Message: "Specified cast is not valid."
Source: "test"
StackTrace: " at test.clsCompanies.Add(clsCompany Obj) in
c:\inetpub\wwwroot\test\Class1.vb:line 10"
TargetSite: {System.Reflection.RuntimeMethodInfo}

John-Arne Lillebø

Scott M. said:
Can you post the entire text of the exception? The message would
indicate what line is running into this problem.


message Hi.

I run into this problem and i could need some help to solve it.
The project is an ASP.NET Web project.

Including code sample of the problem.

Any idea what is causing the error message ?

John-Arne Lillebø


The ASPX page:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'test is the project name.
Dim var_Companies As New test.clsCompanies
Dim var_Company As New test.clsCompany
Dim Index As Integer

var_Company.Name = "Test"

Index = var_Companies.Add(var_Company)
Index = var_Companies.Add(var_Company)
Index = var_Companies.Add(var_Company)
Index = var_Companies.Add(var_Company)

var_Company = Nothing
var_Companies = Nothing
End Sub


The Class1.vb file

Public Class clsCompanies
Private CompanyArray() As clsCompany

Public Sub New()
ReDim CompanyArray(0)
End Sub
Public Function Add(ByVal Obj As clsCompany) As Integer
Dim Index As Integer
Try
Index = clsArray.Add(CompanyArray, Obj)

Catch ex As Exception
System.Diagnostics.Debugger.Break()
Throw ex
Finally
Add = Index
End Try
End Function
End Class

Public Class clsCompany
Private var_Name As String

Public Property Name() As String
Get
Name = var_Name
End Get
Set(ByVal Value As String)
var_Name = Value
End Set
End Property
End Class

Public Class clsArray
Shared Function Add(ByRef objArray() As Object, ByVal Obj As Object)
As Integer
Dim Index As Integer

Index = -1

Try
If objArray.GetUpperBound(0) > 0 Or IsNothing(objArray(0)) =
False Then
ReDim Preserve objArray(objArray.GetUpperBound(0) + 1)
End If

objArray(objArray.GetUpperBound(0)) = Obj

Index = CInt(objArray.GetUpperBound(0))
Catch ex As Exception
System.Diagnostics.Debugger.Break()
Throw ex
Finally

End Try

Return Index
End Function
End Class
 
S

Scott M.

Ok John, here's the deal.

First to address the exception you are getting...Your Add method isn't
returning an Integer (although you may want it to), it is returning an Array
and Index is declared as an Integer so:

Index = clsArray.Add(CompanyArray, Obj)

tells the compiler to take the array that the add method returns and convert
it to an object and this is where your program is crashing. Even if that
part worked, Index is declared as an integer and you would still get an
exception there as well.

What you want to do is get away from an "array" class and create a
Strongly-Typed collection class. Arrays are for data-types that should
contain more than one value using the same pointer. Collections are for
objects that should contain groups of other objects.

Here's a sample:

Public Class Companies
Inherits System.Collections.CollectionBase

Default Public Property Item(ByVal Index As Integer) As Integer
Get
Return CType(Me.List.Item(Index))
End Get
Set(ByVal Value As Index)
Me.List.Item(Index) = Value
End Set
End Property

Public Function Add(ByVal myComp As Company) As Integer
Me.List.Add(myComp)

'Returns the size of the collection and since the item just added
will be added to the
'end of the collection, this should be the index of the item just
added
Return Me.List.Count -1
End Sub

Public Sub Remove(ByVal myComp As Company)
Me.List.Remove(myComp )
End Sub

Public Sub RemoveAll()
Me.List.Clear()
End Sub

Public Function Exists(ByVal index As Integer) As Boolean
If IsNothing(Me.List.Item(index)) Then
Return False
Else
Return True
End If
End Function

End Class



Hope this helps.

-Scott


John-Arne Lillebø said:
Hi.

Here are the answers to your previous post:
"Now, can you show us line 10 of c:\inetpub\wwwroot\test\Class1.vb so
we can
see just which line of code the compiler is having trouble casting?"

Line 10: Index = clsArray.Add(CompanyArray, Obj)


And now for the post i replayed on:

Thanks for the tip on the Option Strict.
When i turned this on the compiler tells me this:
Option Strict On disallows implicit conversions from '1-dimensional
array of System.Object' to '1-dimensional array of test.clsCompany'.

Since i now know what the problem is, i need help solving this one also.

What i try to do is to make a general array class. This class only
contains shared functions so no need for any instance of this class inside
all my other classes.
Why i need this is that i have a lot of container classes for other
classes like class Companies are for class Company.
The clsArray should have functions like Add, Find, Remove, Exist,
RemoveAll, ToString etc...
So when i make the other container classes like class Companies all i have
to do is to call all the shared functions in clsArray for the machting
function in class Companies.

How can i write the shared procedure Add so it will support an array of
any class (Company, Person, Car etc).
I now used the following statement:
Shared Function Add(ByRef ObjArray() as Object, ObjToAdd as Object) as
Integer
The return integer should be the index from the array where the new
element (object) was added.

I really hope i do not have to make all of my shared function with a
spesific class type.
Any idea how to solve this ? Perhaps with some Cast statements ?

Please use my example and modify it the way you need to make this work.

John-Arne Lillebø



Scott M. said:
I'm guessing the problem is in here:

Dim Index As Integer
Try
Index = clsArray.Add(CompanyArray, Obj)

So, a couple of questions for you.. are you working with Option Strict
turned on? If not, you should be. It would alert you to problems like
this with a blue wavy underline before you ever attempt to run your code.

Second, does the Add method of the clsArray class return anything? If
so, what does it return (an array perhaps)? If it does return an array,
then your problem is that you are attempting to assign an Integer to an
array and so the cast would be invalid.

If this is the case, change this:

Dim Index As Integer

to this:

Dim Index As Array

Does this help?


John-Arne Lillebø said:
Hi.

Here is the exception.

?ex
{System.InvalidCastException}
[System.InvalidCastException]: {System.InvalidCastException}
HelpLink: Nothing
InnerException: Nothing
Message: "Specified cast is not valid."
Source: "test"
StackTrace: " at test.clsCompanies.Add(clsCompany Obj) in
c:\inetpub\wwwroot\test\Class1.vb:line 10"
TargetSite: {System.Reflection.RuntimeMethodInfo}

John-Arne Lillebø

Can you post the entire text of the exception? The message would
indicate what line is running into this problem.


message Hi.

I run into this problem and i could need some help to solve it.
The project is an ASP.NET Web project.

Including code sample of the problem.

Any idea what is causing the error message ?

John-Arne Lillebø


The ASPX page:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'test is the project name.
Dim var_Companies As New test.clsCompanies
Dim var_Company As New test.clsCompany
Dim Index As Integer

var_Company.Name = "Test"

Index = var_Companies.Add(var_Company)
Index = var_Companies.Add(var_Company)
Index = var_Companies.Add(var_Company)
Index = var_Companies.Add(var_Company)

var_Company = Nothing
var_Companies = Nothing
End Sub


The Class1.vb file

Public Class clsCompanies
Private CompanyArray() As clsCompany

Public Sub New()
ReDim CompanyArray(0)
End Sub
Public Function Add(ByVal Obj As clsCompany) As Integer
Dim Index As Integer
Try
Index = clsArray.Add(CompanyArray, Obj)

Catch ex As Exception
System.Diagnostics.Debugger.Break()
Throw ex
Finally
Add = Index
End Try
End Function
End Class

Public Class clsCompany
Private var_Name As String

Public Property Name() As String
Get
Name = var_Name
End Get
Set(ByVal Value As String)
var_Name = Value
End Set
End Property
End Class

Public Class clsArray
Shared Function Add(ByRef objArray() As Object, ByVal Obj As
Object) As Integer
Dim Index As Integer

Index = -1

Try
If objArray.GetUpperBound(0) > 0 Or IsNothing(objArray(0))
= False Then
ReDim Preserve objArray(objArray.GetUpperBound(0) + 1)
End If

objArray(objArray.GetUpperBound(0)) = Obj

Index = CInt(objArray.GetUpperBound(0))
Catch ex As Exception
System.Diagnostics.Debugger.Break()
Throw ex
Finally

End Try

Return Index
End Function
End Class
 

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

Top