Cast from type 'Object()' to type 'String' is not valid

G

Guest

Hello,

I'm not a developper, so sorry if it's a stupid question...
I'm trying to develop an application in vb.net and I have the following
problem:

I have some information in an array: sdist(i). The information is a string.
When I run the application, I don"t have problem for compilation but during
te execution, I have the error Cast from type 'Object()' to type 'String' is
not valid on the line: sdistinguishedname = Sdist(i). Or sdistinguishedname
is defined as string.

Do you have any idea ?

Thanks

Benoit
 
P

Patrice

How is declared/initialized the array ? It looks like you are trying to
assign an array of objects to a string...
 
G

Guest

Hi,

I am not sure how you declared the array. Following is the sample code
that might help you.

Dim test(4) as string
test(0) = "hello"
test(1) = "world"
test(2) = "hi"

Dim test1 as string
test1 = test(0)

Now test1 will have "hello" in it.

Thanks,
Sridhar.
 
G

Guest

Hi,

I've just to try to change the declaration: dim dist(100) as string and I
have the same error but on an other line: "Cast from type 'Object()' to type
'String' is not "valid.

Sdist(i) = oRecordSet.Fields("distinguishedname").value

do you have an idea ?
 
P

Patrice

As you saw not specifying the type defaults to Object (i.e. it was an array
of objects).

Now it looks like oRecordsset.Fields("distinguishedname").value returns an
array of objects. It would be consistent with the previous behavior (ie. it
returned an array of object that you put in a cell as an object and later
you tried to assign this in a string).

So finally the culprit should be
oRecordSet.Fields("distinguishedname").value. Not frequent but it looks like
you are retrieving a multivalued field (from LDAP ?)

--
 
G

Guest

You're right, it 's for LDAP query.
If I use a "watch", I can see:

Name
oRecordSet.Fields("distinguishedname").Value = > type object

(0) = > type string

Value
{System.Array} = > type object
"test string" = > type string


Do you know how I can do ?
 
P

Patrice

AFAIK some LDAP fields are actually arrays... So you would have to test if
this is an array and to react accordingly.

What annoys me for now is that AFAIK the distinguishedname is NOT
multivalued. Is this field the "real" distinguishedname LDAP value ?

You could try something like (not tested) :

o=oRecordSet.Fields("distinguishedname").Value
If IsArray(o) Then
For i=0 to Ubound(o)
MsgBox o(i)
Next
Else
Mgbox("Not an array")
End If

To see what values are returned in this array. It would perhaps allows to
find out if this the info you expected or some other kind of information
than the distinguishedname...

Good luck.
 
G

Guest

Patrice, thanks, It seems better now.

Benoit

Patrice said:
AFAIK some LDAP fields are actually arrays... So you would have to test if
this is an array and to react accordingly.

What annoys me for now is that AFAIK the distinguishedname is NOT
multivalued. Is this field the "real" distinguishedname LDAP value ?

You could try something like (not tested) :

o=oRecordSet.Fields("distinguishedname").Value
If IsArray(o) Then
For i=0 to Ubound(o)
MsgBox o(i)
Next
Else
Mgbox("Not an array")
End If

To see what values are returned in this array. It would perhaps allows to
find out if this the info you expected or some other kind of information
than the distinguishedname...

Good luck.
 

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