By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,077 Members | 1,790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,077 IT Pros & Developers. It's quick & easy.

With block variable not set, error

P: n/a
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
Sep 24 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"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

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

Sep 24 '07 #2

P: n/a

"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"


Al G
Sep 24 '07 #3

P: n/a
"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

Sep 25 '07 #4

P: n/a
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.
"Al G" wrote:
>
"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"


Al G
Sep 25 '07 #5

P: n/a
Thank You. This group has been wonderful.

Al G

"Armin Zingler" <az*******@freenet.dewrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
"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

Sep 25 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.