Can't assign values to array

B

Buddy Ackerman

I created a simple class:

Public Class MyTestClass
Public Test() As String
End Class



I tried to assign some values to the array Test() and display them like this:


Dim clsTest As New MyTestClass
Dim r As String

clsTest.Test.SetValue("First Item", 0)
clsTest.Test.SetValue("Second Item", 1)
clsTest.Test.SetValue("Third Item", 2)
clsTest.Test.SetValue("Fourth Item", 3)
clsTest.Test.SetValue("Fifth Item", 4)

For Each r in clsTest.Test
response.write(r & "<br>")
Next


I get an "Object reference not set to an instance of an object." exception on
the line 'clsTest.Test.SetValue("First Item", 0)'.

I don't understand why. WTF.


--Buddy
 
T

Tor Bådshaug

When you get this runtime-error, there is some problem with an object
reference that is not initialized.
In your case, the problem is inside your "MyTestClass". The public member
"Test()" defines a variable that
refers to an array of strings, but in your code, the array is never
initialized.
If you change your code to the listing below, your code should work just
fine.

Public Class MyTestClass
Public Test(4) As String
End Class

Hope this helps.
Tor Bådshaug
tor.badshaug [//at\\] bekk.no.
 
B

Buddy Ackerman

That's a little strange because the example that I posted was based on an example class that was autogenerated from the
SOAP Proxy Class Wizard in WebMatrix. Here is the class:


<System.Xml.Serialization.XmlTypeAttribute([Namespace]:="urn:crmondemand/ws/lead/")> _
Public Class LeadWS_LeadInsert_Input

'<remarks/>
<System.Xml.Serialization.XmlArrayAttribute([Namespace]:="urn:/crmondemand/xml/lead"), _
System.Xml.Serialization.XmlArrayItemAttribute([Namespace]:="urn:/crmondemand/xml/lead", IsNullable:=false)> _
Public ListOfLead() As Lead1
End Class



So, how would you add a lead to the ListOfLead() array?



--Buddy

When you get this runtime-error, there is some problem with an object
reference that is not initialized.
In your case, the problem is inside your "MyTestClass". The public member
"Test()" defines a variable that
refers to an array of strings, but in your code, the array is never
initialized.
If you change your code to the listing below, your code should work just
fine.

Public Class MyTestClass
Public Test(4) As String
End Class

Hope this helps.
Tor Bådshaug
tor.badshaug [//at\\] bekk.no.

I created a simple class:

Public Class MyTestClass
Public Test() As String
End Class



I tried to assign some values to the array Test() and display them like
this:


Dim clsTest As New MyTestClass
Dim r As String

clsTest.Test.SetValue("First Item", 0)
clsTest.Test.SetValue("Second Item", 1)
clsTest.Test.SetValue("Third Item", 2)
clsTest.Test.SetValue("Fourth Item", 3)
clsTest.Test.SetValue("Fifth Item", 4)

For Each r in clsTest.Test
response.write(r & "<br>")
Next


I get an "Object reference not set to an instance of an object." exception
on

the line 'clsTest.Test.SetValue("First Item", 0)'.

I don't understand why. WTF.


--Buddy
 
T

Tor Bådshaug

There are definitely other options besides the one i mentioned.
Just make sure that in one way or the other, that the variable references an
instantiated array object before it is used.
Public ListOfLead() simply declares it. Using "ListOfLead(4)" instantiates
the array object (an array with 5 elements - each element is Nothing, but
the array itself is no longer Nothing).

So, another approach to your code may be to ensure that the array is
initialized from outside the class

clsTest.Test = New String() {"First Item", "Second Item", "Third Item",
"Fourth Item", "Fifth Item"}

Analogously I would use code to add a lead to the ListOfLead-array (assuming
parameter-less constructor of the classes involved).

Dim leadInput As New LeadWS_LeadInsert_Input
leadInput.ListOfLead = New Lead1() { new Lead1 }

If you want to add a new Lead1 object to the array at the end of a current
array (if an initialized and non-empty array already exists), you will have
to add some logic to handle that. (However, there are other datastructures
that
are more appropriate for this.

Hope this helps.

Tor Bådshaug
tor.badshaug [//at\\] bekk.no.

Buddy Ackerman said:
That's a little strange because the example that I posted was based on an
example class that was autogenerated from the
SOAP Proxy Class Wizard in WebMatrix. Here is the class:
<System.Xml.Serialization.XmlTypeAttribute([Namespace]:="urn:crmondemand/ws/
lead/")> _
Public Class LeadWS_LeadInsert_Input

'<remarks/>
<System.Xml.Serialization.XmlArrayAttribute([Namespace]:="urn:/crmondemand/x
ml/lead"), _System.Xml.Serialization.XmlArrayItemAttribute([Namespace]:="urn:/crmondeman
d/xml/lead", IsNullable:=false)> _
Public ListOfLead() As Lead1
End Class



So, how would you add a lead to the ListOfLead() array?



--Buddy

When you get this runtime-error, there is some problem with an object
reference that is not initialized.
In your case, the problem is inside your "MyTestClass". The public member
"Test()" defines a variable that
refers to an array of strings, but in your code, the array is never
initialized.
If you change your code to the listing below, your code should work just
fine.

Public Class MyTestClass
Public Test(4) As String
End Class

Hope this helps.
Tor Bådshaug
tor.badshaug [//at\\] bekk.no.

I created a simple class:

Public Class MyTestClass
Public Test() As String
End Class



I tried to assign some values to the array Test() and display them like
this:


Dim clsTest As New MyTestClass
Dim r As String

clsTest.Test.SetValue("First Item", 0)
clsTest.Test.SetValue("Second Item", 1)
clsTest.Test.SetValue("Third Item", 2)
clsTest.Test.SetValue("Fourth Item", 3)
clsTest.Test.SetValue("Fifth Item", 4)

For Each r in clsTest.Test
response.write(r & "<br>")
Next


I get an "Object reference not set to an instance of an object."
exception

on
the line 'clsTest.Test.SetValue("First Item", 0)'.

I don't understand why. WTF.


--Buddy
 
B

Buddy Ackerman

Thanks that worked for initializing an array but how do I add to an alrady initialized array? That is going to be what
I need to do most. Why doesn't System.Array have an append or add method (seems like an obvious need to me)?


--Buddy

There are definitely other options besides the one i mentioned.
Just make sure that in one way or the other, that the variable references an
instantiated array object before it is used.
Public ListOfLead() simply declares it. Using "ListOfLead(4)" instantiates
the array object (an array with 5 elements - each element is Nothing, but
the array itself is no longer Nothing).

So, another approach to your code may be to ensure that the array is
initialized from outside the class

clsTest.Test = New String() {"First Item", "Second Item", "Third Item",
"Fourth Item", "Fifth Item"}

Analogously I would use code to add a lead to the ListOfLead-array (assuming
parameter-less constructor of the classes involved).

Dim leadInput As New LeadWS_LeadInsert_Input
leadInput.ListOfLead = New Lead1() { new Lead1 }

If you want to add a new Lead1 object to the array at the end of a current
array (if an initialized and non-empty array already exists), you will have
to add some logic to handle that. (However, there are other datastructures
that
are more appropriate for this.

Hope this helps.

Tor Bådshaug
tor.badshaug [//at\\] bekk.no.

That's a little strange because the example that I posted was based on an

example class that was autogenerated from the
SOAP Proxy Class Wizard in WebMatrix. Here is the class:

<System.Xml.Serialization.XmlTypeAttribute([Namespace]:="urn:crmondemand/ws/
lead/")> _
Public Class LeadWS_LeadInsert_Input

'<remarks/>
<System.Xml.Serialization.XmlArrayAttribute([Namespace]:="urn:/crmondemand/x
ml/lead"), _

System.Xml.Serialization.XmlArrayItemAttribute([Namespace]:="urn:/crmondeman
d/xml/lead", IsNullable:=false)> _
Public ListOfLead() As Lead1
End Class



So, how would you add a lead to the ListOfLead() array?



--Buddy

When you get this runtime-error, there is some problem with an object
reference that is not initialized.
In your case, the problem is inside your "MyTestClass". The public
member
"Test()" defines a variable that
refers to an array of strings, but in your code, the array is never
initialized.
If you change your code to the listing below, your code should work just
fine.

Public Class MyTestClass
Public Test(4) As String
End Class

Hope this helps.
Tor Bådshaug
tor.badshaug [//at\\] bekk.no.



I created a simple class:

Public Class MyTestClass
Public Test() As String
End Class



I tried to assign some values to the array Test() and display them like

this:


Dim clsTest As New MyTestClass
Dim r As String

clsTest.Test.SetValue("First Item", 0)
clsTest.Test.SetValue("Second Item", 1)
clsTest.Test.SetValue("Third Item", 2)
clsTest.Test.SetValue("Fourth Item", 3)
clsTest.Test.SetValue("Fifth Item", 4)

For Each r in clsTest.Test
response.write(r & "<br>")
Next


I get an "Object reference not set to an instance of an object."
exception
on


the line 'clsTest.Test.SetValue("First Item", 0)'.

I don't understand why. WTF.


--Buddy
 
N

Nick Malik

Hello Buddy,

If you need to add to an array, why not use an ArrayList type instead. It
has the methods you seek.

--- Nick

Buddy Ackerman said:
Thanks that worked for initializing an array but how do I add to an alrady
initialized array? That is going to be what
I need to do most. Why doesn't System.Array have an append or add method
(seems like an obvious need to me)?
--Buddy

There are definitely other options besides the one i mentioned.
Just make sure that in one way or the other, that the variable references an
instantiated array object before it is used.
Public ListOfLead() simply declares it. Using "ListOfLead(4)" instantiates
the array object (an array with 5 elements - each element is Nothing, but
the array itself is no longer Nothing).

So, another approach to your code may be to ensure that the array is
initialized from outside the class

clsTest.Test = New String() {"First Item", "Second Item", "Third Item",
"Fourth Item", "Fifth Item"}

Analogously I would use code to add a lead to the ListOfLead-array (assuming
parameter-less constructor of the classes involved).

Dim leadInput As New LeadWS_LeadInsert_Input
leadInput.ListOfLead = New Lead1() { new Lead1 }

If you want to add a new Lead1 object to the array at the end of a current
array (if an initialized and non-empty array already exists), you will have
to add some logic to handle that. (However, there are other datastructures
that
are more appropriate for this.

Hope this helps.

Tor Bådshaug
tor.badshaug [//at\\] bekk.no.

That's a little strange because the example that I posted was based on
an

example class that was autogenerated from the
SOAP Proxy Class Wizard in WebMatrix. Here is the class:

<System.Xml.Serialization.XmlTypeAttribute([Namespace]:="urn:crmondemand/ws/
lead/")> _
Public Class LeadWS_LeadInsert_Input

'<remarks/>
<System.Xml.Serialization.XmlArrayAttribute([Namespace]:="urn:/crmondemand/x
ml/lead"), _

System.Xml.Serialization.XmlArrayItemAttribute([Namespace]:="urn:/crmondeman
d/xml/lead", IsNullable:=false)> _
Public ListOfLead() As Lead1
End Class



So, how would you add a lead to the ListOfLead() array?



--Buddy


Tor Bådshaug wrote:

When you get this runtime-error, there is some problem with an object
reference that is not initialized.
In your case, the problem is inside your "MyTestClass". The public
member

"Test()" defines a variable that
refers to an array of strings, but in your code, the array is never
initialized.
If you change your code to the listing below, your code should work just
fine.

Public Class MyTestClass
Public Test(4) As String
End Class

Hope this helps.
Tor Bådshaug
tor.badshaug [//at\\] bekk.no.



I created a simple class:

Public Class MyTestClass
Public Test() As String
End Class



I tried to assign some values to the array Test() and display them like

this:


Dim clsTest As New MyTestClass
Dim r As String

clsTest.Test.SetValue("First Item", 0)
clsTest.Test.SetValue("Second Item", 1)
clsTest.Test.SetValue("Third Item", 2)
clsTest.Test.SetValue("Fourth Item", 3)
clsTest.Test.SetValue("Fifth Item", 4)

For Each r in clsTest.Test
response.write(r & "<br>")
Next


I get an "Object reference not set to an instance of an object."
exception

on


the line 'clsTest.Test.SetValue("First Item", 0)'.

I don't understand why. WTF.


--Buddy
 
B

Buddy Ackerman

The class wasn't created by me it was auto generated by the WebServices Proxy Class wizard. I'm just trying to figure
out how to use it.


--Buddy


Nick said:
Hello Buddy,

If you need to add to an array, why not use an ArrayList type instead. It
has the methods you seek.

--- Nick

Thanks that worked for initializing an array but how do I add to an alrady

initialized array? That is going to be what
I need to do most. Why doesn't System.Array have an append or add method

(seems like an obvious need to me)?

references an
instantiated array object before it is used.
Public ListOfLead() simply declares it. Using "ListOfLead(4)"
instantiates
the array object (an array with 5 elements - each element is Nothing,
but
the array itself is no longer Nothing).

So, another approach to your code may be to ensure that the array is
initialized from outside the class

clsTest.Test = New String() {"First Item", "Second Item", "Third Item",
"Fourth Item", "Fifth Item"}

Analogously I would use code to add a lead to the ListOfLead-array
(assuming
parameter-less constructor of the classes involved).

Dim leadInput As New LeadWS_LeadInsert_Input
leadInput.ListOfLead = New Lead1() { new Lead1 }

If you want to add a new Lead1 object to the array at the end of a
current
array (if an initialized and non-empty array already exists), you will
have
to add some logic to handle that. (However, there are other
datastructures
that
are more appropriate for this.

Hope this helps.

Tor Bådshaug
tor.badshaug [//at\\] bekk.no.



That's a little strange because the example that I posted was based on
an
example class that was autogenerated from the


SOAP Proxy Class Wizard in WebMatrix. Here is the class:
<System.Xml.Serialization.XmlTypeAttribute([Namespace]:="urn:crmondemand/ws/
lead/")> _


Public Class LeadWS_LeadInsert_Input

'<remarks/>
<System.Xml.Serialization.XmlArrayAttribute([Namespace]:="urn:/crmondemand/x
ml/lead"), _
System.Xml.Serialization.XmlArrayItemAttribute([Namespace]:="urn:/crmondeman
d/xml/lead", IsNullable:=false)> _


Public ListOfLead() As Lead1
End Class



So, how would you add a lead to the ListOfLead() array?



--Buddy


Tor Bådshaug wrote:


When you get this runtime-error, there is some problem with an object
reference that is not initialized.
In your case, the problem is inside your "MyTestClass". The public

member


"Test()" defines a variable that
refers to an array of strings, but in your code, the array is never
initialized.
If you change your code to the listing below, your code should work
just
fine.

Public Class MyTestClass
Public Test(4) As String
End Class

Hope this helps.
Tor Bådshaug
tor.badshaug [//at\\] bekk.no.




I created a simple class:

Public Class MyTestClass
Public Test() As String
End Class



I tried to assign some values to the array Test() and display them
like
this:



Dim clsTest As New MyTestClass
Dim r As String

clsTest.Test.SetValue("First Item", 0)
clsTest.Test.SetValue("Second Item", 1)
clsTest.Test.SetValue("Third Item", 2)
clsTest.Test.SetValue("Fourth Item", 3)
clsTest.Test.SetValue("Fifth Item", 4)

For Each r in clsTest.Test
response.write(r & "<br>")
Next


I get an "Object reference not set to an instance of an object."

exception


on



the line 'clsTest.Test.SetValue("First Item", 0)'.

I don't understand why. WTF.


--Buddy
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top