By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,032 Members | 1,183 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,032 IT Pros & Developers. It's quick & easy.

Error using object type array.

P: n/a
Despite the fact this deals with webservices I believe it is a VB question.

I am working on a test application that passes data to a webservice.
The webservices takes a variable type that is defined below:

Public Class Variable
Inherits MarshalByRefObject

'<remarks/>
Public strVariableName As String

'<remarks/>
Public objVariableValue() As Object
End Class

My code is as follows
Dim varList(2) as Variable

varList(0) = New Variable
varList(0).strVariableName = "Name"
varList(0).objVariableValue = New Object <- Error Occurs Here
varList(0).objVariableValue(0) = name

If I use the above code I get a "Specified Cast is not Valid" error.

If I code it this way...

Dim varList(2) as Variable

varList(0) = New Variable
varList(0).strVariableName = "Name"
varList(0).objVariableValue(0) = name <-- Error Occurs here

I get "Object reference not set to an instance of an object." as my
error. What am I missing?
Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Tue, 28 Sep 2004 09:14:33 -0400, William Apple wrote:
Public Class Variable
Inherits MarshalByRefObject

'<remarks/>
Public strVariableName As String

'<remarks/>
Public objVariableValue() As Object
End Class

My code is as follows
Dim varList(2) as Variable

varList(0) = New Variable
varList(0).strVariableName = "Name"
varList(0).objVariableValue = New Object <- Error Occurs Here
varList(0).objVariableValue(0) = name


You've declared objVariableValue as an array of objects, so you need to
initialize it as an array of objects and specify a size for the array:

Public objVariableValue(size) As Object

Or if you didn't mean for objVariableValue to be an array of objects,
remove the parentheses in the declaration:

'<remarks/>
Public objVariableValue As Object

Hope this helps

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Nov 21 '05 #2

P: n/a
Chris has answered the first part of your problem. For the second part:
If I code it this way...

Dim varList(2) as Variable

varList(0) = New Variable
varList(0).strVariableName = "Name"
varList(0).objVariableValue(0) = name <-- Error Occurs here


What variable is 'name'? I don't see it being declared anywhere in the piece
of code you've posted. Is this declared somewhere else? Or did you mean
something like:
varList(0).objVariableValue(0) = "name"
If so, then you do need to create an instance of the object before assigning
it a value:
varList(0).objVariableValue(0) = New Object
varList(0).objVariableValue(0) = "name"
hope that helps..
Imran.
Nov 21 '05 #3

P: n/a
I didn't write the webservice, so I have no control over the declaration of
objVariableValue. Is it possible to get this to work or is it a bug on their
part?

"Chris Dunaway" <"dunawayc[[at]_lunchmeat_sbcglobal[dot]]net"> wrote in
message news:yl*****************************@40tude.net...
On Tue, 28 Sep 2004 09:14:33 -0400, William Apple wrote:
Public Class Variable
Inherits MarshalByRefObject

'<remarks/>
Public strVariableName As String

'<remarks/>
Public objVariableValue() As Object
End Class

My code is as follows
Dim varList(2) as Variable

varList(0) = New Variable
varList(0).strVariableName = "Name"
varList(0).objVariableValue = New Object <- Error Occurs Here
varList(0).objVariableValue(0) = name


You've declared objVariableValue as an array of objects, so you need to
initialize it as an array of objects and specify a size for the array:

Public objVariableValue(size) As Object

Or if you didn't mean for objVariableValue to be an array of objects,
remove the parentheses in the declaration:

'<remarks/>
Public objVariableValue As Object

Hope this helps

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.

Nov 21 '05 #4

P: n/a
My code should have read.....the variable name was declared. How I wish it
was simple stupidity on my part.

Dim varList(2) as Variable
Dim name As Object

name = "xyz"

varList(0) = New Variable
varList(0).strVariableName = "Name"
varList(0).objVariableValue = New Object <- Error Occurs Here
varList(0).objVariableValue(0) = name

If I use the above code I get a "Specified Cast is not Valid" error.

If I code it this way...

Dim varList(2) as Variable

varList(0) = New Variable
varList(0).strVariableName = "Name"
varList(0).objVariableValue(0) = name <-- Error Occurs here
"Imran Koradia" <no****@microsoft.com> wrote in message
news:ua*************@TK2MSFTNGP14.phx.gbl...
Chris has answered the first part of your problem. For the second part:
If I code it this way...

Dim varList(2) as Variable

varList(0) = New Variable
varList(0).strVariableName = "Name"
varList(0).objVariableValue(0) = name <-- Error Occurs here
What variable is 'name'? I don't see it being declared anywhere in the

piece of code you've posted. Is this declared somewhere else? Or did you mean
something like:
varList(0).objVariableValue(0) = "name"
If so, then you do need to create an instance of the object before assigning it a value:
varList(0).objVariableValue(0) = New Object
varList(0).objVariableValue(0) = "name"
hope that helps..
Imran.

Nov 21 '05 #5

P: n/a
The problem is that you've not defined the length of the objVariableValue
array. So, you should do something like this:

Dim varList(2) as Variable
Dim name As Object

name = "xyz"

' this will make your object array of length 3
ReDim varList(0).objVariableValue(2)

varList(0) = New Variable
varList(0).strVariableName = "Name"
varList(0).objVariableValue(0) = New Object
varList(0).objVariableValue(0) = name
hope that helps..
Imran.

"William Apple" <wa****@nospam-cisys.com> wrote in message
news:uY**************@TK2MSFTNGP15.phx.gbl...
My code should have read.....the variable name was declared. How I wish it
was simple stupidity on my part.

Dim varList(2) as Variable
Dim name As Object

name = "xyz"

