C
C#2w 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
Mathall2w ago
going to add another part later that goes through the lists and randomly picks and writes a questions
Angius
Angius2w ago
What is the error?
Mathall
Mathall2w ago
I'm not sure to be honest
No description
shanks
shanks2w ago
Can you send the whole code snippet
Mathall
Mathall2w ago
one moment
Mathall
Mathall2w ago
oop sorry
Angius
Angius2w ago
$code
MODiX
MODiX2w 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
Angius2w ago
Or $paste more like
MODiX
MODiX2w 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
Angius2w 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
Mathall2w ago
BlazeBin - tkfiymhsvnwo
A tool for sharing your source code with the world!
Mathall
Mathall2w ago
would you also like the Question class?
Mathall
Mathall2w ago
BlazeBin - bhyzkojmfeuu
A tool for sharing your source code with the world!
Mathall
Mathall2w ago
well there it is if needed
Angius
Angius2w ago
Seems... fine Does it build?
Mathall
Mathall2w ago
nope
Angius
Angius2w ago
Even from the CLI, with dotnet build?
Mathall
Mathall2w ago
let me try nope
Mathall
Mathall2w ago
No description
Mathall
Mathall2w ago
I understant the null pointer erros just not the namespace stuff
shanks
shanks2w ago
You cant use public inside your function i guess
Angius
Angius2w ago
Oooooh Good catch! Yeah, local variables don't have access modifiers They're local
shanks
shanks2w ago
And i recommend u to use .add to ur list
Mathall
Mathall2w ago
Oh, interesting, thanks
Angius
Angius2w ago
I don't recommend .Add at all, it would make the whole thing unnecessarily verbose
Mathall
Mathall2w ago
i got rid of the publics and it works thanks!
Angius
Angius2w ago
I'd even shorten it even more with target-type new and collection expressions
Mathall
Mathall2w ago
im thinking about changing it to an array once i confirm all the questions im going to use
Angius
Angius2w ago
Or even better, by loading the questions from a json file
Mathall
Mathall2w 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
Angius2w 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
Mathall2w ago
oh interesting, so you dont need to define it as a new list?
Angius
Angius2w ago
It is a new list
shanks
shanks2w ago
[] does that for u
Angius
Angius2w ago
It already has the type information in one place
Mathall
Mathall2w ago
oh, i see
Angius
Angius2w ago
It knows it's a list, and a list of things
Mathall
Mathall2w ago
would it be the same for an array? or similar with an array syntax
Angius
Angius2w ago
Should be
MODiX
MODiX2w 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
Angius2w ago
ye
Mathall
Mathall2w ago
what is the "record" i havent seen that before
Angius
Angius2w ago
It's basically a shorthand for creating a class + constructor + properties + other stuff
Mathall
Mathall2w ago
neat, thanks I appreciate the help
MODiX
MODiX2w 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
Angius2w ago
Here's what this record compiles into
Mathall
Mathall2w 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
Angius2w 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
Mathall2w ago
ah yea, i havent really gone into the whole get set finish whatnots
Angius
Angius2w ago
Properties
Mathall
Mathall2w ago
Alright well thanks for your help I really appreciate
Angius
Angius2w ago
Anytime :Ok: