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