0

I am attempting to create a parse function that takes in a string and returns an array. So far I am having a problem with my array declaration:

var sArray = sfunctions.ParseThis(ctrlValue);
ctrlValue = sArray[0]; // Get the first string from the array
rValue = sNew[1];

I have also tried:

string [] sArray = sfunctions.ParseThis(ctrlValue);

In my old c programming days I could declare an array and it would live outside a function without passing anything but a reference, just not sure how to do that in c# or coded ui test.

Thanks for assistance.

control.ClickCustomControl(parent, "InnerText", "Register-16"); 

where ctrlValue = "Register-16"

And - other answers:

public Array ParseThis(string sToParse)
{
    char cSplit = '-';
    string[] sNew = sToParse.Split(new char[] { cSplit });
    return sNew;
}
5
  • Hows your string input looks like? Commented Mar 2, 2016 at 2:35
  • What is the signature of the ParseThis() function? Both samples you posted will work just fine if that function is declared and implemented properly. Commented Mar 2, 2016 at 2:36
  • Post the declaration of ParseThis and the value of ctrlValue. Commented Mar 2, 2016 at 2:36
  • public Array ParseThis(string sToParse) { char cSplit = '-'; string[] sNew = sToParse.Split(new char[] { cSplit }); return sNew; } Commented Mar 2, 2016 at 2:39
  • control.ClickCustomControl(parent, "InnerText", "Register-16"); Commented Mar 2, 2016 at 2:41

2 Answers 2

3

If your ParseThis method must return an Array, you should then call sArray.GetValue(0) instead. Or else, simply make that method return a string[] and you can use the [0] indexer.

public Array ParseThis(string sToParse)
{
    return sToParse.Split(new char[] { '-' });
}

Corresponds to:

ctrlValue = sArray.GetValue(0);

And

public string[] ParseThis(string sToParse)
{
    return sToParse.Split(new char[] { '-' });
}

Corresponds to:

ctrlValue = sArray[0];

But you could use the GetValue method as well in this case.

On another topic, you should check the returned object has at least one element before trying to access the first one.

Sign up to request clarification or add additional context in comments.

6 Comments

I have tried to return string[], but get an error even with [0]. I am not sure where you mean to call sArray.GetValue(0)?
What error exactly? You should call it in the exact same place you are calling it right now, of course. Maybe if you post more of your code it would be easier to understand the situation.
code <this> Array sArray = sfunctions.ParseThis2(ctrlValue); ctrlValue = sArray.GetValue(0); // Get the first string from the array rValue = sArray[1]; <\this>
Sorry - I don't know how to use the formatter! I tried both suggestions and neither work. Please refer back to my original call. Maybe that is where the problem is?
For comments, you just to have to surround your code with the grave accent ``. What type do both your ParseThis methods return? If it returns Array, you must use the GetValue method. I suggest you make it return a string array.
|
0

You should avoid using the Array class directly in your code (unless you're calling its static methods). You should be returning the actual array type instead (string[] in your case)

From MSDN:

The Array class is the base class for language implementations that support arrays. However, only the system and compilers can derive explicitly from the Array class. Users should employ the array constructs provided by the language.

So a more appropriate declaration would be

public string[] ParseThis(string sToParse)
{
    char cSplit = '-';
    string[] sNew = sToParse.Split(new char[] { cSplit });
    return sNew;
}

But you really don't need a function since you could just do

string[] parts = ctrlValue.Parse('-');

3 Comments

@D Stanley, code public Array ParseThis(string sToParse) { char cSplit = '-'; string[] sNew = sToParse.Split(new char[] { cSplit }); return string[];// sNew; } code
Thank you! I updated with your recommendation. <br/> I have been using the single line without the function, but wanted to get the hang of using a called and returned array as I will need it in other places. <br/> Thank you again! Now if I could figure out how to format my comments ;)
@QADev Formatting in comments is very limited. The best you can do for code is surround it with backticks but there's no way to show multiple lines, indent, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.