Creating array of specific object types dynamically

Hi,

little gotcha. Some COM objects requires array of specific type.

To give you example, following DOESN'T work:
[array]$X = @()

However below code works:
[int32[]]$X = @()

For my framework, array type can be dynamic and I don't know in advance what 
type it's going to be. So what I would need is below:
[$PropertyType[]]$X = @()

This doesn't work with error message "Unable to find type".

As a workaround, I solved it using following:
Invoke-Expression "[$$PropertyType[]]`$Var = @()"

This code works as expected, I am just curious if there is any way how to 
skip Invoke-Expression??

To replicate:

[string]$PropertyType = "Int32"

# Doesn't work:
[$PropertyType[]]$Var = @()

# Works:
Invoke-Expression "[$$PropertyType[]]`$Var = @()"

Martin 

0
Martin
12/2/2009 10:02:49 AM
windows.powershell 690 articles. 0 followers. Follow

3 Replies
1481 Views

Similar Articles

[PageSpeed] 17

W3N0cmluZ10kUHJvcGVydHlUeXBlID0gIkludDMyIg0KDQojIHVzaW5nIHRoZSBBcyBvcGVyYXRv
cg0KJFZhcjEgPSBAKCkgLWFzICRQcm9wZXJ0eVR5cGUgKyAnW10nDQokVmFyMS5HZXRUeXBlKCku
TmFtZQ0KDQojIHVzaW5nIE5ldy1PYmplY3QNCiMgYXJyYXkgY29uc3RydWN0b3IgcmVxdWlyZXMg
dGhlIGVsZW1lbnQgY291bnQsIGlmIHVua25vd24sIDANCiRjb3VudCA9IDANCiRWYXIyID0gTmV3
LU9iamVjdCAoJFByb3BlcnR5VHlwZSArICdbXScpICRjb3VudA0KJFZhcjIuR2V0VHlwZSgpLk5h
bWUNCg0KLS0gDQpSb2JlcnQ=

0
Robert
12/2/2009 4:10:42 PM
[string]$PropertyType = "Int32"

# using the As operator
$Var1 = @() -as $PropertyType + '[]'
$Var1.GetType().Name

# using New-Object
# array constructor requires the element count, if unknown, 0
$count = 0
$Var2 = New-Object ($PropertyType + '[]') $count
$Var2.GetType().Name

-- 
Robert
0
Utf
12/2/2009 4:19:01 PM
Thanks Robert - I am almost sure I tried both examples below, however 
obviously I made some typos :)

Works like a charm

Martin

"Robert Robelo" <Kiron@HighPlainsDrifter.com> wrote in message 
news:E2766580-A70A-4F75-893A-4AA017C16462@microsoft.com...
> [string]$PropertyType = "Int32"
>
> # using the As operator
> $Var1 = @() -as $PropertyType + '[]'
> $Var1.GetType().Name
>
> # using New-Object
> # array constructor requires the element count, if unknown, 0
> $count = 0
> $Var2 = New-Object ($PropertyType + '[]') $count
> $Var2.GetType().Name
>
> -- 
> Robert 

0
Martin
12/2/2009 5:23:51 PM
Reply:

Similar Artilces:

Text objects
I am creating customer invoices using a query report. What is the best way to place text into the report that is not tied to a field? Examples of such text are: "Bill to:" and "Date." I tried using text boxes but, as usual, ran into problems. If I should use text boxes, how specically do I accomplish this for the text I am trying to insert? Thanks Do you mean that you want a text box with different values to appear in the report when you open it? If eg you want a filtered report to give the name of the customer it concerns, you can put a text box in it saying ...

How can I create a Mailing List?
We need to use CRM for Direct Mail purposes. I used Advanced Find to get the list of contacts that Allow Email. Is it possible to save this information in a form of mailing list for further reference? Out of box, you would have to create "views" in the System customization. If you get the SearchPaq from c360, you are allowed to save groups. Stephen P.S. The second post tonight that I mention c360's seachpaq - I don't work for them! ;-) "Galia" <Galia@discussions.microsoft.com> wrote in message news:93896DA0-5D70-4363-8EC1-1C150FD542EF@microsoft.com....

Creating an A6 booklet with 16 pages
Can anyone help me here please. I am attempting to print a 16 page A6 booklet with text/pictures on both sides on the paper in portrait mode. I am using Pub.2007 and an HP8000 printer with a duplex unit attached. I would like if possible to use stock A4 paper but if necessary I can cut it in 2 and make A5. None of the templates I have tried seems to work for me and I can't seem to find one else where. Please where can I go to find information on how to get it to print correctly or how to construct my own template. Regards Terry I use Publisher 2000 but I would think 2007 would...

const data type?
Hi, We know that 2.f denotes it's a float while 2L denotes it's a long integer. How to denote a short integer 2? I read relative inform in MSDN (const data type?) but cannot find it now. Please help. Thanks. Tony On Thu, 13 Oct 2005 17:43:42 -0700, Tony Young <jdt_young@yahoo.com> wrote: >Hi, > >We know that 2.f denotes it's a float while 2L denotes it's a long >integer. How to denote a short integer 2? There's no suffix to denote short int literals, but you can use: static_cast<short>(2) Now, the question is, why do you think you ...

