"Al G" <ag*******@nospam.charter.netschrieb
>
"Armin Zingler" <az*******@freenet.dewrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
"Al G" <ag*******@nospam.charter.netschrieb
Hi,
>
I'm converting a bit of POP3 VB6 code to VB2005, and have run
into this error with the following code.
>
Can someone help me find out what I'm missing/doing wrong?
>
'holds the attachments
>
snip for brevity, see below...
>
Console.Read()
>
End Sub
I can not explain /this/ error because there is no With statement
in your code. If you got an IndexOutOfRange exception, I would
recommend to create an array that is not empty (Redim statement).
Currently, you have an empty array, but you are trying to access
the first item in the array.
Armin
Thank you for taking the time, this is all a little new for me. I
went back,
and added a redim statement,
Module Module1
'holds the attachments
Class attachmentBlockParameter
Public cTypeAttach As String
Public cEncoding As String
Public cFileName As String
Public cAttachment As String
End Class
Public AttachBlock() = New attachmentBlockParameter() {}
Sub Main()
ReDim AttachBlock(20)
AttachBlock(1).cTypeAttach = "Base64"
' The previous line generates an "Object variable or With
' block variable not set." Error
Console.WriteLine(" Attach Block Type=" &
AttachBlock(1).cTypeAttach)
Console.Read()
End Sub
End Module
The actual error is:
System.NullReferenceException was unhandled
Message="Object variable or With block variable not set."
Source="Microsoft.VisualBasic"
First, enable Option Strict and correct the syntax error. One valid syntax
is
Public AttachBlock As attachmentBlockParameter()
This declares an array variable but it doesn't create an array, yet. The
array is created by the ReDim statement and the reference to the array is
stored in variable AttachBlock.
After using Redim, you can store 21 references (indexes 0 to 20) in the
array. Each item can point to an object of type attachmentBlockParameter.
Currently, all 21 items don't contain references. They are "Nothing". You
must create an object and store the object reference in the array:
AttachBlock(1) = New attachmentBlockParameter
After this you can store values in the object's properties:
AttachBlock(1).cTypeAttach = "Base64"
Note that only the item at index position 1 points to an object. All other
items in the array are still Nothing. You'd have to create objects in a loop
and store them - to be exact, only the object references - in the array.
(Also note that the length of an array can not be changed. If you want to
keep the number of items dynamic, you can also use an Arraylist instead. Or,
starting with VB 2005, there is also a generic List.)
Armin