Solved

Unable to index into an object of type System.String

Posted on 2013-01-12
11
3,022 Views
Last Modified: 2013-01-15
Hello, I am getting the following error and not sure how to fix it.
11
10
9
8
7
6
5
4
3
2
1
0
Type the new TELEPHONE Number: 3033334444
BINGO NO -
Unable to index into an object of type System.String.
At C:\scripts\AD\loops.ps1:71 char:3
+         $InputTelephoneNumber[$counter] = "-"
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : CannotIndex

Open in new window

And this is the code:
[string]$InputTelephoneNumber = Read-Host 'Type the new TELEPHONE Number'
for($counter = 11; $counter -gt ($InputTelephoneNumber.Count - 2); $counter--) {	 
	 if (($counter -eq "7") -and ($InputTelephoneNumber[$counter] -ne "-")) {
	    write-host "BINGO NO -"
		$InputTelephoneNumber[$counter] = "-"
	 }
 }

Open in new window

Thanks for your help
0
Comment
Question by:namerg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 17

Expert Comment

by:Learnctx
ID: 38771311
I'm not really sure what you're trying to achieve here, but if it is pattern matching on the input of a phone number look at using regular expressions. But, looking at your code as is, you're using $InputTelephoneNumber.count. This will always return 1, because the string only contains 1 object, the string. So When you finally get $counter to be less than 1, and -2, it is -1. So you are effectively doing:

$InputTelephoneNumber[-1]

Open in new window

This is an invalid index for an array. So, maybe you mean to use $InputTelephoneNumber.Length instead, which is the length of the string. I don't think this will achieve whatever it is you're trying to do either, what are you actually trying to check? If a certain character in the string is a dash/hyphen?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38771413
can u explain first what is the purpose of your code?
0
 

Author Comment

by:namerg
ID: 38772396
Well, $InputTelephoneNumber  is an array once you enter the phone number right ?

So, if I enter 3034445555

3 -> Position 0
0 -> Position 1
3 -> Position 2
4 -> Position 3
.
.
5 -> Position 9

Correct ?

So, if I want to change the value of Position 3 for (-) and Position 7 for (-), how would i do it ?

In other words how do i replace values in an array ?

Thanks,
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 42

Expert Comment

by:sedgwick
ID: 38772476
use index to change array values:

position 3:
$InputTelephoneNumber[2] = "-"

position 7:
$InputTelephoneNumber[6] = "-"
0
 

Author Comment

by:namerg
ID: 38772508
Hmm, but can I replace the number [2] by a counter $i or $counter ? Like my code reflects it ?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38772635
Yes use $counter as the array index
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38772642
What do u expect the phone to look like?
For example
3034445555

Should be
303-444-5555

Am i correct?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38772643
In any case you dont have to loop the number.
With one line of code you can reformat it.
0
 

Author Comment

by:namerg
ID: 38772677
Yes, you are correct.

The thing is in another language your treat the array as I said, but in powershell is different and is hard to understand the logic for the array if i want to change values if i want to treat the index as a counter..
0
 
LVL 42

Assisted Solution

by:sedgwick
sedgwick earned 250 total points
ID: 38772703
To format a number the same way you want you do the following:
$telephoneNumber = {0:###-###-####} -f [double]$telephoneNumber
0
 
LVL 40

Accepted Solution

by:
Subsun earned 250 total points
ID: 38772951
Why don't you simply insert the the '-'?
$InputTelephoneNumber.Insert(3,"-").Insert(7,"-")
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Synchronize a new Active Directory domain with an existing Office 365 tenant
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question