By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,710 Members | 1,332 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,710 IT Pros & Developers. It's quick & easy.

Array parameter instead of Object in Excel Worksheet Function

P: 10
This is my first post so I'm not sure if this should be in the .NET forum or the VB forum. Anyway I'm using VB .NET and trying to call the Percentile function. The function needs an Object as a parameter, but I'm trying to pass it an Array. Here's some of my code:

Imports Microsoft.Office.Interop.Excel

Dim worksheetFunction1 As WorksheetFunction

percentileValue = worksheetFunction1.Percentile(intenArray, inputPerValue)

I get the error System.NullReferenceException when trying to execute the Percentile function call.

Is this due to the array not being an Object data type? Is there a way to convert or cast the array (it's an array of Doubles) to an Excel Object data type or something the Percentile function will be ok with?

I'm pretty new to VB and programming in general. Any help will be appreciated.
Mar 2 '07 #1
Share this Question
Share on Google+
7 Replies

Expert 100+
P: 692

You can send the array as an Object.So ,I think that must not be giving error.Are you using dynamic array? If so , it may generate Runtime exception
if not initialised.
Mar 2 '07 #2

Expert 100+
P: 759
How you pass the array to that method ?

The usual method is,

string[] weekDays = new string[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };


Print array is a user defined method.
Mar 2 '07 #3

P: 10
How would I pass the array as an Object? Could you give me an example.

I'm pretty sure the array is initialized properly.

I'll post more of my code so you can see what's going on. I'm reading in a bitmap image, getting the brightness values of each pixel, and storing the values in an array of doubles. I then want to find a certain percentile value of the array. Rather than write my own percentile function I'm trying to use the Excel Worksheet Function. Then I make all pixels <= the percentile brightness black else all other pixels are made white.

Expand|Select|Wrap|Line Numbers
  2. Public Function posterizeImage(ByVal gImage As Bitmap) As Image
  4.         Dim pImage As Bitmap = gImage
  5.         'prep soon to be posterized image
  7.         Dim wid As Integer = gImage.Width
  8.         Dim hgt As Integer = gImage.Height
  9.         Dim totalPixels As Integer = Math.BigMul(wid, hgt) 'could be a very large number
  10.         Dim tempcolor As Color
  11.         Dim intenArray(totalPixels - 1) As Double
  12.         Dim counter As Integer = 0
  14.         'new variables for Excel percentile function
  15.         Dim inputPerValue As Double
  16.         Dim percentileValue As Double
  17.         Dim worksheetFunction1 As WorksheetFunction
  19.         'fill array intenArray with brightness (intesity) values
  20.         'brightness values range from 0 (black) to 1 (white)
  21.         For x As Integer = 0 To wid - 1
  22.             For y As Integer = 0 To hgt - 1
  23.                 tempcolor = gImage.GetPixel(x, y)
  24.                 intenArray(counter) = tempcolor.GetBrightness
  25.                 counter += 1
  26.             Next
  27.         Next
  29.         'sort the array - I don't know if this is necessary for the Excel function
  30.         intenArray.Sort(intenArray)
  32.         'find percentile value - uses Excel percentile function
  33.         inputPerValue = txtPerValue.Text / 100.00
  35.         'Excel percentile function
  36.         percentileValue = worksheetFunction1.Percentile(intenArray, inputPerValue)  
  38.         For x As Integer = 0 To wid - 1
  39.             For y As Integer = 0 To hgt - 1
  40.                 tempcolor = gImage.GetPixel(x, y)
  41.                 If tempcolor.GetBrightness <= percentileValue Then
  42.                     pImage.SetPixel(x, y, Black)
  43.                 Else
  44.                     pImage.SetPixel(x, y, White)
  45.                 End If
  46.             Next
  47.         Next
  49.         Return pImage
  50. End Function
Mar 2 '07 #4

Expert 2.5K+
P: 4,871
Either intenArray or inputPerValue are null, write values to discover which and work backwards debugging from there.
Mar 3 '07 #5

P: 10
Neither parameter is null. None of the elements in the array are null.

Here is the link to the Percentile method:

It asks for an Object and I'm giving it an array of doubles. I believe this is the problem. So what can I pass to the method so it will work?

Is there a way to make an array of Doubles as an array of Objects?
Mar 3 '07 #6

Expert 100+
P: 692

This is the way you can do it,

Dim intenarry() as Double
Redim intenarray(totalpixels-1)

Percentile(Byval o1 as Object)
'Access object as an array
End Sub
Mar 5 '07 #7

P: 10
ReDim didn't work as a workaround. I did figure it out finally though. Here is the different syntax that worked.

Found it at:

Dim xl As Object
xl = CreateObject("Excel.Application")

'rather than
'Dim worksheetFunction1 As WorksheetFunction

then use
percentileValue = xl.Percentile(intenArray, inputPerValue)

which works perfectly!
Mar 5 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.