Click here to Skip to main content
15,860,859 members
Home / Discussions / C#
   

C#

 
GeneralRe: WPF MVVM Entity Framework(using code First Approach) Pin
Member 145206354-Jul-19 2:48
Member 145206354-Jul-19 2:48 
GeneralRe: WPF MVVM Entity Framework(using code First Approach) Pin
Gerry Schmitz4-Jul-19 7:36
mveGerry Schmitz4-Jul-19 7:36 
GeneralRe: WPF MVVM Entity Framework(using code First Approach) Pin
Mycroft Holmes4-Jul-19 13:01
professionalMycroft Holmes4-Jul-19 13:01 
AnswerRe: WPF MVVM Entity Framework(using code First Approach) Pin
BillWoodruff4-Jul-19 2:22
professionalBillWoodruff4-Jul-19 2:22 
Questionindex was out of bound of the array Pin
Derbz3-Jul-19 16:50
Derbz3-Jul-19 16:50 
AnswerRe: index was out of bound of the array Pin
phil.o3-Jul-19 17:26
professionalphil.o3-Jul-19 17:26 
GeneralRe: index was out of bound of the array Pin
lmoelleb3-Jul-19 22:36
lmoelleb3-Jul-19 22:36 
AnswerRe: index was out of bound of the array Pin
OriginalGriff3-Jul-19 19:49
mveOriginalGriff3-Jul-19 19:49 
Phil.o is exactly right with what your problem is: array indexes in c# (and most modern languages) run from zero to N-1 where N is the number of elements. An attempt to use index N or any negative number is automatically outside the array and will generate a error.

But ... a couple of things:
0) It's a very good idea to always use curly brackets round even single lines inside an if:
C#
if (num[input] % 2 == 0)
    {
    Console.Write(num[input]);
    }
Even++;
If you don't, then you can get problems because the indentation is just to make teh code more readable to us, it means nothing to the compiler. If you add a line which is meant to be inside the if without curly brackets, it looks like it should work, but it won't:
C#
if (num[input] % 2 == 0)
    Console.Write(num[input]);
    num[input] = 0;
Even++;
If you always use the brackets, it's obvious and you can't get it wrong:
C#
if (num[input] % 2 == 0)
    {
    Console.Write(num[input]);
    num[input] = 0;
    }
Even++;

1) Don't comment what the instruction is doing:
C#
int[] num = new int[10]; //array declaration
We know it's declaring an array, because we can read the code: it's declaring an integer array variable called num and assigning a new instance of 10 elements.
Your comment tells us nothing useful; it add nothing to the code. In fact it tells us less than the code does, and it's more likely to get "out of step" with the code if you change the array to a List (which is a kind of array that you can add and remove elements from - you'll meet them later).
Pretty much every comment in there does the same thing: document what the code is doing, not why you are doing it.
A better comment in this case is no comment at all, but use a different name for the variable:
C#
int[] userInputs = new int[10];
That tells you what the array actually contains, without needing a comment.
This is called "self documentation" and it has the major advantage that it's much, much more likely to be kept up to date when code changes.

2) Don't use Convert to parse user inputs: users make mistakes, and Convert will cause your app to fail if they do. While this is only ten numbers, it's still going to be very annoying to type nine of them successfully and then miskey on the tenth and have the app crash out as a result!
Instead, use the TryParse methods:
C#
int[] userInputs = new int[10];
int index = 0;
while (index < userInputs.Length)
    {
    Console.Write("Enter a whole number: ");
    string input = Console.ReadLine();
    if (int.TryParse(input, out userInputs[index]))
        {
        index++;
        }
    else
        {
        Console.WriteLine($"\"{input}\" is not an integer!");
        }
    }

3) Are you sure you want to use input in your final pair of loops?
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!


modified 4-Jul-19 3:08am.

AnswerRe: index was out of bound of the array Pin
bVagadishnu5-Jul-19 7:03
bVagadishnu5-Jul-19 7:03 
QuestionTrying to use VS2017 C# and Excel2007. I need help understanding the error being reported Pin
Greg Gonzales2-Jul-19 19:15
Greg Gonzales2-Jul-19 19:15 
AnswerRe: Trying to use VS2017 C# and Excel2007. I need help understanding the error being reported Pin
OriginalGriff2-Jul-19 20:17
mveOriginalGriff2-Jul-19 20:17 
AnswerRe: Trying to use VS2017 C# and Excel2007. I need help understanding the error being reported Pin
Gerry Schmitz2-Jul-19 21:42
mveGerry Schmitz2-Jul-19 21:42 
AnswerRe: Trying to use VS2017 C# and Excel2007. I need help understanding the error being reported Pin
Maciej Los3-Jul-19 8:44
mveMaciej Los3-Jul-19 8:44 
Questionusing c# to copy files from clients to main server Pin
coderz322-Jul-19 12:34
coderz322-Jul-19 12:34 
AnswerRe: using c# to copy files from clients to main server Pin
BillWoodruff2-Jul-19 15:35
professionalBillWoodruff2-Jul-19 15:35 
QuestionHttpException Pin
indra160230-Jun-19 18:50
indra160230-Jun-19 18:50 
AnswerRe: HttpException Pin
Richard Deeming1-Jul-19 0:56
mveRichard Deeming1-Jul-19 0:56 
Questionexception for httpTransportBindingElement Pin
indra160230-Jun-19 17:46
indra160230-Jun-19 17:46 
AnswerRe: exception for httpTransportBindingElement Pin
#realJSOP2-Jul-19 2:46
mve#realJSOP2-Jul-19 2:46 
QuestionHTML to PDF conversion Pin
Member 1164896329-Jun-19 23:59
Member 1164896329-Jun-19 23:59 
AnswerRe: HTML to PDF conversion Pin
OriginalGriff30-Jun-19 1:53
mveOriginalGriff30-Jun-19 1:53 
QuestionFinding many locations of a Bitmap contained inside another Bitmap Pin
IELTS for Beginners29-Jun-19 0:41
IELTS for Beginners29-Jun-19 0:41 
AnswerRe: Finding many locations of a Bitmap contained inside another Bitmap Pin
OriginalGriff29-Jun-19 0:46
mveOriginalGriff29-Jun-19 0:46 
AnswerRe: Finding many locations of a Bitmap contained inside another Bitmap Pin
Richard MacCutchan29-Jun-19 7:24
mveRichard MacCutchan29-Jun-19 7:24 
AnswerRe: Finding many locations of a Bitmap contained inside another Bitmap Pin
BillWoodruff29-Jun-19 19:05
professionalBillWoodruff29-Jun-19 19:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.