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
Str1ke2y ago
We didn't learn lists yet I need to do it somehow without
phaseshift
phaseshift2y ago
Well arrays are fixed size
Str1ke
Str1ke2y ago
Yes It doesn't need to go beyond 100
phaseshift
phaseshift2y ago
Ok. So what's wrong with what you have?
Str1ke
Str1ke2y ago
Str1ke
Str1ke2y ago
it doesn't see the arrProg from the TvWeek
phaseshift
phaseshift2y ago
Paste text not images please
Str1ke
Str1ke2y 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
Str1ke2y ago
I defined it in the class
phaseshift
phaseshift2y ago
Nope Kinda, but nope
Str1ke
Str1ke2y 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
Str1ke2y 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
Str1ke2y ago
Because it feels kinda strange
phaseshift
phaseshift2y ago
Look how you defined current
Str1ke
Str1ke2y ago
Yes I don't get it
phaseshift
phaseshift2y ago
What's the difference between that and your array?
Str1ke
Str1ke2y ago
Good question
phaseshift
phaseshift2y ago
You can't see any difference?
Str1ke
Str1ke2y ago
I can see a difference I defined the TvWeek and didn't the " current"
phaseshift
phaseshift2y ago
You did
Str1ke
Str1ke2y 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
Str1ke2y 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
Str1ke2y 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
Str1ke2y 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
Str1ke2y 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
Str1ke2y ago
Yes
phaseshift
phaseshift2y ago
You pass in arguments, then assign the value to class fields
Str1ke
Str1ke2y 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
Str1ke2y ago
Can you please explain that?
phaseshift
phaseshift2y ago
Ie you still need arrProg
Str1ke
Str1ke2y ago
Oh, so define this array as arrProg in the constructor?
phaseshift
phaseshift2y ago
No Probably best if you look up constructors
Str1ke
Str1ke2y 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
Str1ke2y 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
More Posts
❔ Are there standards to whether someone should use fields or properties?I always spend a second thinking if I should use fields or properties when I am making classes. I'm ❔ Recommended Session Approach for WebApplication (React frontend, WebApi backend)Hey everyone. I'm making a web application with a React frontend and a .NET WebApi backend. Was wond❔ (Very beginner ) - For some reasons I get CS0019 error❔ Smart way to code an insurance pricing calculation with explanation?I'm refactoring a ~60% correct pricing method. To give you an idea its currently like 1000 lines of ❔ I received an assignment that I don't know how to get aroundThe assignment goes like this : Application for managing bank accounts for individua❔ How can I create a UML Class Diagram from an existing .NET 6 Project?I have a project that has grown to a larger size than I had anticipated. I want to generate a UML Cl❔ PM update-database not working in Package manager console.The add-migration was successful but update-data-base says server not found I wrote the correct name✅ MongoDB Document NameSo I'm learning MongoDB.Driver in DotNet, and I'm figuring things out. But I got stuck on this littl❔ What architecture is recommended for a new (to be enterprise) project?I've decided to opt for a monolith. What I see most commonly around here is vertical slice, clean an❔ Entity Framework relationship problem between entities.Hello, I am currently doing an Entity Framework internship and I have a relationship problem between