C
C#2y ago
Str1ke

❔ (Very beginner)

Guys, I've got a class TvProgram(The program it self), and a class TvWeek(the programs in the week), the class TvWeek consists of a maximum of 100 programs, now I need to make a method that adds a program to the week and adds the amount of the current amount of programs in that week by 1. How do I do that?
internal class TvWeek
{
private TvWeek()
{
TvProgram[] arrProg = new TvProgram[100];
}
private int current;
public void TvProgramAdd(TvProgram Program)
{
arrProg[current++] = Program;
}
}
}
internal class TvWeek
{
private TvWeek()
{
TvProgram[] arrProg = new TvProgram[100];
}
private int current;
public void TvProgramAdd(TvProgram Program)
{
arrProg[current++] = Program;
}
}
}
46 Replies
phaseshift
phaseshift2y ago
Use a list instead
Str1ke
Str1keOP2y ago
We didn't learn lists yet I need to do it somehow without
phaseshift
phaseshift2y ago
Well arrays are fixed size
Str1ke
Str1keOP2y ago
Yes It doesn't need to go beyond 100
phaseshift
phaseshift2y ago
Ok. So what's wrong with what you have?
Str1ke
Str1keOP2y ago
Str1ke
Str1keOP2y ago
it doesn't see the arrProg from the TvWeek
phaseshift
phaseshift2y ago
Paste text not images please
Str1ke
Str1keOP2y ago
Compiler Error CS0103 - The name 'identifier' does not exist in the current context
phaseshift
phaseshift2y ago
Why do you think that is? Where did you define it?
Str1ke
Str1keOP2y ago
I defined it in the class
phaseshift
phaseshift2y ago
Nope Kinda, but nope
Str1ke
Str1keOP2y ago
xd, if I define it in another method it won't see it in another? Even tho it's in the same class?
phaseshift
phaseshift2y ago
Correct
Str1ke
Str1keOP2y ago
mhmm, so It's only possible to do this question by making a new array in the new method?
phaseshift
phaseshift2y ago
No
Str1ke
Str1keOP2y ago
Because it feels kinda strange
phaseshift
phaseshift2y ago
Look how you defined current
Str1ke
Str1keOP2y ago
Yes I don't get it
phaseshift
phaseshift2y ago
What's the difference between that and your array?
Str1ke
Str1keOP2y ago
Good question
phaseshift
phaseshift2y ago
You can't see any difference?
Str1ke
Str1keOP2y ago
I can see a difference I defined the TvWeek and didn't the " current"
phaseshift
phaseshift2y ago
You did
Str1ke
Str1keOP2y ago
I didn't encounter questions like this with arrays before So I have hard time understand what's wrong with how I defined the TvWeek
phaseshift
phaseshift2y ago
It's nothing to do with arrays It's basic c# syntax Where is current defined?
Str1ke
Str1keOP2y ago
in " internal class TvWeek "
phaseshift
phaseshift2y ago
Right. In the class body Notice the compiler is not complaining about the use of current in your method
Str1ke
Str1keOP2y ago
ohh $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
namespace PrepForExam
{
internal class TvWeek
{
TvProgram[] arrProg = new TvProgram[100];

private int current;

public void TvWeekAddProgram(TvProgram Program)
{
arrProg[current++] = Program;

}
}
}
namespace PrepForExam
{
internal class TvWeek
{
TvProgram[] arrProg = new TvProgram[100];

private int current;

public void TvWeekAddProgram(TvProgram Program)
{
arrProg[current++] = Program;

}
}
}
but they say, that TvWeek has two parameters - array in a length of 100 TvPrograms and current in int Is that considered a parameter of the class TvWeek?
phaseshift
phaseshift2y ago
Parameters are inputs to methods (including constructors)
Str1ke
Str1keOP2y ago
So that's not a parameter You know how can I make it a parameter and use it in the class?
phaseshift
phaseshift2y ago
Look up constructors
Str1ke
Str1keOP2y ago
Yes
phaseshift
phaseshift2y ago
You pass in arguments, then assign the value to class fields
Str1ke
Str1keOP2y ago
public TvWeek(TvProgram[] arr)
{

}
public TvWeek(TvProgram[] arr)
{

}
I do this, and then I send array of 100 programs?
phaseshift
phaseshift2y ago
Yes And save the value to a field
Str1ke
Str1keOP2y ago
Can you please explain that?
phaseshift
phaseshift2y ago
Ie you still need arrProg
Str1ke
Str1keOP2y ago
Oh, so define this array as arrProg in the constructor?
phaseshift
phaseshift2y ago
No Probably best if you look up constructors
Str1ke
Str1keOP2y ago
I will check it, but I did work with constructors, just not arrays
phaseshift
phaseshift2y ago
Again, your questions are not any difficulty to do with arrays
Str1ke
Str1keOP2y ago
You right Ohhh, I see now
internal class TvWeek
{
private TvProgram[] arrProg;
public TvWeek()
{
arrProg = new TvProgram[100];
}

private int current;

public void TvWeekAddProgram(TvProgram Program)
{
arrProg[current++] = Program;

}
}
}
internal class TvWeek
{
private TvProgram[] arrProg;
public TvWeek()
{
arrProg = new TvProgram[100];
}

private int current;

public void TvWeekAddProgram(TvProgram Program)
{
arrProg[current++] = Program;

}
}
}
I had to write it that way, thank you for your time!
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server