ReDim语句出现问题 [英] Problem with ReDim statement

查看:79
本文介绍了ReDim语句出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,我将使用ReDim语句调整大小。对于一些我无法弄清楚的原因,我收到了以下错误:


[NullReferenceException:对象引用未设置为
对象。]


出现的代码如下:

Dim NavButton As NavButtonInfo


Dim TempButtons()As NavButtonInfo


每个按钮列表中的NavButton


如果NavButton.parentname = tempparentname然后


ReDim Preserve TempButtons(TempButtons.GetUpperBound(0)+ 1)


TempButtons(TempButtons.GetUpperBound(0))= NavButton


结束如果


下一页

此代码的ReDim行发生错误。我假设这有一些事实,即我最初声明TempButtons没有

给出它的大小,但如果我没有这个数组确切的大小(直到运行时才知道
未知)它会在我的代码中导致问题。例如,

如果我在Redim和以下行中放置10和5之类的常量,它将会超过我的代码的这一部分然后再给我一个错误在我的代码中

当它试图从尚未赋值的索引中读取时。任何

帮助将不胜感激。谢谢。

-

Nathan Sokalski
nj * *******@hotmail.com
http:// www.nathansokalski.com/

I have an Array that I will be resizing with the ReDim statement. For some
reason that I cannot figure out, I am recieving the following error:

[NullReferenceException: Object reference not set to an instance of an
object.]

The code that this occurs in is as follows:

Dim NavButton As NavButtonInfo

Dim TempButtons() As NavButtonInfo

For Each NavButton In ButtonList

If NavButton.parentname = tempparentname Then

ReDim Preserve TempButtons(TempButtons.GetUpperBound(0) + 1)

TempButtons(TempButtons.GetUpperBound(0)) = NavButton

End If

Next
The error occurs on the ReDim line of this code. I am assuming that this has
something to do with the fact that I initially declare TempButtons without
giving it''s size, but if I do not have the array the exact size (which is
unknown until runtime) it will cause problems later in my code. For example,
if I place constants like 10 and 5 in the Redim and following lines, it will
get past this section of my code and then give me an error later in my code
when it tries to read from indexes that have not been assigned values. Any
help would be appreciated. Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

推荐答案

Nathan,


Redims问题是因为没有多少人使用dotNet中的Redim来使用




这是VB6及更早版本的实用方法。例如,ArrayList或任何其他的
动态数组提供了更多的性能,值得将其中的一个用于其中之一并重新编码。 />

这里是arraylist,但是有更多的集合/数组/列表
http://msdn.microsoft.com/library/de...classtopic.asp


我希望这会有所帮助,


Cor
Nathan,

The problem with Redims problems is that there are not many people who use
the Redim in dotNet.

It real a method from VB6 and older. By instance the ArrayList or any other
dynamic array give real much more performance, that it is worth to take that
one of those and to recode a little bit.

Here the arraylist, however there are much more collections/arrays/lists
http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps,

Cor


嗨Nathan

替换行
Hi Nathan

Replace the line
Dim TempButtons()As NavButtonInfo


with


Dim TempButtons(-1)As NavButtonInfo


这将初始化没有元素的数组。


HTH

Charles

" Nathan Sokalski" < NJ ******** @ hotmail.com>写在消息中

news:ea ************** @ tk2msftngp13.phx.gbl ...我有一个数组,我将使用ReDim语句调整大小。对于一些我无法弄清楚的原因,我收到以下错误:

[NullReferenceException:对象引用未设置为
对象的实例。]

发生的代码如下:

Dim NavButton As NavButtonInfo

Dim TempButtons()As NavButtonInfo

每个NavButton In ButtonList

如果NavButton.parentname = tempparentname那么

ReDim保留TempButtons(TempButtons.GetUpperBound(0)+ 1)

TempButtons( TempButtons.GetUpperBound(0))= NavButton

结束如果

$

此代码的ReDim行发生错误。我假设这与我最初声明TempButtons
而没有给出它的大小这一事实有关,但如果我没有确切大小的数组
(其中)在运行之前是未知的)它会在我的代码中导致问题。
例如,如果我在Redim中放置像10和5这样的常量并且跟随
行,它将超过我的代码的这一部分,稍后在我的代码中尝试从尚未分配值的索引中读取时给出错误。任何帮助,将不胜感激。谢谢。
-
Nathan Sokalski
nj ******** @ hotmail.com
http://www.nathansokalski.com/
Dim TempButtons() As NavButtonInfo
with

