Initializing Array & Assigning value to everything in it in VB 2005

Schadenfroh

Elite Member
Mar 8, 2003
38,416
4
0
Writing a graphical interface for a console application on sourceforge in VB.NET 2005 for personal use, I am more of a c++ kind of guy and I have not messed with VB since VB 6 (with a little VBA thrown around for access's sake). I even caught myself priming a certain VB loop the other day:eek:

Anyway, I am trying to create an array of processes.

in the main form's declarations I have the global variable:

Dim prcRipper(99) as process

I then try to assign it to an executable when the form loads by doing this, so that I might call for its execution later:

Dim i As Integer = 0
For i = 0 To 99
prcRipper(i).StartInfo.FileName = "theexecutable.exe"
Next i

error says that "NullReferenceException was unhandled"
Object reference not set to an instance of an object

Normally I would use the "new" command to handle this, but I cant seem to figure out the syntax in regard to an array of this unique nature.

Everything works fine when I use the tool at form view to create the process object and map it using the properties window, but I do not want to do that 100 times when I could use an array.

Update: I figured it out, see my reply later in the thread
 

clamum

Lifer
Feb 13, 2003
26,252
403
126
Would this work?

Dim i As Integer = 0
For i = 0 To 99
Dim prcRipper(i) As New Process()
prcRipper(i).StartInfo.FileName = "theexecutable.exe"
Next i

Also, maybe you'd want to use C# instead of VB since its syntax is closer to C++.
 

Schadenfroh

Elite Member
Mar 8, 2003
38,416
4
0
Cant use the new command with arrays, but thanks for the try, when I remove it I get the error " Object reference not set to an instance of an object." pointing to line: "prcRipper(i).StartInfo.FileName = "theexecutable.exe"


I have never programmed in C#, VB has changed a good deal since VB6, man I am a dinosaur, but still this is easier than making multiple windows forms in C++.
 

ApacheChiefx

Junior Member
Apr 5, 2007
18
0
0
Move to C#, it's a lot easier.

You'd probably end up with this code in C# (psuedo code)
for(int i=0;i<99;i++)
{
Process[] tempProcess = new Process();
tempProcess.StartInfo.FileName = "theexecutable.exe";
}

I have never really used the Process class, so I can't tell exactly what you're trying to do.
 

Schadenfroh

Elite Member
Mar 8, 2003
38,416
4
0
I am trying to declare an array to use them as objects of processes that I will call. I have a download client (console application) from sourceforge that I wish to run in batches of up to 100 windows (but, I have the process property set to hidden, so I only have one window) and I have a front end for various options and viewing / changing the status of each of the open console windows.
 

Jaxidian

Platinum Member
Oct 22, 2001
2,230
0
71
twitter.com
Originally posted by: Schadenfroh
Writing a graphical interface for a console application on sourceforge in VB.NET 2005 for personal use, I am more of a c++ kind of guy and I have not messed with VB since VB 6 (with a little VBA thrown around for access's sake). I even caught myself priming a certain VB loop the other day:eek:

Anyway, I am trying to create an array of processes.

in the main form's declarations I have the global variable:

Dim prcRipper(99) as process

I then try to assign it to an executable when the form loads by doing this, so that I might call for its execution later:

Dim i As Integer = 0
For i = 0 To 99
prcRipper(i).StartInfo.FileName = "theexecutable.exe"
Next i

error says that "NullReferenceException was unhandled"
Object reference not set to an instance of an object

Normally I would use the "new" command to handle this, but I cant seem to figure out the syntax in regard to an array of this unique nature.

Everything works fine when I use the tool at form view to create the process object and map it using the properties window, but I do not want to do that 100 times when I could use an array.

I suck at VB but I believe I see the problem (at least what's causing your exception). (and as previously mentioned, if you're a C++ guy, use C# and not VB - it'll be tons more intuitive and easier to get help here)

So the problem.....

The exception you're getting is most likely being thrown on the line of code:
prcRipper(i).StartInfo.FileName = "theexecutable.exe"

The reason is this - you have prcRipper which is an array of process objects (I think), however, NONE of them have been initialized yet so they are effectively "Nothing" (VB's version of NULL). It's the same as if you were to have the following code:
string s = null;
s.ToUpper();
The s.ToUpper() would fail because s is null.

Now personally, what I would do here would be to use a Generic list. The C# syntax would be something like this (underscores = my way of tabbing so ignore them):
List<Process> prcRipper = new List<Process>(); // now we have an empty list that can grow

for (int i = 0; i < 99; i++)
{
____Process p = new Process(); // your constructor - pass in appropriate arguments
____p.StartInfo.FileName = "theexecutable.exe";
____prcRipper.Add(p);
}

// if you want to return the array then the following line would work but I think you get what I mean...
return prcRipper.ToArray(); // converts the list into an array of your Process type

Hope that helps! :)

-Jax
 

Schadenfroh

Elite Member
Mar 8, 2003
38,416
4
0
Thanks for all of your help guys!

I had to get some car work done today, so I spent the better part of the morning with just me and my notebook computer in the waiting room, probably freaked some people out that were in there with me (I usually talk to myself when I am working).


Anyways, problem solved! If any of you need to setup a process array in VB.NET 2005, here is how you do it.