在VB.net 2010中为其分配值之前,先使用变量"x" [英] Variable 'x' is used before it assigned a value in VB.net 2010

查看:95
本文介绍了在VB.net 2010中为其分配值之前,先使用变量"x"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我总是面对这个问题.为什么变量有错误?它说空引用异常可能在运行时导致.有人可以告诉我我犯了什么错误以及如何恢复我的错误.

Hi all,

I always face this problem. Why is there is an error for the variable? It say that null reference exception could result at runtime. Could someone told me what mistake that I''ve done and how to recover my mistake back.

推荐答案

您应该始终为变量分配一个值.如果您不这样做,编译器将抱怨.

如果您不想分配值,则可以分配null.
You should always assign a value to your variables. The compiler will complain if you do not do this.

In case you don''t want to assign a value you can assign a null.


这是因为您要声明变量"x"并执行在为它分配值之前先进行一些处理.如果不为变量分配值,则该值通常为null.因此,如果声明一个字符串变量,但不进行设置,然后在其上调用.ToString(),则会得到NullReferenceException.
It''s because you''re declaring a variable ''x'' and doing something with it before you''ve assigned a value to it. If you don''t assign a value to a variable it tends to be null. Therefore if you declare a string variable, but don''t set it and then call .ToString() on it, you''ll get a NullReferenceException.


这篇关于在VB.net 2010中为其分配值之前,先使用变量"x"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