473,466 Members | 1,331 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Fill an array dymanically

I know this may seem like a silly question, but I can't figure it out!

I've made my own object, which has five properties. When I instantiate this object, the new constructor is passed these values. I need to create a dynamic array of these objects, and I don't know how many could be created.

Basically I want to do this (which doesn't work..) "TemplateSearch" is my object

....
Private search() As TemplateSearch
Private searchCount As Integer = 0

For each blah in myBlahs
search(searchCount) = new TemplateSearch(f1, f2, f3, f4, f5)
searchCount = searchCount + 1
Next
....

I get the dreadful "Object reference not set to an instance of an object.",

I know I can put a number when I declare my object, "Private search(100) as TemplateSearch", but that seems like a cop-out, and if I ever insert more than this number, it'll break. Can someone help me out on this?

Thanks!
--Michael

Nov 20 '05 #1
3 937
Raterus wrote:
I know this may seem like a silly question, but I can't figure it out!

I've made my own object, which has five properties. When I instantiate this object, the new constructor is passed these values. I need to create a dynamic array of these objects, and I don't know how many could be created.

Basically I want to do this (which doesn't work..) "TemplateSearch" is my object

...
Private search() As TemplateSearch
Private searchCount As Integer = 0

For each blah in myBlahs
search(searchCount) = new TemplateSearch(f1, f2, f3, f4, f5)
searchCount = searchCount + 1
Next
...


Ever thought about dimensioning your array before using it?

\\\
ReDim search(whatevernumberyouneed)

For each blah in myBlahs
search(searchCount) = new TemplateSearch(f1, f2, f3, f4, f5)
searchCount = searchCount + 1
Next
///

--
Konrad -
http://madrat.net/
Nov 20 '05 #2
* "Raterus" <ra*****@spam.org> scripsit:
I've made my own object, which has five properties. When I instantiate this object, the new constructor is passed these values. I need to create a dynamic array of these objects, and I don't know how many could be created.

Basically I want to do this (which doesn't work..) "TemplateSearch" is my object

...
Private search() As TemplateSearch
Private searchCount As Integer = 0

For each blah in myBlahs
search(searchCount) = new TemplateSearch(f1, f2, f3, f4, f5)
searchCount = searchCount + 1
Next
...


Have a look in the documentation for 'ReDim Preserve'. You can use it
to change the size of the array. Notice that this may dramatically
reduce the performance if you increase the size of the array in every
iteration of the loop. Instead, you can create an array with maximum
size (maybe you have to determine an upper bound using a heuristic),
assign the object to the element the counter is pointing to, and after
assigning all objects, perform a 'ReDim Preserve' with the number of
items in the array - 1.

Alternatively, you can use a more dynamic data structure to store your
items, like an 'ArrayList', or a 'Collection'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3
Hi Raterus,

In addition to Herfried,

Have a look for Ilist for the collections and in my idea not for the VB
collection, one of the few parts from the Microsoft.Visual Basic namespace
which I advice not to use.

http://msdn.microsoft.com/library/de...classtopic.asp

Cor
Nov 20 '05 #4

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

Similar topics

5
by: Fabio Papa | last post by:
Hi, I am fairly new to programming and and even newer to dotnet. I appoligise in advance if this is a dumb questions, and I would appreciate if you could answer it anyways. :) I am writing a...
5
by: Joe C | last post by:
I'm a hobbiest, and made the forray into c++ from non-c type languages about a year ago. I was "cleaning up" some code I wrote to make it more "c++ like" and have a few questions. I'm comfortable...
14
by: Gattaca | last post by:
I would like to create a matrix of integers by allocating memory dynamically (malloc or calloc) because i and j are defined during execution of the program. I have got not problem to do this in...
19
by: Alex | last post by:
Hello list This question has probably already been asked, but let me ask again I have a mysql database to which I connect with my php scripts. The database contains articles. Name, Unit_Price...
14
by: romayankin | last post by:
Hello All, I'm writing cross-platform code so i'm bound to standards. Here is the code I have: ~~~~~~~~~~~~~~~~~~ double **mx = new double*; for(int i = 0; i < col - 1; i++) { mx = new...
2
by: cpisz | last post by:
I saw that using std::fill was the way to go for setting all elements of an array to some value in one foul swoop. However when I tryed it I am getting an error. Can I only use this for vectors...
20
by: silverburgh.meryl | last post by:
In my code, I have an array of char* pointer which is populated statically: void function1() { char *ppsz_argv2 = { "abc" , "def", "dummy"}; //... }
0
by: dprjessie | last post by:
Hello, I am a Web programmer and I'm working on my first desktop application as a favor for a friend. I'm sure I have a stupid error here, but there is no error being thrown so I can't figure out...
1
by: bytesFTW99 | last post by:
I have been struggling with this for some time can anyone help out? just trying to have 3 dropdown boxes that fill depending on what is selected, then in some cases click a button and have the second...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
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...
0
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 ...

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.