cannot type in greek in publisher 2007
it enters text only in english the settings of the greek language are ok ...

Dynamics GP KB site not working
Dear All, I have been trying to access KB for 10 hours, but every time I clicked on any searched article, I got an error message saying "Our Applogise.... An error has occurred while retrieving this document." Is anyone else also experiencing the same problem with KB. Thanks, - Amjad Ali Yes. Frank Hamelly MCP-GP, MCT, MVP East Coast Dynamics www.eastcoast-dynamics.com get your gptip42today at www.gp2themax.blogspot.com Knowledge Base site is up and running again. Best regards, -- MG.- Mariano Gomez, MIS, MCP, PMP Maximum Global Business, LLC ht...

Emailing directly from Access with Send object
Emailing directly from Access with Send object command is not including default signature on stationary. If I email directly from Outlook 2007, my default siganture appears. If I email through msAccess 2002 with a command button that runs the send object command, the default signature does not appear. I have to manually select a signature. Help! Thank you! slickdock@yahoo.com wrote: >Emailing directly from Access with Send object command is not >including default signature on stationary. If I email directly from >Outlook 2007, my default siganture appears. If I email through &...

delete messages by subject on specific nformation store
We are a K12 school. We are running Ex2k3 on W2k3. We have an anti-spam solution in place that sucessfully tags the subject of spam messages as 'SUSPECT'. We have all staff mailboxes in one information store and student mailboxes in another. We want to have SUSPECT mail delivered (for review) when addressed to a mailbox in the staff information store but deleted when addressed to a mailbox in the student information store (to stop the students receiving offensive mail - even if it has been flagged as SUSPECT). I know we could create a rule on every student mailbox- but we have 1600 ...

What is the best way to create a conversion table in Excel ?
I want to create a conversion table where at the prompt of a given measured value, Excel will return the value in a (host of) new measurement(s). Example: If for gas, i want to convert 1 cubic meter into kcal, I would enter 1, choose cubic meter as my initial measurement, and then easily read off a table what this equals in kcal. see this url interestsing martin a. <martin a.@discussions.microsoft.com> wrote in message news:693B27D8-E553-4699-8295-63450807D1A7@microsoft.com... > I want to create a conversion table where at the prompt of a given measured > value, Excel will re...

Query to see what percentage of people scored a specific number
I am fairly new to access and have created a great database. However the queries are now becomming more complicated than I expected. I have two fields (Goal One and Goal Two) the nurse asks the patient have they met their goal. They use a 1-5 rating scale. 1=no, 2=sometimes, 3=likely, 4=most and 5=always. They select the number from a drop down list. I need to see how many patients were given a one, a two, a three etc.... and what percentage of patients were given a three or higher. I need to know this for goal one and goal two seperatly. There are many ways. A relatively simpl...

Create two rows based on one row
A worksheet has information in a single row. The new worksheet has columns. How can I create two rows per column in this new worksheet, based on information from the single row (key=document number, same amount for debit- and credit - but in a different row, accounts based on table) . Hi not really sure about your spreadsheet layout. Could you post an example (plain text - no attachment please) and describe your expected result based on this example -- Regards Frank Kabel Frankfurt, Germany Toni wrote: > A worksheet has information in a single row. The new worksheet has > columns. ...

repaint object not working
Hi. I've got here a strange case. I've got a main form with a chart, and a subform with a chart values. The idea is when I change the values in the subform the chart should automatically change with the new values. An it's working perfectly in Windows XP and Windows 2003 server. The problem is if I use the same database in Windopws Vista and Windows 7 the chart appears always in blank. but if I minimize access and restore/maximize the chart appears with data/lines on it. I already change the windows theme to windows classic. Same result. What can it be? ...

Hidden Objects in A2007
Hello, I imported an A2002 database into A2007 and came upon the problem of a missing table reference. I discovered that the table was hidden in the 2002 database data table and didn't get linked. I also don't see an option to display hidden objects. I thought I did but can't find the option again. Is this something new or a bug? Thanks for any help and God Bless, Mark A. Sam In Access 2007: Click the Office button, then Access Options, then Current Database, then Navigation Options, and there it is at the bottom. Obviously. "Mark A. Sam" <MarkASam@EmE...

error creating an If simple statement
I am getting the following error, and I dont know why "The expression you entered has a function containing the wrong number of arguments." Past 0-30 days: IIf([proj_ship_date]-[request_date]>=0 and IIf([proj_ship_date]-[request_date]<30,[Days Diffe],0)) Any ideas??? I am sure I am missing something... Thanks in advance! Thank you very much to both of u. That did it :-) ...

Create Table Record on Current of Form
I have a table called LastVisitedRecord. There is but one field in that table called "lvCompanyID". I'd like to use the OnCurrent event of my record form to insert the record id (CompanyID) into a new record in the LastVisitedRecord table. After that, I'd like to use a query I've created qLastVisitedRecord which only shows the top 1 record of the LastVisitedRecord table to query my contact form by default, showing all records, but skipping to the record whose CompanyID field matches the sole record in the query qLastVisitedRecord. Anybody got an idea on how to do this? ...

