473,545 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can't assign values to array

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.Se tValue("First Item", 0)
clsTest.Test.Se tValue("Second Item", 1)
clsTest.Test.Se tValue("Third Item", 2)
clsTest.Test.Se tValue("Fourth Item", 3)
clsTest.Test.Se tValue("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.S etValue("First Item", 0)'.

I don't understand why. WTF.
--Buddy
Jul 21 '05 #1
6 1724
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 "MyTestClas s". 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" <bu**********@b uddyackerman.co m> wrote in message
news:Of******** ******@TK2MSFTN GP11.phx.gbl...
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.Se tValue("First Item", 0)
clsTest.Test.Se tValue("Second Item", 1)
clsTest.Test.Se tValue("Third Item", 2)
clsTest.Test.Se tValue("Fourth Item", 3)
clsTest.Test.Se tValue("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.S etValue("First Item", 0)'.

I don't understand why. WTF.
--Buddy

Jul 21 '05 #2
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.Ser ialization.XmlT ypeAttribute([Namespace]:="urn:crmondem and/ws/lead/")> _
Public Class LeadWS_LeadInse rt_Input

'<remarks/>
<System.Xml.Ser ialization.XmlA rrayAttribute([Namespace]:="urn:/crmondemand/xml/lead"), _
System.Xml.Seri alization.XmlAr rayItemAttribut e([Namespace]:="urn:/crmondemand/xml/lead", IsNullable:=fal se)> _
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 "MyTestClas s". 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" <bu**********@b uddyackerman.co m> wrote in message
news:Of******** ******@TK2MSFTN GP11.phx.gbl...
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.Se tValue("First Item", 0)
clsTest.Test.Se tValue("Second Item", 1)
clsTest.Test.Se tValue("Third Item", 2)
clsTest.Test.Se tValue("Fourth Item", 3)
clsTest.Test.Se tValue("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.S etValue("First Item", 0)'.

I don't understand why. WTF.
--Buddy


Jul 21 '05 #3
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_LeadInse rt_Input
leadInput.ListO fLead = 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" <bu**********@b uddyackerman.co m> wrote in message
news:#1******** ******@TK2MSFTN GP15.phx.gbl...
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.Ser ialization.XmlT ypeAttribute([Namespace]:="urn:crmondem and/ws/
lead/")> _ Public Class LeadWS_LeadInse rt_Input

'<remarks/>
<System.Xml.Ser ialization.XmlA rrayAttribute([Namespace]:="urn:/crmondemand/x
ml/lead"), _ System.Xml.Seri alization.XmlAr rayItemAttribut e([Namespace]:="urn:/crmondeman
d/xml/lead", IsNullable:=fal se)> _ 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 "MyTestClas s". 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" <bu**********@b uddyackerman.co m> wrote in message
news:Of******** ******@TK2MSFTN GP11.phx.gbl...
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.Se tValue("First Item", 0)
clsTest.Test.Se tValue("Second Item", 1)
clsTest.Test.Se tValue("Third Item", 2)
clsTest.Test.Se tValue("Fourth Item", 3)
clsTest.Test.Se tValue("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.S etValue("First Item", 0)'.

I don't understand why. WTF.
--Buddy


Jul 21 '05 #4
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_LeadInse rt_Input
leadInput.ListO fLead = 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" <bu**********@b uddyackerman.co m> wrote in message
news:#1******** ******@TK2MSFTN GP15.phx.gbl...
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.Ser ialization.XmlT ypeAttribute([Namespace]:="urn:crmondem and/ws/
lead/")> _
Public Class LeadWS_LeadInse rt_Input

'<remarks/>

<System.Xml.Ser ialization.XmlA rrayAttribute([Namespace]:="urn:/crmondemand/x
ml/lead"), _

System.Xml.Seri alization.XmlAr rayItemAttribut e([Namespace]:="urn:/crmondeman
d/xml/lead", IsNullable:=fal se)> _
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 "MyTestClas s". The public
member
"Test()" defines a variable that
refers to an array of strings, but in your code, the array is never
initialize d.
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.badsha ug [//at\\] bekk.no.

"Buddy Ackerman" <bu**********@b uddyackerman.co m> wrote in message
news:Of***** *********@TK2MS FTNGP11.phx.gbl ...
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.Se tValue("First Item", 0)
clsTest.Test.Se tValue("Second Item", 1)
clsTest.Test.Se tValue("Third Item", 2)
clsTest.Test.Se tValue("Fourth Item", 3)
clsTest.Test.Se tValue("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.S etValue("First Item", 0)'.

I don't understand why. WTF.
--Buddy


Jul 21 '05 #5
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" <bu**********@b uddyackerman.co m> wrote in message
news:ue******** ******@TK2MSFTN GP15.phx.gbl...
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_LeadInse rt_Input
leadInput.ListO fLead = 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" <bu**********@b uddyackerman.co m> wrote in message
news:#1******** ******@TK2MSFTN GP15.phx.gbl...
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.Ser ialization.XmlT ypeAttribute([Namespace]:="urn:crmondem and/ws/ lead/")> _
Public Class LeadWS_LeadInse rt_Input

'<remarks/>

<System.Xml.Ser ialization.XmlA rrayAttribute([Namespace]:="urn:/crmondemand/x ml/lead"), _

System.Xml.Seri alization.XmlAr rayItemAttribut e([Namespace]:="urn:/crmondeman d/xml/lead", IsNullable:=fal se)> _
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 "MyTestClas s". The public


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

justfine.

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

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

"Buddy Ackerman" <bu**********@b uddyackerman.co m> wrote in message
news:Of***** *********@TK2MS FTNGP11.phx.gbl ...
>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.Se tValue("First Item", 0)
> clsTest.Test.Se tValue("Second Item", 1)
> clsTest.Test.Se tValue("Third Item", 2)
> clsTest.Test.Se tValue("Fourth Item", 3)
> clsTest.Test.Se tValue("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.S etValue("First Item", 0)'.
>
>I don't understand why. WTF.
>
>
>--Buddy


Jul 21 '05 #6
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" <bu**********@b uddyackerman.co m> wrote in message
news:ue******** ******@TK2MSFTN GP15.phx.gbl...
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
instantiat ed 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
initialize d from outside the class

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

Analogousl y 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_LeadInse rt_Input
leadInput.Li stOfLead = 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.badsha ug [//at\\] bekk.no.

"Buddy Ackerman" <bu**********@b uddyackerman.co m> wrote in message
news:#1***** *********@TK2MS FTNGP15.phx.gbl ...
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.Ser ialization.XmlT ypeAttribute([Namespace]:="urn:crmondem and/ws/
lead/")> _
Public Class LeadWS_LeadInse rt_Input

'<remarks/>
<System.Xml.Ser ialization.XmlA rrayAttribute([Namespace]:="urn:/crmondemand/x
ml/lead"), _
System.Xml.Seri alization.XmlAr rayItemAttribut e([Namespace]:="urn:/crmondeman
d/xml/lead", IsNullable:=fal se)> _
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
>referenc e that is not initialized.
>In your case, the problem is inside your "MyTestClas s". The public

member
>"Test()" defines a variable that
>refers to an array of strings, but in your code, the array is never
>initialize d.
>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.badsha ug [//at\\] bekk.no.
>
>"Buddy Ackerman" <bu**********@b uddyackerman.co m> wrote in message
>news:Of*** ***********@TK2 MSFTNGP11.phx.g bl...
>
>
>
>>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.Se tValue("First Item", 0)
>> clsTest.Test.Se tValue("Second Item", 1)
>> clsTest.Test.Se tValue("Third Item", 2)
>> clsTest.Test.Se tValue("Fourth Item", 3)
>> clsTest.Test.Se tValue("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.S etValue("First Item", 0)'.
>>
>>I don't understand why. WTF.
>>
>>
>>--Buddy
>
>
>


Jul 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3840
by: Greg Baker | last post by:
I don't know what standard protocol is in this newsgroup. Am I allowed to post code and ask for help? I hope so.. :) Here's my problem: I am trying problem 127 of the valladolid online contests (http://online-judge.uva.es/p/v1/127.html). The program I wrote seems to work fine, but it takes way too much memory to run. I am not that...
14
4324
by: Randell D. | last post by:
Folks, Here's my problem: I am createing an array in a file using the reslut of some PHP and MySQL processing. An example of my array would be examlpe="example one"; examlpe="example two"; examlpe="example three";
9
2293
by: Timothy Madden | last post by:
Hello If I write a function like this void Process(double Data) { ... } it is ok, but if I try class DataProcess { double (&Data); DataProcess(double Data)
8
3668
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was static; if the input file contained more than entries, tough. This time I want to do it right - use a dynamic array that increases in size with...
33
3124
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
26
7048
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of the structure inside the array. When I try this, an error about late assignment appears. Is it possible to assign a value to a structure field...
6
1454
by: Buddy Ackerman | last post by:
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:
3
17398
by: roopa.v1 | last post by:
Hi, How to assign long to character array and later extract it
3
27901
by: bharathi228 | last post by:
hi, how to assign a array as datasource to datagridview in vb.net. my problem is iam having an array with 60 values
0
7420
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7934
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7446
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7778
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4966
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
731
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.