varList(0) = New Variable
varList(0).strVariableName = "Name"
varList(0).objVariableValue = New Object <- Error Occurs Here
varList(0).objVariableValue(0) = name

If I use the above code I get a "Specified Cast is not Valid" error.

If I code it this way...

Dim varList(2) as Variable

varList(0) = New Variable
varList(0).strVariableName = "Name"
varList(0).objVariableValue(0) = name <-- Error Occurs here
"Imran Koradia" <no****@microsoft.com> wrote in message
news:ua*************@TK2MSFTNGP14.phx.gbl...
Chris has answered the first part of your problem. For the second part:
If I code it this way...

Dim varList(2) as Variable

varList(0) = New Variable
varList(0).strVariableName = "Name"
varList(0).objVariableValue(0) = name <-- Error Occurs here


What variable is 'name'? I don't see it being declared anywhere in the

piece
of code you've posted. Is this declared somewhere else? Or did you mean
something like:
varList(0).objVariableValue(0) = "name"
If so, then you do need to create an instance of the object before

assigning
it a value:
varList(0).objVariableValue(0) = New Object
varList(0).objVariableValue(0) = "name"
hope that helps..
Imran.


Nov 21 '05 #6

P: n/a
Thanks, that works. I also found this to work also

varlist(0).objVariableValue = New Object(2) {}

"Imran Koradia" <no****@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
The problem is that you've not defined the length of the objVariableValue
array. So, you should do something like this:

Dim varList(2) as Variable
Dim name As Object

name = "xyz"

' this will make your object array of length 3
ReDim varList(0).objVariableValue(2)

varList(0) = New Variable
varList(0).strVariableName = "Name"
varList(0).objVariableValue(0) = New Object
varList(0).objVariableValue(0) = name
hope that helps..
Imran.

"William Apple" <wa****@nospam-cisys.com> wrote in message
news:uY**************@TK2MSFTNGP15.phx.gbl...
My code should have read.....the variable name was declared. How I wish it was simple stupidity on my part.

Dim varList(2) as Variable
Dim name As Object

name = "xyz"

varList(0) = New Variable
varList(0).strVariableName = "Name"
varList(0).objVariableValue = New Object <- Error Occurs Here
varList(0).objVariableValue(0) = name

If I use the above code I get a "Specified Cast is not Valid" error.

If I code it this way...

Dim varList(2) as Variable

varList(0) = New Variable
varList(0).strVariableName = "Name"
varList(0).objVariableValue(0) = name <-- Error Occurs here
"Imran Koradia" <no****@microsoft.com> wrote in message
news:ua*************@TK2MSFTNGP14.phx.gbl...
Chris has answered the first part of your problem. For the second part:
> If I code it this way...
>
> Dim varList(2) as Variable
>
> varList(0) = New Variable
> varList(0).strVariableName = "Name"
> varList(0).objVariableValue(0) = name <-- Error Occurs here

What variable is 'name'? I don't see it being declared anywhere in the

piece
of code you've posted. Is this declared somewhere else? Or did you mean something like:
varList(0).objVariableValue(0) = "name"
If so, then you do need to create an instance of the object before

assigning
it a value:
varList(0).objVariableValue(0) = New Object
varList(0).objVariableValue(0) = "name"
hope that helps..
Imran.



Nov 21 '05 #7

P: n/a
Yup - thats fine too. However, if you just want to assign the variable
'name' to the object, you don't necessarily have to create a new instance
(which is what's happening with New Object(2) {}) since the instance already
exists (the 'name' instance) and you directly assign the reference to it.
Just a thought...

Imran.

"William Apple" <wa****@nospam-cisys.com> wrote in message
news:uT**************@TK2MSFTNGP09.phx.gbl...
Thanks, that works. I also found this to work also

varlist(0).objVariableValue = New Object(2) {}

"Imran Koradia" <no****@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
The problem is that you've not defined the length of the objVariableValue
array. So, you should do something like this:

Dim varList(2) as Variable
Dim name As Object

name = "xyz"

' this will make your object array of length 3
ReDim varList(0).objVariableValue(2)

varList(0) = New Variable
varList(0).strVariableName = "Name"
varList(0).objVariableValue(0) = New Object
varList(0).objVariableValue(0) = name
hope that helps..
Imran.

"William Apple" <wa****@nospam-cisys.com> wrote in message
news:uY**************@TK2MSFTNGP15.phx.gbl...
My code should have read.....the variable name was declared. How I wish
it was simple stupidity on my part.

Dim varList(2) as Variable
Dim name As Object

name = "xyz"

varList(0) = New Variable
varList(0).strVariableName = "Name"
varList(0).objVariableValue = New Object <- Error Occurs Here
varList(0).objVariableValue(0) = name

If I use the above code I get a "Specified Cast is not Valid" error.

If I code it this way...

Dim varList(2) as Variable

varList(0) = New Variable
varList(0).strVariableName = "Name"
varList(0).objVariableValue(0) = name <-- Error Occurs here
"Imran Koradia" <no****@microsoft.com> wrote in message
news:ua*************@TK2MSFTNGP14.phx.gbl...
> Chris has answered the first part of your problem. For the second part: >
> > If I code it this way...
> >
> > Dim varList(2) as Variable
> >
> > varList(0) = New Variable
> > varList(0).strVariableName = "Name"
> > varList(0).objVariableValue(0) = name <-- Error Occurs here
>
> What variable is 'name'? I don't see it being declared anywhere in
the piece
> of code you've posted. Is this declared somewhere else? Or did you

mean > something like:
> varList(0).objVariableValue(0) = "name"
> If so, then you do need to create an instance of the object before
assigning
> it a value:
> varList(0).objVariableValue(0) = New Object
> varList(0).objVariableValue(0) = "name"
>
>
> hope that helps..
> Imran.
>
>



Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.