Dim TempButtons(-1) As NavButtonInfo

This will initialise the array with no elements.

HTH

Charles
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:ea**************@tk2msftngp13.phx.gbl...I have an Array that I will be resizing with the ReDim statement. For some
reason that I cannot figure out, I am recieving the following error:

[NullReferenceException: Object reference not set to an instance of an
object.]

The code that this occurs in is as follows:

Dim NavButton As NavButtonInfo

Dim TempButtons() As NavButtonInfo

For Each NavButton In ButtonList

If NavButton.parentname = tempparentname Then

ReDim Preserve TempButtons(TempButtons.GetUpperBound(0) + 1)

TempButtons(TempButtons.GetUpperBound(0)) = NavButton

End If

Next
The error occurs on the ReDim line of this code. I am assuming that this
has something to do with the fact that I initially declare TempButtons
without giving it''s size, but if I do not have the array the exact size
(which is unknown until runtime) it will cause problems later in my code.
For example, if I place constants like 10 and 5 in the Redim and following
lines, it will get past this section of my code and then give me an error
later in my code when it tries to read from indexes that have not been
assigned values. Any help would be appreciated. Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/



你也可以尝试


Dim TempButtons()作为新的NavButtonInfo


这似乎对我有用。


-Jerry


" Charles Law" < BL *** @ nowhere.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP15.phx.gbl ...
You could also try

Dim TempButtons() As New NavButtonInfo

That seemed to work for me.

-Jerry

"Charles Law" <bl***@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
嗨Nathan

替换行
Hi Nathan

Replace the line
Dim TempButtons()As NavButtonInfo
Dim TempButtons() As NavButtonInfo





昏暗的TempButtons(-1)作为NavButtonInfo

这将初始化没有元素的数组。

HTH

Charles

Nathan Sokalski < NJ ******** @ hotmail.com>在消息中写道
新闻:ea ************** @ tk2msftngp13.phx.gbl ...



with

Dim TempButtons(-1) As NavButtonInfo

This will initialise the array with no elements.

HTH

Charles
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:ea**************@tk2msftngp13.phx.gbl...

我有一个数组,我将调整大小使用ReDim语句。对于一些我无法弄清楚的原因,我收到以下错误:

[NullReferenceException:对象引用未设置为
对象的实例。]

发生的代码如下:

Dim NavButton As NavButtonInfo

Dim TempButtons()As NavButtonInfo

每个NavButton In ButtonList

如果NavButton.parentname = tempparentname那么

ReDim保留TempButtons(TempButtons.GetUpperBound(0)+ 1)

TempButtons( TempButtons.GetUpperBound(0))= NavButton

结束如果

$

此代码的ReDim行发生错误。我假设这与我最初声明TempButtons
而没有给出它的大小这一事实有关,但如果我没有确切大小的数组
(其中)在运行之前是未知的)它会在我的代码中导致问题。
例如,如果我在Redim和
后面的行中放置10和5之类的常量,它将超出我的代码的这一部分,然后在我的代码中尝试从没有分配值的索引中读取时给我一个错误。任何帮助,将不胜感激。谢谢。
-
Nathan Sokalski
nj ******** @ hotmail.com
http://www.nathansokalski.com/
I have an Array that I will be resizing with the ReDim statement. For some
reason that I cannot figure out, I am recieving the following error:

[NullReferenceException: Object reference not set to an instance of an
object.]

The code that this occurs in is as follows:

Dim NavButton As NavButtonInfo

Dim TempButtons() As NavButtonInfo

For Each NavButton In ButtonList

If NavButton.parentname = tempparentname Then

ReDim Preserve TempButtons(TempButtons.GetUpperBound(0) + 1)

TempButtons(TempButtons.GetUpperBound(0)) = NavButton

End If

Next
The error occurs on the ReDim line of this code. I am assuming that this
has something to do with the fact that I initially declare TempButtons
without giving it''s size, but if I do not have the array the exact size
(which is unknown until runtime) it will cause problems later in my code.
For example, if I place constants like 10 and 5 in the Redim and
following lines, it will get past this section of my code and then give
me an error later in my code when it tries to read from indexes that have
not been assigned values. Any help would be appreciated. Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/




这篇关于ReDim语句出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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