vb.net 对象变量或 With 块变量未设置错误 Visual Basic
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16298955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Object variable or With block variable not set error Visual Basic
提问by Oprea Cristian Alexandru
The problem i'm facing is the fact that i'm trying to use a variable from a form in another form and it gives me the error "Object variable or With block variable not set". I've already tryed to use the documentation but it's preety f***-up.
我面临的问题是,我试图使用另一种形式的表单中的变量,它给了我错误“对象变量或块变量未设置”。我已经尝试使用文档,但它很漂亮 f* **-up。
I've tryed this method:
我试过这个方法:
Public urlpoza, regizor, film, blabla(0 To 9999)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
x = Form1.codfilm
Try
film(x) = TextBox1.Text
regizor(x) = TextBox2.Text
blabla(x) = TextBox3.Text
urlpoza(x) = TextBox4.Text
Form1.ListBox1.Items.Add(film(x))
Form1.ListBox1.Refresh()
Catch ex As NullReferenceException
MsgBox(ex.Message)
End Try
I've tryed to use in form 2 this:
我尝试在表格 2 中使用:
Public Shared codfilm As Form1
http://www.dreamincode.net/forums/uploads/monthly_04_2013/post-625768-136731764705.png
http://www.dreamincode.net/forums/uploads/monthly_04_2013/post-625768-136731764705.png
I've tryed to use in form 2 this too:
我也尝试在表格 2 中使用:
Public urlpoza, regizor, film, blabla(0 To 9999) As String
http://www.dreamincode.net/forums/uploads/monthly_04_2013/post-625768-136731763757.png
http://www.dreamincode.net/forums/uploads/monthly_04_2013/post-625768-136731763757.png
But i still have the same problem...Any ideeas?
但我仍然有同样的问题......有任何想法吗?
回答by Matt Wilko
You have declared filmas:
您已声明film为:
Public urlpoza, regizor, film, blabla(0 To 9999)
But then you try to assign a string to a position in the array:
但是随后您尝试将字符串分配给数组中的某个位置:
film(x) = TextBox1.Text
This can't work. fileis not an array, it is probably an Object (hence the error message). If Option Explicit and Option Strict are On - this wont compile
这行不通。file不是数组,它可能是一个对象(因此是错误消息)。如果 Option Explicit 和 Option Strict 开启 - 这不会编译
Try explicitly declaring your variables instead - something like this instead:
尝试显式声明你的变量——而不是像这样:
Public urlpoza(0 To 9999) As String, regizor(0 To 9999) As String, film(0 To 9999) As String, blabla(0 To 9999) As String

