Hi.
Here are the answers to your previous post:
"Now, can you show us line 10 of c:\inetpub\wwwr oot\test\Class1 .vb so we
can
see just which line of code the compiler is having trouble casting?"
Line 10: Index = clsArray.Add(Co mpanyArray, 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." <s-***@nospam.nosp am> wrote in message
news:uc******** ******@TK2MSFTN GP15.phx.gbl...
I'm guessing the problem is in here:
Dim Index As Integer
Try
Index = clsArray.Add(Co mpanyArray, 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ø" <ja*@will.not.g et.any.spam.bar n-nett.com> wrote in
message news:d%******** ***********@new s4.e.nsc.no... Hi.
Here is the exception.
?ex
{System.Invalid CastException}
[System.InvalidC astException]: {System.Invalid CastException}
HelpLink: Nothing
InnerException: Nothing
Message: "Specified cast is not valid."
Source: "test"
StackTrace: " at test.clsCompani es.Add(clsCompa ny Obj) in
c:\inetpub\wwwr oot\test\Class1 .vb:line 10"
TargetSite: {System.Reflect ion.RuntimeMeth odInfo}
John-Arne Lillebø
"Scott M." <s-***@nospam.nosp am> wrote in message
news:OB******** ******@tk2msftn gp13.phx.gbl... Can you post the entire text of the exception? The message would
indicate what line is running into this problem.
"John-Arne Lillebø" <ja*@will.not.g et.any.spam.bar n-nett.com> wrote in
message news:MK******** ***********@new s4.e.nsc.no...
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.EventArg s) Handles MyBase.Load
'test is the project name.
Dim var_Companies As New test.clsCompani es
Dim var_Company As New test.clsCompany
Dim Index As Integer
var_Company.Nam e = "Test"
Index = var_Companies.A dd(var_Company)
Index = var_Companies.A dd(var_Company)
Index = var_Companies.A dd(var_Company)
Index = var_Companies.A dd(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(Co mpanyArray, Obj)
Catch ex As Exception
System.Diagnost ics.Debugger.Br eak()
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.GetUpp erBound(0) > 0 Or IsNothing(objAr ray(0)) =
False Then
ReDim Preserve objArray(objArr ay.GetUpperBoun d(0) + 1)
End If
objArray(objArr ay.GetUpperBoun d(0)) = Obj
Index = CInt(objArray.G etUpperBound(0) )
Catch ex As Exception
System.Diagnost ics.Debugger.Br eak()
Throw ex
Finally
End Try
Return Index
End Function
End Class