Storing lots of objects in a variable in VB.NET

Encode Data Matrix ECC200 in VB.NET Storing lots of objects in a variable

15.4 Storing lots of objects in a variable
Encode Data Matrix 2d Barcode In Visual Basic .NET
Using Barcode generation for Visual Studio .NET Control to generate, create ECC200 image in .NET framework applications.
www.OnBarcode.com
DataMatrix Recognizer In Visual Basic .NET
Using Barcode decoder for .NET framework Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
At this point, we ve been working with variables that contain a single object, and those objects have all been simple values. We ve worked directly with the object itself, rather than with properties or methods. Now, let s try putting a bunch of objects into a variable. One way to do so is to use a comma-separated list, because PowerShell recognizes those lists as collections of objects:
Code 39 Generation In VB.NET
Using Barcode generation for .NET framework Control to generate, create Code 39 image in .NET applications.
www.OnBarcode.com
Create ECC200 In VB.NET
Using Barcode creation for VS .NET Control to generate, create Data Matrix ECC200 image in .NET applications.
www.OnBarcode.com
PS C:\> $computers = 'SERVER-R2','SERVER1','localhost' PS C:\> $computers SERVER-R2 SERVER1 Localhost
UCC - 12 Drawer In Visual Basic .NET
Using Barcode drawer for .NET framework Control to generate, create UPC-A image in VS .NET applications.
www.OnBarcode.com
PDF 417 Generator In Visual Basic .NET
Using Barcode maker for VS .NET Control to generate, create PDF417 image in .NET framework applications.
www.OnBarcode.com
Notice that I was careful to put the commas outside the quotation marks. If I d put them inside, I would have had a single object that included commas and three computer names. This way, I get three distinct objects, all of which are String types. As you can see, when I examined the contents of the variable, PowerShell displayed each object on its own line.
UCC-128 Generation In Visual Basic .NET
Using Barcode generation for .NET framework Control to generate, create EAN 128 image in VS .NET applications.
www.OnBarcode.com
Make Delivery Point Barcode (DPBC) In Visual Basic .NET
Using Barcode drawer for Visual Studio .NET Control to generate, create USPS POSTNET Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Storing lots of objects in a variable
Painting DataMatrix In Objective-C
Using Barcode maker for iPhone Control to generate, create Data Matrix 2d barcode image in iPhone applications.
www.OnBarcode.com
ECC200 Printer In None
Using Barcode generator for Software Control to generate, create Data Matrix ECC200 image in Software applications.
www.OnBarcode.com
You can also access individual elements, one at a time. To do so, specify an index number for the object you want, in square brackets. The first object is always at index number 0, the second is at index number 1, and so forth. You can also use an index of -1 to access the last object, -2 for the next-to-the-last object, and so on.
PDF-417 2d Barcode Creator In None
Using Barcode creator for Software Control to generate, create PDF-417 2d barcode image in Software applications.
www.OnBarcode.com
Scan ECC200 In C#.NET
Using Barcode decoder for .NET framework Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
PS C:\> $computers[0] SERVER-R2 PS C:\> $computers[1] SERVER1 PS C:\> $computers[-1] localhost PS C:\> $computers[-2] SERVER1
Barcode Encoder In Java
Using Barcode drawer for BIRT reports Control to generate, create Barcode image in Eclipse BIRT applications.
www.OnBarcode.com
Draw ANSI/AIM Code 128 In VS .NET
Using Barcode generator for ASP.NET Control to generate, create Code-128 image in ASP.NET applications.
www.OnBarcode.com
The variable itself has a property that lets you see how many objects are in it:
Encoding GTIN - 12 In None
Using Barcode encoder for Font Control to generate, create UPC Symbol image in Font applications.
www.OnBarcode.com
Read Barcode In VS .NET
Using Barcode scanner for .NET framework Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
PS C:\> $computers.count 3
Paint GS1-128 In Java
Using Barcode creation for Java Control to generate, create UCC - 12 image in Java applications.
www.OnBarcode.com
Decoding PDF-417 2d Barcode In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Beyond that special property, you can access the properties and methods of the objects inside the variable as if they were properties and methods of the variable itself. This is a bit easier to see, at first, with a variable that contains a single object:
Printing Barcode In Java
Using Barcode encoder for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
GS1 - 12 Generation In .NET Framework
Using Barcode creation for Reporting Service Control to generate, create UPC-A Supplement 5 image in Reporting Service applications.
www.OnBarcode.com
PS C:\> $computername.length 9 PS C:\> $computername.toupper() SERVER-R2 PS C:\> $computername.tolower() server-r2 PS C:\> $computername.replace('R2','2008') SERVER-2008 PS C:\> $computername SERVER-R2
Here, I m using the $computername variable that I created earlier in the chapter. If you remember, that variable contained an object of the type System.String, and you should have seen the complete list of properties and methods of that type when you piped a string to Gm. I ve used the Length property, as well as the ToUpper(), ToLower(), and Replace() methods. In each case, I have to follow the method name with parentheses, even though neither ToUpper() nor ToLower() require any parameters inside those parentheses. Also, none of these methods changed what was in the variable you can see that on the last line. Instead, each method created a new String based on the original one, as modified by the method. When a variable contains multiple objects, this gets a bit trickier. Even if every object inside the variable is of the same type, as is the case with my $computers variable, you can t call a method, or access a property, on multiple objects at the same time. If you try to do so, you ll get an error:
PS C:\> $computers.toupper() Method invocation failed because [System.Object[]] doesn't contain a metho d named 'toupper'.
Variables: a place to store your stuff
At line:1 char:19 + $computers.toupper <<<< () + CategoryInfo : InvalidOperation: (toupper:String) [], Runt imeException + FullyQualifiedErrorId : MethodNotFound
Instead, you have to specify which object within the variable you want, and then access a property or execute a method:
PS C:\> $computers[0].tolower() server-r2 PS C:\> $computers[1].replace('SERVER','CLIENT') CLIENT1
Again, these methods are producing new strings, not changing the ones inside the variable. You can test that by examining the contents of the variable:
PS C:\> $computers SERVER-R2 SERVER1 Localhost
What if you wanted to change the contents of the variable You d simply assign a new value into one of the existing objects:
PS C:\> $computers[1] = $computers[1].replace('SERVER','CLIENT') PS C:\> $computers SERVER-R2 CLIENT1 Localhost
You can see that I changed the second object in the variable, rather than producing a new string. By the way, I want to show you two other options for working with the properties and methods of a bunch of objects contained in a variable. The previous couple of examples only executed methods on a single object within the variable. If I wanted to run the ToLower() method on every object within the variable, and store the results back into the variable, I would do this:
PS C:\> $computers = $computers | ForEach-Object { $_.ToLower() } PS C:\> $computers server-r2 client1 localhost
This is a bit complicated, so let s break it down in figure 15.2. I started the pipeline with $computers =, which means the results of the pipeline will be stored in that variable. Those results will overwrite whatever was in the variable previously. The pipeline begins with $computers being piped to ForEach-Object. The cmdlet will enumerate each object in the pipeline (I have three computer names, which are String objects), and execute its script block for each. Within the script block, the $_ placeholder will contain one piped-in object at a time, and I m executing the ToLower() method of
Copyright © OnBarcode.com . All rights reserved.