473,799 Members | 3,224 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

check array size when variable = nothing

Hi

I declared an array as follows:
dim a as integer()

somewhere else in my program I have the following command:
redim preserve a(size)

I want to check the array length like that
if a.length 1 then
.......
end if

the problem is that if I check the array length before the redim
command I get an exception:
"Object reference not set to an instance of an object."
abd thats because the array doesn't have length yet (not even 0)

so what should I do? how can check if array is equal to nothing such
in this case?
I tried: if a = nothing then ..... but it has a
compilation error

Yhlove

May 13 '07 #1
7 7091
yh****@gmail.co m wrote in news:1179061627 .545653.191900
@w5g2000hsg.goo glegroups.com:
Hi

I declared an array as follows:
dim a as integer()

somewhere else in my program I have the following command:
redim preserve a(size)

I want to check the array length like that
if a.length 1 then
.......
end if

if not a is nothing andalso a.length 0 then
...
end if

a is nothing = null.

May 13 '07 #2
Well IMHO

you would better use this syntax

Dim a() As Integer = Array.CreateIns tance(GetType(I nteger), 0)

MsgBox(a.Length )

Array.Resize(a, 2)

MsgBox(a.Length )
HTH

Michel Posseth
<yh****@gmail.c omschreef in bericht
news:11******** **************@ w5g2000hsg.goog legroups.com...
Hi

I declared an array as follows:
dim a as integer()

somewhere else in my program I have the following command:
redim preserve a(size)

I want to check the array length like that
if a.length 1 then
.......
end if

the problem is that if I check the array length before the redim
command I get an exception:
"Object reference not set to an instance of an object."
abd thats because the array doesn't have length yet (not even 0)

so what should I do? how can check if array is equal to nothing such
in this case?
I tried: if a = nothing then ..... but it has a
compilation error

Yhlove

May 13 '07 #3
On May 13, 2:32 pm, "Michel Posseth [MCP]" <msn...@posseth .comwrote:
Well IMHO

you would better use this syntax

Dim a() As Integer = Array.CreateIns tance(GetType(I nteger), 0)
Why would you use the CreateInstance method instead of this:

Dim a(0) As Integer

This seems more readable to me.

Array.Resize(a, 2)
I agree with the use of Array.Resize. It seems more readable to me
than ReDim.

Chris
May 14 '07 #4
There are subtleties as to how you 'size' an array.

Dim _a(0) As Integer
or
Dim _a() As Integer = New Integer(0) {}

both instantiate an array of integer with 1 element (index 0). That is,
interrogating _a.length will return 1.

Dim a() As Integer = Array.CreateIns tance(GetType(I nteger), 0)

instantiates an array of integer with 0 elements. That is, interrogating
_a.length will return 0.

"Chris Dunaway" <du******@gmail .comwrote in message
news:11******** **************@ p77g2000hsh.goo glegroups.com.. .
On May 13, 2:32 pm, "Michel Posseth [MCP]" <msn...@posseth .comwrote:
>Well IMHO

you would better use this syntax

Dim a() As Integer = Array.CreateIns tance(GetType(I nteger), 0)

Why would you use the CreateInstance method instead of this:

Dim a(0) As Integer

This seems more readable to me.

>Array.Resize(a , 2)

I agree with the use of Array.Resize. It seems more readable to me
than ReDim.

Chris

May 14 '07 #5
On May 14, 10:08 am, "Stephany Young" <noone@localhos twrote:
There are subtleties as to how you 'size' an array.

Dim _a(0) As Integer
or
Dim _a() As Integer = New Integer(0) {}

both instantiate an array of integer with 1 element (index 0). That is,
interrogating _a.length will return 1.

Dim a() As Integer = Array.CreateIns tance(GetType(I nteger), 0)

instantiates an array of integer with 0 elements. That is, interrogating
_a.length will return 0.
Thanks for the clarification. I was thinking that the CreateInstance
call was doing the same as the VB syntax and the 0 was the upper
bound.

Chris

May 14 '07 #6
As a follow up, I discovered you can use this syntax:

Dim a(-1) As Integer 'An array with 0 elements

But this is not as intuitive. Other possibilities are available too:

http://msdn2.microsoft.com/en-us/lib...y0(VS.80).aspx

Chris

May 14 '07 #7
yh****@gmail.co m wrote:
I declared an array as follows:
dim a as integer()
if I check the array length before the redim command I get an exception:
How about

Dim a As Integer() = {}

Who said Visual Basic is the one /without/ the curly braces?

HTH,
Phill W.
May 17 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
10186
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
9
3971
by: pvinodhkumar | last post by:
The number of elemets of the array, the array bound must be constant expression?Why is this restriction? Vinodh
4
38258
by: Mark Hannon | last post by:
I am trying to initialize an array only once so it can be seen & used by any functions that need it. As I understand it, if a variable is declared by itself outside of any functions, its scope is global and any functions should be able to access it. I have been having trouble getting this to work. In the 1st of the 2 examples below I initialize the Array "initArray" with 4 form text fields. When I try to use initArray inside either of...
1
2203
by: aemazing | last post by:
i've been tryin to do the following - -Add a new flight number to the end of the queue (got it done) -LAnd the plane at the front of the queue - problems wit it- -display the queue - got it done -seach for a specific flight number in queue ( didn't get there yet) -move a flight number one one position in the queue to another ( didn't get there yet) this is what i have so far. it runs but something is wrong and i don't know what it is.
5
14115
by: dam_fool_2003 | last post by:
Hai, I studied that the array size is fixed. But I come across a word called "variable length array". Is it possible to change the array size? So I tried the following: #include<stdio.h> #include<stdlib.h> int main(void) { int y = { 7, 9,10},i; for (;i<20;i++)
8
3688
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was static; if the input file contained more than entries, tough. This time I want to do it right - use a dynamic array that increases in size with each word read from the file. A few test programs that make use of **List and realloc( List, blah...
11
4473
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
4
7298
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving from an interface and only implementing some of it means something is wrong: either the interface specification is wrong e.g. not minimal or the derivation is wrong e.g. the type can't actually honour this contract.
7
8136
by: bowlderster | last post by:
Hello,all. I want to get the array size in a function, and the array is an argument of the function. I try the following code. /*************************************** */ #include<stdio.h> #include<stdlib.h> #include<math.h>
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10214
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6803
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5459
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5583
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2935
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.