C
C#2mo ago
Nate

C# Array and Bubble Sort

hello i'm learning arrays and bubble sort. My program does not display anything in the console after selecting something from the menu. could someone help me? i'm a begineer to coding so any help with be greatly appreiecated
No description
22 Replies
PixxelKick
PixxelKick2mo ago
You're program is throwing a null reference exception there, thats causing execution to halt based on the code on that line, its because the variable names is null
Nate
Nate2mo ago
what do i have to do to fix that? all my methods don't run in the console
PixxelKick
PixxelKick2mo ago
so you're trying to evaluate what null.Length is which isnt possible, so it throws the null ref exception the variable names should have its value set before you hit that code effectively you're trying to access a variable before you gave it a value, so that throws an exception
Nate
Nate2mo ago
i am lost because i have never used ref, is there another way?
PixxelKick
PixxelKick2mo ago
variables are like a box you can store values in you have a box you have written the label names on with a marker, but the box is still empty your method ShowUnsortedList() is now trying to get the stuff inside the names box... but the box is empty so it goes "wat?" and the program crashes in C# we put stuff in our boxes (the variables) with the = operation
Nate
Nate2mo ago
what line of code can be used to fill in the names box?
PixxelKick
PixxelKick2mo ago
if you do
int foo = 1
int foo = 1
that makes a box named foo that can only hold ints and then we also put a value of 1 in that box
Nate
Nate2mo ago
i assume all my methods have the same problem
PixxelKick
PixxelKick2mo ago
very possible can you paste the code you currently have where you declare names?
Nate
Nate2mo ago
paste the code in that chat?
PixxelKick
PixxelKick2mo ago
yes using the ``` backtick markdown to put it in a code block
like this
like this
``` like this ``` not all your code just the part where you declare names
Nate
Nate2mo ago
static void ShowUnsortedList() {
Console.WriteLine("Name, Grade, Age List from File (Unsorted):"); for (int i = 0; i < names.Length; i++) { Console.WriteLine(names[i] + ", " + grades[i] + ", " + ages[i]); }
}
PixxelKick
PixxelKick2mo ago
nah thats evaluating names somewhere else you have declared names earlier declaring is when you first "make" the variable exist usually in the format of
type name = value;
type name = value;
IE
int foo = 1;
int foo = 1;
is declaring foo with a value of 1
Nate
Nate2mo ago
so because i'm talking about name grade age would it be int intgrade= 0; int intage=0;
PixxelKick
PixxelKick2mo ago
you're accessing what appears to be an array or list of names, grades, and ages so its likely more than just an int if its an array it'll have [ and ] to indicate its multiple
Nate
Nate2mo ago
i don't get it, can you show me?
PixxelKick
PixxelKick2mo ago
int foo = 1; means my box labeled foo can only hold a single int, it is an int, thats it int[] foos = [ 1, 2, 3, 4, 5] is an array of ints, which is sorta like a list of things (but List is its own thing in C# so we dont mix em up)
Nate
Nate2mo ago
so i declare int[] grade = 0; something like that?
PixxelKick
PixxelKick2mo ago
arrays need to be wrapped in [ ] int[] grades = [0]; would work, and specifically would declare "An array of grades 1 item long, with that item #0 having a value of 0" (in programming we are 0 based usually so stuff like arrays and lists start at 0, not 1, so the first item is "#0" if you will) and then you access it with [] as well. myArray[4] would get the item at index 4 inside of myArray
Nate
Nate2mo ago
i belive i got the method to be working thank you!
Nate
Nate2mo ago
No description
Nate
Nate2mo ago
i added this which made the console display instead of crashing/freezing
Want results from more Discord servers?
Add your server
More Posts
Decrease all of arrays by 1, optimization because of Time limit ExeededHi I want a code to reduce all ints in int[] happiness, exept for zeros to stay. I was wondering if ✅ Difference between explicit and implicit operatorsHello there. What's the difference between defining an implicit and explicit operator? I would likresource dictionary confusionSo this is a really weird question, so bear with me as I'm gonna try explaining. I am building a WPFWhat is the equivalent of this react component if made using C# and Blazor?I'm used to just doing .NET + React, but since they improved Blazor so much with .net 8 I'm having sApplication memory keeps increasingI have an application whose memory usage keeps increasing. The obvious thought is that I have a memoServe SPA (React) in ASP.NET web APIIs it a correct way to serve SPA in ASP.NET Web API project? Is there a better way to do it? The wayHow can i detect a new e-mail arrival using GraphClientServerI'm using Microsoft's outlook service through their graph library `Microsoft.Graph.` I make a login Concern over inheritance with static interface methodsConsider the following interface: ```cs public interface ILuaModel { static abstract void SetUseResolving External Components with Nested Components in NSwag for OpenAPI specsI am using NSwag to generate a C# client from an OpenAPI spec I want this spec to reference an exteLooking for help with database injection in NinjaTrader - Advanced(I think)I'm using NinjaTrader to build a fully automated trading bot. Right now the issue I'm having is that