/    Sign up×
Community /Pin to ProfileBookmark

Error – array does not exist in the current context c#

Hi, Can someone tell me what is wrong?
I need to take the numbers inputted and console only the positive numbers. So I am trying to add the positive numbers into an array and negative numbers into another. But I get this error at this line `int[] splitPositiveNumbers = positiveNumbers.Split();` – positiveNumbers does not exist in the current context.
Here is the code.

[code]
string inputData;

do
{
inputData = Console.ReadLine();
int number = Convert.ToInt32(inputData);

int[] positiveNumbers;
int[] negativeNumbers;

if (number > 0)
{
positiveNumbers = new int[number];

}else if(number < 0)
{
negativeNumbers = new int[number];
}

} while (inputData != “x”);

int[] splitPositiveNumbers = positiveNumbers.Split();

Console.WriteLine(splitPositiveNumbers);
Console.ReadLine();
[/code]

to post a comment

4 Comments(s)

Copy linkTweet thisAlerts:
@daveyerwinOct 13.2019 — @RaulRogojan#1610012

A variable which is declared inside a loop body will not be visible to the outside of loop body.

try like this ...


``
int[] positiveNumbers;<br/>
int[] negativeNumbers;<br/>
do<br/>
{<br/>
inputData = Console.ReadLine();<br/>
int number = Convert.ToInt32(inputData);<br/>
...
``
Copy linkTweet thisAlerts:
@codyhillauthorOct 13.2019 — @DaveyErwin#1610017 Tryed it that way and it did not work. But I fixed the problem anyway. ty
Copy linkTweet thisAlerts:
@VITSUSAOct 14.2019 — @RaulRogojan#1610025 What was the error?
Copy linkTweet thisAlerts:
@codyhillauthorOct 15.2019 — @VITSUSA#1610035 the same error. The code logic was not good. I solve it using Lists.
×

Success!

Help @codyhill spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...