When I try to expand or create public folder with ESM
I have this error message "An existing Connection was forcibly closed by the remote host" Facility: win32 ID no: c0072746 Exchange System Manager Is someone knows this problem? Thanks in advance ...

Create a search folder to look at all emails in one folder and selected criteria in other folders
Hi! Is is possible to create a search folder that shows ALL the email in the Inbox while only showing the unread email is other folders and subfolders? I would like to use this as a modified Inbox. Thanks, PJW I don't think you can do that since a search folder only allows one set of criteria. <phil.whitacre@cox.net> wrote in message news:1144603155.163778.133800@z34g2000cwc.googlegroups.com... > Hi! > > Is is possible to create a search folder that shows ALL the email in > the Inbox while only showing the unread email is other folders and > subfolders? I wou...

date and time in 2 cells to create subtraction in 3rd
I have 2 cells that are in the format line of A1: 27/06/2006 9:42:00 pm B1: 7/06/2006 9:52:05 pm Want third column C1: 00:10 I wish to subtract the two cells to get the difference in time i.e 10:00 (only interested in nearest minute) Then a average at the end of the column of the the times in this 3rd column as a result of the difference between the 2 times. How would I do this? I could do it if it was just numbers but once it is in this time/dtae format I am completely lost! Thanks MIke Hi Mike, =MOD(B1,1)-MOD(A1,1) Format as Time You can use the AVERAGE function on the ...

Resize dynamically the size of the CFormView
Hello, I have a view which derives directly from the CFormView and then I display a child dialog on it at runtime. Visual Studio creates automatically resource entry for my view class. The dialog can be resied dynamically by the user by pressing the button and I would like the view to always fit the size of the dialog. At the moment when I press a button and the dialog dimensions change I cannot resize the view and a scrollbar is created, which looks really ugly. Could someone help me with this problem? How can I automatically resize the view so that it always fits the dialog window? Every...

Orphaned Objects
Hi guys ... My question is about how to remove a orphaned mailbox from a global address list in exchange 5.5 servers. This object was created on exchange 2000 server that shows in his GAL only the correct mailbox. Resume: Exchange 5.5 - 2 objects: 1 correct and 1 orphaned Exchange 2k - 1 object: 1 correct Thanks Alex "Alex" <anonymous@discussions.microsoft.com> wrote: >My question is about how to remove a orphaned mailbox from >a global address list in exchange 5.5 servers. And the object doesn't appear in the Active Directory? If the mailbox doesn'...

disabling linked object before you open a file
I have a small excel file created by people in a remote location that takes forever (more than 10 minutes) to open. the file is less than 2 mb. similarly print previews take FOREVER I am guessing its looking up, or trying to look up something on the far end of our very slow VPN. how do I prevent this updating before I attempt to open the file? while waiting for the file to open the CPU utilization maxes out and stays there which frustrates multitasking. thanks in advance The following site has downloadable afindlinks function. You might want to try it to see if there are links to the ...

Custom Report / Z Type
I need a custom report that gives me information by date in a Z-report type format. The first column would be batch and/or closing date. Then the next columns I need are: COGS Deposits made Deposits redeemed Sales (net of discounts and returns, pre-tax) Paid on account Paid to account Tax collected Over/short total Closing total (for each temder type, so multiple columns) Paid out total I checked out the Z report that is floating around out there. It is a good start, but I can't seem to modify it to suit my needs. Any help? The Combined/All Z Reports that I found on the http://ww...

Switchboard buttons not creating
I am using Access 2007. When I add a button it does not appear on the switchboard. I did delete one button, but it was not working either. How do I create a button that will appear and work properly? Thanks in advance for your advice. Did you edit the Switchboard in Design View or edit the Switchboard through Tools, Database Utilities, Switchboard Manager? Bonnie http://www.dataplus-svc.com JMD.Park wrote: >I am using Access 2007. When I add a button it does not appear on the >switchboard. I did delete one button, but it was not working either. How do I >create a ...

Creating CRM summary reports
We are using the CRM 1.2 for sales and customer service departments. Most of the reports needed by the managers are of summary type. One example is, orders by sales rep. The requirement here is to generate a report that just shows the number of cases resolved by each customer service rep for the current quarter/year. The problem I am facing is that there is no way for me to get summary of information from CRM SDK. I have to get all cases and then in crystal reports, I do a grouping by owner and hide the details tab. Though this solution works, but it ends up consuming a lot of memory ...

How do I create a combination bar and XY chart?
I'm using Excel 2002 and would like to create a horizontal bar chart with some dots (or other symbols) overlayed on top. The idea is to show an operating range with the bar, and then highlight some key operating points within that range with the dots. Jon Peltier has instructions for a Bar-Scatter graph here: http://www.peltiertech.com/Excel/Charts/BarLineCombo.html Engineer_SM wrote: > I'm using Excel 2002 and would like to create a horizontal bar chart with > some dots (or other symbols) overlayed on top. The idea is to show an > operating range with the bar, and ...