With block variable not set, error

A

Al G

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

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()

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



Thanks, Al G
 
A

Armin Zingler

Al G said:
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

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()

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


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
 
A

Al G

Armin Zingler said:
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"




Al G
 
A

Armin Zingler

Al G said:
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
 
G

Guest

Now the array is dimensioned to 20, which is good, but each array entry needs
to be initialized to a new object. The array initialization only opens the
array space.

Between the redim and using AttachBlock(1), insert this line:

AttachBlock (1) = New attachmentBlockParameter

Also, it should be noted that AttachBlock(1) is actually the second array
element. AttachBlock(0) is the first.
 

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