C
C#2y ago
Mathall

method with object lists error

Hey all, new to C# and am trying to learn some basics, so im starting by making a trivia game, however im running into an error and i'm not sure why, any help would be greatly appreciated!
No description
54 Replies
Mathall
MathallOP2y ago
going to add another part later that goes through the lists and randomly picks and writes a questions
Angius
Angius2y ago
What is the error?
Mathall
MathallOP2y ago
I'm not sure to be honest
No description
BeyondMo
BeyondMo2y ago
Can you send the whole code snippet
Mathall
MathallOP2y ago
one moment
Mathall
MathallOP2y ago
oop sorry
Angius
Angius2y ago
$code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Angius
Angius2y ago
Or $paste more like
MODiX
MODiX2y ago
If your code is too long, you can post to https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code!
Angius
Angius2y ago
And the whole thing. My suspicion is you have something like a variable inside of a namespace, a semicolon after an if(), something like that
Mathall
MathallOP2y ago
BlazeBin - tkfiymhsvnwo
A tool for sharing your source code with the world!
Mathall
MathallOP2y ago
would you also like the Question class?
Mathall
MathallOP2y ago
BlazeBin - bhyzkojmfeuu
A tool for sharing your source code with the world!
Mathall
MathallOP2y ago
well there it is if needed
Angius
Angius2y ago
Seems... fine Does it build?
Mathall
MathallOP2y ago
nope
Angius
Angius2y ago
Even from the CLI, with dotnet build?
Mathall
MathallOP2y ago
let me try nope
Mathall
MathallOP2y ago
No description
Mathall
MathallOP2y ago
I understant the null pointer erros just not the namespace stuff
BeyondMo
BeyondMo2y ago
You cant use public inside your function i guess
Angius
Angius2y ago
Oooooh Good catch! Yeah, local variables don't have access modifiers They're local
BeyondMo
BeyondMo2y ago
And i recommend u to use .add to ur list
Mathall
MathallOP2y ago
Oh, interesting, thanks
Angius
Angius2y ago
I don't recommend .Add at all, it would make the whole thing unnecessarily verbose
Mathall
MathallOP2y ago
i got rid of the publics and it works thanks!
Angius
Angius2y ago
I'd even shorten it even more with target-type new and collection expressions
Mathall
MathallOP2y ago
im thinking about changing it to an array once i confirm all the questions im going to use
Angius
Angius2y ago
Or even better, by loading the questions from a json file
Mathall
MathallOP2y ago
yeah no clue what that is lol was thinking about this but still inexperienced with json and just wanted to get a working thing
Angius
Angius2y ago
List<Thing> things = [
new(1, 2, 3, "Goodbye"),
new(4, 5, 6, "Hello"),
];
List<Thing> things = [
new(1, 2, 3, "Goodbye"),
new(4, 5, 6, "Hello"),
];
Mathall
MathallOP2y ago
oh interesting, so you dont need to define it as a new list?
Angius
Angius2y ago
It is a new list
BeyondMo
BeyondMo2y ago
[] does that for u
Angius
Angius2y ago
It already has the type information in one place
Mathall
MathallOP2y ago
oh, i see
Angius
Angius2y ago
It knows it's a list, and a list of things
Mathall
MathallOP2y ago
would it be the same for an array? or similar with an array syntax
Angius
Angius2y ago
Should be
MODiX
MODiX2y ago
Angius
REPL Result: Success
record Thing(int Foo, string Name);

Thing[] foos = [
new(1, "a"),
new(2, "b"),
];
record Thing(int Foo, string Name);

Thing[] foos = [
new(1, "a"),
new(2, "b"),
];
Compile: 370.544ms | Execution: 66.123ms | React with ❌ to remove this embed.
Angius
Angius2y ago
ye
Mathall
MathallOP2y ago
what is the "record" i havent seen that before
Angius
Angius2y ago
It's basically a shorthand for creating a class + constructor + properties + other stuff
Mathall
MathallOP2y ago
neat, thanks I appreciate the help
MODiX
MODiX2y ago
Angius
sharplab.io (click here)
record Thing(int Foo, string Name);
record Thing(int Foo, string Name);
Try the /sharplab command! | React with ❌ to remove this embed.
Angius
Angius2y ago
Here's what this record compiles into
Mathall
MathallOP2y ago
So would one moment let me write this out lol
c#
class Thing
{
int Foo;
string Name;
public Thing(int Foo, string Name) {
this.Foo = Foo;
this.Name = Name;
}
}
c#
class Thing
{
int Foo;
string Name;
public Thing(int Foo, string Name) {
this.Foo = Foo;
this.Name = Name;
}
}
be the same as the record
Angius
Angius2y ago
class Thing
{
public int Foo { get; init }
public string Name { get, init; }
public Thing(int Foo, string Name) {
this.Foo = Foo;
this.Name = Name;
}
}
class Thing
{
public int Foo { get; init }
public string Name { get, init; }
public Thing(int Foo, string Name) {
this.Foo = Foo;
this.Name = Name;
}
}
would be closer But yes
Mathall
MathallOP2y ago
ah yea, i havent really gone into the whole get set finish whatnots
Angius
Angius2y ago
Properties
Mathall
MathallOP2y ago
Alright well thanks for your help I really appreciate
Angius
Angius2y ago
Anytime :Ok:

Did you find this page helpful?