C
C#2y ago
Str1ke

✅ (Very Begginer) - I Get errors when I can't access method within other class and syntax

I have a class " Course " and a class "University", in class Course there is a Code for each teacher (Between 1 and 15) I need to find the MostCommon teacher that has the most Courses in the University. $code
130 Replies
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Str1ke
Str1keOP2y ago
public int Mostcommon()
{
int[] arr = new int[15];
for(int i = 0; i < PlaceCourse.Length; i++)
{
arr[PlaceCourse.GetCode()]++;
}
int MostCommon;
for (int x = 0; x < arr.Length - 1; x++)
MostCommon = Math.Max(arr[x], arr[x + 1]);
for(int y = 0; y < arr.Length; y++)
if (MostCommon == arr[y])
{
return y;
}
}
public int Mostcommon()
{
int[] arr = new int[15];
for(int i = 0; i < PlaceCourse.Length; i++)
{
arr[PlaceCourse.GetCode()]++;
}
int MostCommon;
for (int x = 0; x < arr.Length - 1; x++)
MostCommon = Math.Max(arr[x], arr[x + 1]);
for(int y = 0; y < arr.Length; y++)
if (MostCommon == arr[y])
{
return y;
}
}
internal class Course
{
private string CourseName;
private int WeekHours;
private int Code;
public int GetCode()
{
return Code;
}
}
internal class Course
{
private string CourseName;
private int WeekHours;
private int Code;
public int GetCode()
{
return Code;
}
}
The access other class error is fixed But there is syntax error with MostCommon (int MostCommon) And if you know a more efficient/better way of doing this I will be very glad to hear it Because I'am pretty positive mine is not so good
public int Mostcommon()
{
int[] arr = new int[15];
for(int i = 0; i < PlaceCourse.Length; i++)
{
arr[PlaceCourse[i].GetCode()]++;
}
int MostCommon;
for (int x = 0; x < arr.Length - 1; x++)
MostCommon = Math.Max(arr[x], arr[x + 1]);
int y;
for (int y = 0; y < arr.Length; y++)
if (MostCommon == arr[y])
{
y = arr[y];
}
return y;
}
public int Mostcommon()
{
int[] arr = new int[15];
for(int i = 0; i < PlaceCourse.Length; i++)
{
arr[PlaceCourse[i].GetCode()]++;
}
int MostCommon;
for (int x = 0; x < arr.Length - 1; x++)
MostCommon = Math.Max(arr[x], arr[x + 1]);
int y;
for (int y = 0; y < arr.Length; y++)
if (MostCommon == arr[y])
{
y = arr[y];
}
return y;
}
I had to put "return y" out side the loop because it said that "not all code paths return" And now I get syntax error with y as well
MODiX
MODiX2y ago
you probably want $scopes
hiyosilver
hiyosilver2y ago
$scopes
MODiX
MODiX2y ago
scope A {
thing a;
scope B {
thing b;
}
}
scope A {
thing a;
scope B {
thing b;
}
}
thing a is available in scope A and scope B thing b is available only in scope B
hiyosilver
hiyosilver2y ago
You are trying to return y which was defined inside your for loop. I assume that causes your syntax error.
Str1ke
Str1keOP2y ago
But isn't it if it's inside the scopes of the whole method it should be defined inside all of it?
hiyosilver
hiyosilver2y ago
You could define y at the very top so it's in scope for all of it.
Str1ke
Str1keOP2y ago
It didn't change much xd I guess the problem is that it's value is defined inside the loop but I don't know how can I define it without getting the error because I need the loop to define it
hiyosilver
hiyosilver2y ago
Give me a second, had to switch from phone to pc
Str1ke
Str1keOP2y ago
Yeah I noticed that xd Thank you
hiyosilver
hiyosilver2y ago
No idea how people get stuff done on their phones. Just awful all around. Anyway, so I see now what this is supposed to do If we start at the top, you know how many courses there are from PlaceCourse no?
Str1ke
Str1keOP2y ago
Only if you could see my screen...xd YOu mean like what is the size of PlaceCourse? If so, yeah that's a 1000 private Course[] PlaceCourse; private int current=0; public University() { PlaceCourse = new Course[1000]; } done it in a constructor previoulsy
hiyosilver
hiyosilver2y ago
Oh wait, I'm dumb yeah that makes more sense Array contains up to 15 ids for each of the teachers Are you sure they go from 1 to 15?
Str1ke
Str1keOP2y ago
yes that is declared in the question
hiyosilver
hiyosilver2y ago
because you are accessing the array with indices 1 to 15 then, but it only goes from 0 to 14
int[] arr = new int[15];
for(int i = 0; i < PlaceCourse.Length; i++)
{
arr[PlaceCourse.GetCode()]++;
}
int[] arr = new int[15];
for(int i = 0; i < PlaceCourse.Length; i++)
{
arr[PlaceCourse.GetCode()]++;
}
if GetCode ever returned 15 for any teacher, this would explode
Str1ke
Str1keOP2y ago
Oh yeah, Ig it's better to write it as 16 and start from 1
hiyosilver
hiyosilver2y ago
you could just use GetCode() - 1 to index into the array. You should probably leave it starting at 0, that's pretty standard.
Str1ke
Str1keOP2y ago
But then it will return the index with the code-1 And that won't be the right teacher
hiyosilver
hiyosilver2y ago
It will be if you always access it like that Lets say you ask "How many courses does the teacher with Id 8 have"? its arr[8 - 1] i.e. the 8th index of your array. Which by convention starts at index 0, not 1 🙂
Str1ke
Str1keOP2y ago
True, but the question wants me to return a number between 1 and 15
hiyosilver
hiyosilver2y ago
I supposed you could just increase it to 16 and just leave the 0 slot empty, but I would suggest that that's not good practice and is more likely to confuse you later. You can still do that So you want the most common teacher Id from all the courses.
Str1ke
Str1keOP2y ago
yeah
hiyosilver
hiyosilver2y ago
You already define int MostCommon so thats almost certainly going to be what you want to return rather than 1 Now lets see how you set it
Str1ke
Str1keOP2y ago
The ' y ' is the Index (ID of the teacher)
hiyosilver
hiyosilver2y ago
I see you are looping once to find the actual number of courses And then again to find the index of that count You could probably just do that in one go
Str1ke
Str1keOP2y ago
Once to find the teached with the most courses and then to find his index yeah
hiyosilver
hiyosilver2y ago
something like
int mostCommon;
int maxCount = 0;
for(int i = 0; i < arr.Length; i++) {
if(arr[i] > maxCount) {
maxCount = arr[i];
mostCommon = i + 1;
}
}
return mostCommon;
int mostCommon;
int maxCount = 0;
for(int i = 0; i < arr.Length; i++) {
if(arr[i] > maxCount) {
maxCount = arr[i];
mostCommon = i + 1;
}
}
return mostCommon;
Deal with the teacher id like its 0 based for indexing purposes, then only add back one to make it human-readable at the end. Something like this, I don't know. There is probably something nicer you can do, but this should work.
Str1ke
Str1keOP2y ago
Oh wait Let me process that real quick xd
hiyosilver
hiyosilver2y ago
actually, let me make a quick edit, that was dumb by me This is better, so you can just cleanly return mostCommon Technically this doesn't cover a case where no teacher has any courses, not sure what you'd want to happen there.
Str1ke
Str1keOP2y ago
the question says that there is one for sure $code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Str1ke
Str1keOP2y ago
public int Mostcommon()
{
int[] arr = new int[15];
for(int i = 0; i < PlaceCourse.Length; i++)
{
arr[PlaceCourse[i].GetCode()]++;
}
int MostCommon = arr[0];
int Index = 0;
for (int x = 1; x < arr.Length - 1; x++)
{
if (MostCommon < arr[x])
{
MostCommon = arr[x];
Index = x;
}
}
return Index+1;
}
public int Mostcommon()
{
int[] arr = new int[15];
for(int i = 0; i < PlaceCourse.Length; i++)
{
arr[PlaceCourse[i].GetCode()]++;
}
int MostCommon = arr[0];
int Index = 0;
for (int x = 1; x < arr.Length - 1; x++)
{
if (MostCommon < arr[x])
{
MostCommon = arr[x];
Index = x;
}
}
return Index+1;
}
Won't it just returd 0+1 every time now? return*
hiyosilver
hiyosilver2y ago
Only if Index never gets set. That would be the case, if arr[0] is already the most common id also, you are skipping 1 at the end in your for loop condition
Str1ke
Str1keOP2y ago
oy vey forgor to delete it from previous
hiyosilver
hiyosilver2y ago
You dont need to do that anymore, since oyu arent comparing neighbouring indices .) Yeah
Str1ke
Str1keOP2y ago
so even tho it's value is redeclared inside a loop It will see that?
hiyosilver
hiyosilver2y ago
You aren't redefining it in this case. Index refers to a variable outside the loop you are just assigning to it But that doesn't need to happen, if that assignment never gets hit, Index just retains the value you defined at first 0, that is
Str1ke
Str1keOP2y ago
But before that I refereed to a variable outside the loop as well
hiyosilver
hiyosilver2y ago
Wdym?
Str1ke
Str1keOP2y ago
If the first teacher has the most Courses I declared the Index (y) outside the loop before that as well, and only assigned its value inside the loop and it had syntax error
hiyosilver
hiyosilver2y ago
Yeah you did You declared it outside one of your loops But you had two nested loops
Str1ke
Str1keOP2y ago
No no, they were separated
hiyosilver
hiyosilver2y ago
So y existed inside both loops, but NOT outside the outer loop where you tried to return it.
Str1ke
Str1keOP2y ago
public int Mostcommon() { int[] arr = new int[15]; for(int i = 0; i < PlaceCourse.Length; i++) { arr[PlaceCourse[i].GetCode()]++; } int MostCommon; for (int x = 0; x < arr.Length - 1; x++) MostCommon = Math.Max(arr[x], arr[x + 1]); int y; for (int y = 0; y < arr.Length; y++) if (MostCommon == arr[y]) { y = arr[y]; } return y; }
hiyosilver
hiyosilver2y ago
Ah I cant read Formatting is a bit whack Give me a second to process that.
Str1ke
Str1keOP2y ago
xd, np, thank you
hiyosilver
hiyosilver2y ago
Oh I see the problem i was confused. You were right about the loops being separated. Btw, it's probably best to always use curly braces. I know they are optional for one-liners like this, but jesus does that make it hard to format and read 😄
Str1ke
Str1keOP2y ago
xd
hiyosilver
hiyosilver2y ago
You are redefining y as your loop iterator I'm pretty sure that's why its blowing up
Str1ke
Str1keOP2y ago
iterator? wdym
hiyosilver
hiyosilver2y ago
You are using an int called y in your second for loop
Str1ke
Str1keOP2y ago
aaaa omg didn't see that
hiyosilver
hiyosilver2y ago
I keep forgetting what the rght word for that is you redefine it there, causing it to only exist for the scope of the second loop 🙂 I'm like 90% sure that's whats causing your syntax error.
Str1ke
Str1keOP2y ago
nope xd I renamed it to z and still the same
hiyosilver
hiyosilver2y ago
And that, kids, is why we don't use single letters for variable names 😉 except in loops 😛
Str1ke
Str1keOP2y ago
and there is syntax error with MostCommon as well True xd
hiyosilver
hiyosilver2y ago
Still broken? mmmh
Str1ke
Str1keOP2y ago
yep xd public int Mostcommon() { int[] arr = new int[15]; for (int i = 0; i < PlaceCourse.Length; i++) { arr[PlaceCourse[i].GetCode()]++; } int MostCommon; for (int x = 0; x < arr.Length - 1; x++) MostCommon = Math.Max(arr[x], arr[x + 1]); int z; for (int y = 0; y < arr.Length; y++) if (MostCommon == arr[y]) { z = arr[y]; } return z; } sorry that I send it like this it's just tedeous to recopy the $code thing
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
hiyosilver
hiyosilver2y ago
You might need to initialize MostCommon Before accessing it the first time
Str1ke
Str1keOP2y ago
initialize?
hiyosilver
hiyosilver2y ago
set it to some initial value
Str1ke
Str1keOP2y ago
hm
hiyosilver
hiyosilver2y ago
It doesnt like the comparison Because that might be the first thing that happens, before any assignment And it would not be set to anything valid at that point
Str1ke
Str1keOP2y ago
But isn't it like, if it doesn't see that I set it to a certain value inside a loop, it won't see it changing inside a loop?
hiyosilver
hiyosilver2y ago
can you rephrase that? i dont quite follow
Str1ke
Str1keOP2y ago
If it gets syntax error, when I declare the value of the MostCommon inside a loop, why would it see it when I change the value of it, from lets say 0?
hiyosilver
hiyosilver2y ago
I#m not entirely sure what you mean, still 😄
Str1ke
Str1keOP2y ago
Uno momento I try another way xd
hiyosilver
hiyosilver2y ago
What I suspsed the syntax error is, has to do with the comparison in the if *suspect jesus
Str1ke
Str1keOP2y ago
yes
hiyosilver
hiyosilver2y ago
You aren't declaring MostCommon in the loop, just to be clear You are simply assigning a value to it and that ones perfectly fine.
Str1ke
Str1keOP2y ago
yes
hiyosilver
hiyosilver2y ago
But I think what the compiler doesn't like, is the comparison in the if
Str1ke
Str1keOP2y ago
That's what I did in my code as well
hiyosilver
hiyosilver2y ago
Because at that point, MostCommon might not yet have been set even once, making the current value kind of not valid.
Str1ke
Str1keOP2y ago
aaaaaa
hiyosilver
hiyosilver2y ago
By setting it where you declare the variable
Str1ke
Str1keOP2y ago
That makes sense now
hiyosilver
hiyosilver2y ago
You are letting the compiler know "hey this has been properly initialized, you can assume that whatever value it holds is valid in some way"
Str1ke
Str1keOP2y ago
Yes man, you fucking gigachad xd
hiyosilver
hiyosilver2y ago
I think that is why you are getting the error, but you should also be able to hover over it and see the error message btw 😉
Str1ke
Str1keOP2y ago
Let me see if that works real quick Microsoft don't explain it as good as you do xd
hiyosilver
hiyosilver2y ago
Yeah, not to toot my own horn, but Microsoft docs aren't brilliant if you are a beginner
Str1ke
Str1keOP2y ago
yep xd
hiyosilver
hiyosilver2y ago
They are technically good though, really thorough on a lot of things. Just a bit too dense sometimes 🙂
Str1ke
Str1keOP2y ago
I mean some times they do help but it's pretty rare So I much prefer to speak with a human xd yeah man, that fixed it xd
hiyosilver
hiyosilver2y ago
Noice So, just for completeness sake This is what I believe is roughly a good structure
public int MostCommon()
{
int[] arr = new int[15];
for(int i = 0; i < PlaceCourse.Length; i++)
{
arr[PlaceCourse[i].GetCode()]++;
}
int mostCommon = 1;
int maxCount = arr[0];
for(int i = 1; i < arr.Length; i++) {
if(arr[i] > maxCount) {
maxCount = arr[i];
mostCommon = i + 1;
}
}
return mostCommon;
}
public int MostCommon()
{
int[] arr = new int[15];
for(int i = 0; i < PlaceCourse.Length; i++)
{
arr[PlaceCourse[i].GetCode()]++;
}
int mostCommon = 1;
int maxCount = arr[0];
for(int i = 1; i < arr.Length; i++) {
if(arr[i] > maxCount) {
maxCount = arr[i];
mostCommon = i + 1;
}
}
return mostCommon;
}
Btw, local variables we case likeThis Technically not required, but easier for others to read since it is the convention 😉
Str1ke
Str1keOP2y ago
wut xd Didn't understand what you mean by that
hiyosilver
hiyosilver2y ago
"Camel case" It's the commonly agreed way of writing local variable names firstLetterLowerCase You are doing PascalCase
Str1ke
Str1keOP2y ago
Oh
hiyosilver
hiyosilver2y ago
First letter uppercase
Str1ke
Str1keOP2y ago
Really?
hiyosilver
hiyosilver2y ago
camel case is the convention for local variables at least and method parameters
Str1ke
Str1keOP2y ago
oh
hiyosilver
hiyosilver2y ago
As I said, it works either way, but if you share your code, it will probably be more readable if you stick to convention 😉
Str1ke
Str1keOP2y ago
So only methods start with Upper case and every word start with upper? And local vars are with lower then every word with upper?
hiyosilver
hiyosilver2y ago
Methods and Class names are generally PascalCase So first letter upper case, yes
Str1ke
Str1keOP2y ago
that's something new xd, will be sure to stick to that then
hiyosilver
hiyosilver2y ago
Some other naming conventions are a bit more controversial and not everyone does the sam but these two are pretty much universal in C#
Str1ke
Str1keOP2y ago
If not a secret Where did you learn to code? / How
hiyosilver
hiyosilver2y ago
Just on my own to be honest 😄 I technically went to a sort of school for it for a little bit But 99% of what I know I taught myself Mostly making game stuff
hiyosilver
hiyosilver2y ago
Unity first, now Godot Some WPF app stuff general console app shenanigans Whatever I felt like
Str1ke
Str1keOP2y ago
That's cool man
hiyosilver
hiyosilver2y ago
Big gaps though, I know absolutely nothing about databases or networks, so... 😄
Str1ke
Str1keOP2y ago
Bruh, trust me, just let me get to your level first, and that will be great already Because like from what I know
hiyosilver
hiyosilver2y ago
Very flattering
Str1ke
Str1keOP2y ago
There is like tons of shit that are not conventional
hiyosilver
hiyosilver2y ago
When I look at people who I consider great programmers, I feel like an absolute dunce
Str1ke
Str1keOP2y ago
and are not even encripted in the basic visual studio and you have to add it manualy like " Lists " and stuff And like I don't know how can you remember all that
hiyosilver
hiyosilver2y ago
Yeah, but honestly stuff like typing out using System.Collections.Generic at light speed becomes second nature after some time 😄
Str1ke
Str1keOP2y ago
Amen xd Like that's where the programing really beggins I guess
hiyosilver
hiyosilver2y ago
Just stick to it, it'll come to you faster than you think.
Str1ke
Str1keOP2y ago
because what I'am doing now is just math shit and not reall things
hiyosilver
hiyosilver2y ago
What's even real? 😉 Really, knowing syntax is a minor detail at the end of the day. Learn how to understand, compartmentalize and solve logical problems.
Str1ke
Str1keOP2y ago
Like lets say, with the help of chatgpt I made a automate mouse clicker that clicked in certain places on screen with differnet intervals of time
hiyosilver
hiyosilver2y ago
ye
Str1ke
Str1keOP2y ago
and I used it to aumate playing in a casino where every 15 minutes I get 0.03 cents that's a real program xd automate*
hiyosilver
hiyosilver2y ago
I see 😄
Str1ke
Str1keOP2y ago
Yep, if you can think logicaly and technicalities will come with time ig
hiyosilver
hiyosilver2y ago
That's why I like making games in easily accessible engines. You get to make lots of real shit while much of the scaffolding is taken care of and you can actually get somewhere, even as a beginner.
Str1ke
Str1keOP2y ago
the masterpeace of "Cntrl+c, cntrl+v"? xd
hiyosilver
hiyosilver2y ago
Alright, I gotta hustle
Str1ke
Str1keOP2y ago
Cya, man, thank you very much for everything
hiyosilver
hiyosilver2y ago
Pump some iron, bruh chadcardy welcome Any other questions, feel free to ask.
Str1ke
Str1keOP2y ago
I'am sure there will be xd
hiyosilver
hiyosilver2y ago
Or ask in the forum like before, up to you 😉 kk good luck, have fun coding and keep at it!
Str1ke
Str1keOP2y ago
♥️ ♥️ ♥️ ♥️
Want results from more Discord servers?
Add your server