Error using object type array.

  • Thread starter Thread starter William Apple
  • Start date Start date
W

William Apple

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?
 
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.
 
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.
 
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 said:
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.
 
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
 
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.
 
Thanks, that works. I also found this to work also

varlist(0).objVariableValue = New Object(2) {}
 
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.
 

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