Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Can't assign values to array

Reply
Thread Tools

Can't assign values to array

 
 
Buddy Ackerman
Guest
Posts: n/a
 
      12-18-2004
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
 
Reply With Quote
 
 
 
 
Tor Bådshaug
Guest
Posts: n/a
 
      12-18-2004
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.

"Buddy Ackerman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
 
Buddy Ackerman
Guest
Posts: n/a
 
      12-18-2004
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


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.
>
> "Buddy Ackerman" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>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

>
>
>

 
Reply With Quote
 
Tor Bådshaug
Guest
Posts: n/a
 
      12-18-2004
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" <(E-Mail Removed)> wrote in message
news:#(E-Mail Removed)...
> 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.
> >
> > "Buddy Ackerman" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >
> >>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

> >
> >
> >



 
Reply With Quote
 
Buddy Ackerman
Guest
Posts: n/a
 
      12-18-2004
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


Tor Bådshaug wrote:
> 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" <(E-Mail Removed)> wrote in message
> news:#(E-Mail Removed)...
>
>>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.
>>>
>>>"Buddy Ackerman" <(E-Mail Removed)> wrote in message
>>>news:(E-Mail Removed)...
>>>
>>>
>>>>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
>>>
>>>
>>>

>
>

 
Reply With Quote
 
Nick Malik
Guest
Posts: n/a
 
      12-19-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
> Tor Bådshaug wrote:
> > 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" <(E-Mail Removed)> wrote in message
> > news:#(E-Mail Removed)...
> >
> >>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.
> >>>
> >>>"Buddy Ackerman" <(E-Mail Removed)> wrote in message
> >>>news:(E-Mail Removed)...
> >>>
> >>>
> >>>>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
> >>>
> >>>
> >>>

> >
> >



 
Reply With Quote
 
Buddy Ackerman
Guest
Posts: n/a
 
      12-19-2004
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 Malik wrote:
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>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
>>
>>
>>Tor Bådshaug wrote:
>>
>>>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" <(E-Mail Removed)> wrote in message
>>>news:#(E-Mail Removed)...
>>>
>>>
>>>>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.
>>>>>
>>>>>"Buddy Ackerman" <(E-Mail Removed)> wrote in message
>>>>>news:(E-Mail Removed). ..
>>>>>
>>>>>
>>>>>
>>>>>>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
>>>>>
>>>>>
>>>>>
>>>

>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie: assign values/array to variables Mark V Ruby 4 05-09-2007 11:13 AM
for loop is used to assign values to textboxed? =?Utf-8?B?ZGF2aWQ=?= ASP .Net 6 04-04-2006 09:07 PM
How do I assign values to a property in a custom control that has a dropdown style Kay ASP .Net 0 02-04-2006 11:45 AM
Preferred Method To Assign Session Variable Values Matt ASP .Net 1 02-11-2005 09:07 PM
How to Assign Paramer & Values to a hyperlink column in a datagrid =?Utf-8?B?WVNSYW8=?= ASP .Net 1 05-31-2004 10:02 AM



Advertisments