473,396 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Array parameter instead of Object in Excel Worksheet Function

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
7 2714
shweta123
692 Expert 512MB
Hi,

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
radcaesar
759 Expert 512MB
How you pass the array to that method ?

The usual method is,

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

PrintArray(weekDays);

Print array is a user defined method.
Mar 2 '07 #3
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
  1.  
  2. Public Function posterizeImage(ByVal gImage As Bitmap) As Image
  3.  
  4.         Dim pImage As Bitmap = gImage
  5.         'prep soon to be posterized image
  6.  
  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
  13.  
  14.         'new variables for Excel percentile function
  15.         Dim inputPerValue As Double
  16.         Dim percentileValue As Double
  17.         Dim worksheetFunction1 As WorksheetFunction
  18.  
  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
  28.  
  29.         'sort the array - I don't know if this is necessary for the Excel function
  30.         intenArray.Sort(intenArray)
  31.  
  32.         'find percentile value - uses Excel percentile function
  33.         inputPerValue = txtPerValue.Text / 100.00
  34.  
  35.         'Excel percentile function
  36.         percentileValue = worksheetFunction1.Percentile(intenArray, inputPerValue)  
  37.  
  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
  48.  
  49.         Return pImage
  50. End Function
  51.  
  52.  
Mar 2 '07 #4
kenobewan
4,871 Expert 4TB
Either intenArray or inputPerValue are null, write values to discover which and work backwards debugging from there.
Mar 3 '07 #5
Neither parameter is null. None of the elements in the array are null.

Here is the link to the Percentile method:
http://msdn2.microsoft.com/en-us/lib...le(VS.80).aspx

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
shweta123
692 Expert 512MB
Hi,

This is the way you can do it,

Dim intenarry() as Double
Redim intenarray(totalpixels-1)
WorksheetFunction1.Percentile(intenarray)


Percentile(Byval o1 as Object)
'Access object as an array
End Sub
Mar 5 '07 #7
ReDim didn't work as a workaround. I did figure it out finally though. Here is the different syntax that worked.

Found it at:
http://p2p.wrox.com/topic.asp?TOPIC_ID=41465

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

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

Similar topics

2
by: Kumar | last post by:
Hi Folks, I have a question regarding my windows c# application. This application just reads MS Excel file and puts the data in to sql server database. In that excel file ,it has one named cell...
4
by: msnnews.msn.com | last post by:
hi there, i've got a form that populates a datagrid, and a button that calls a function to export to an excel file. All is well with the export, no errors are returned, but the Excel instance...
5
by: Tim Frawley | last post by:
I created a .NET Com Class object for use in ASP reports to export database results directly to Excel. I have it all working just find but I cannot get the Excel process to go away after the job...
7
by: MIKQ | last post by:
Hi, ich habe ein eindimesionales Array dessen ca. 5 Werte (01234) ich in _eine_ Zelle schreiben will. Um in Excel zu schreiben habe ich folgende Sub geschrieben: Private Sub...
14
by: Peter | last post by:
Is there a fast way to move data from DataTable into double array, or do I have to spin through every record and column? I have five tables and I need to merge these tables column wise, each table...
0
by: blainegray | last post by:
Greetings This is one of those Access is not closing Excel problems. The first time through the code works fine. The second time there is a problem. After lots of combinations, I finally...
0
by: =?Utf-8?B?Y29uc3RhbnRpbg==?= | last post by:
How to build an array to make it easy to read the data from Excel worksheet table then load the data into oracle.The table looks like this have 4 columns:1-entity name with two...
4
by: =?Utf-8?B?Y29uc3RhbnRpbg==?= | last post by:
I know my question seems easy,how to build an array to read the data from Excel worksheet table then load the data into oracle.The table(employee) looks like this have 4 columns:1-entity name with...
0
by: =?Utf-8?B?c3Bhcmtlcg==?= | last post by:
Has anybody had the occasion to to build a VB.NET 2008 application that will manipulate an Excel workbook including the Hyperion Essbase Plugin? I was not sure which forum to post this question in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...

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.