C
C#14mo ago
Emelie

❔ Trying to a initialize a list of objects, but keep getting this error:

I´ve been googling around how I can solve it, but its not working out very well... does someone here know why I get this?
38 Replies
Angius
Angius14mo ago
Uh, this looks like an img element with base-64 image...? Where's the error? Where's the code?
Emelie
EmelieOP14mo ago
sorry! here it is
Emelie
EmelieOP14mo ago
No description
Emelie
EmelieOP14mo ago
The contextual keyword `var' may only appear within a local variable declaration
Angius
Angius14mo ago
Well, yeah, that's true You can use var only for local variables Not for fields or properties
Emelie
EmelieOP14mo ago
I dont think I get the difference :/
Angius
Angius14mo ago
$structure
MODiX
MODiX14mo ago
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;
protected double protectedField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;
protected double protectedField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
Angius
Angius14mo ago
Fields and properties are class members Locak variables are inside of methods
Emelie
EmelieOP14mo ago
oh so I should create a method if I want to use var this way?
Exi
Exi14mo ago
or give it a specific type like Section and dont use var
Angius
Angius14mo ago
Well, do you want sections to be available on class-level or do you want it to be local to a method? That's the question you should be asking
Emelie
EmelieOP14mo ago
I want to initialize these objects, fill a combobox with the different sections available, and when the user selects a section, a new window with the specific info on this section should show up if that makes sense giving it a specific type like Section just gives me a bunch of other errors, unfortunately Im doing a WPF app
Exi
Exi14mo ago
List<Sections> sections = new List<Sections> { new Section(), new Section(), }; Did u try that? fill the new section inside like it fits to u
Angius
Angius14mo ago
Or
List<Sections> sections = new()
{
new Section(),
new Section(),
};
List<Sections> sections = new()
{
new Section(),
new Section(),
};
if you don't want to be repeating the type I didn't want to give out a straight answer but aight, fine
Emelie
EmelieOP14mo ago
ok so it works now! thank you. Just trying to understand it a bit more we can´t use var when defining fields and properties of a class?
Exi
Exi14mo ago
imagine if u have fields on class level, they usually need a type when getting initialized and var doesnt provide that
Emelie
EmelieOP14mo ago
which is what I was trying to do?
Exi
Exi14mo ago
and they also cant get modifiers like private, public and stuff and that doesnt work out as well
Emelie
EmelieOP14mo ago
how come var cant get those modifiers? sorry if thats a stupid question
Angius
Angius14mo ago
It's not that var can't get those modifiers It's that var cannot be used on the class-level
Exi
Exi14mo ago
Mh I'm not a expert on this, I just think it's designed like that, maybe @ZZZZZZZZZZZZZZZZZZZZZZZZZ knows that? never seen that
Angius
Angius14mo ago
Probably some legacy reasons ¯\_(ツ)_/¯ The fact remains, var is only valid for local variables Class-level variables (fields, properties) can use target-type new() at most
Emelie
EmelieOP14mo ago
well, guess I dont need to know all of the details anyways! 😄 some things are better to just memorize just one more thing is it correct to say that I have created a fieldvariable which contains objects of the class it defines?
Angius
Angius14mo ago
You created a field, yes It's a list of Sections And contains multiple instances of Section class
Emelie
EmelieOP14mo ago
and if I want to add a constructor then I need to leave out this list right?
Angius
Angius14mo ago
What do you mean?
Emelie
EmelieOP14mo ago
im not sure 😂 just feeling confused
Exi
Exi14mo ago
what are you trying to do?
Emelie
EmelieOP14mo ago
I want to initialize objects of the Section class I am just confused about that its possible to have a field variable in the class, which in turn contains objects of that class Im probably overthinking it
Thinker
Thinker14mo ago
It's apparently because it would introduce possible recursion when binding fields. Eg.
private var x = y;
private var y = x;
private var x = y;
private var y = x;
Angius
Angius14mo ago
Oooh, makes sense Well, you... do have a field that contains objects Or do you mean, having a list of Foos inside of the class Foo?
Emelie
EmelieOP14mo ago
Yes, that last part😅 about Foo
Angius
Angius14mo ago
Then, yeah, using a constructor would probably be best
Emelie
EmelieOP14mo ago
That really confuses me
Angius
Angius14mo ago
And passing the list there Maybe exposing the list as a property, so it can be added to at any time
Emelie
EmelieOP14mo ago
that makes sense, I think Anyhow. Thanks once again! Im going to resume to coding 😄
Accord
Accord14mo 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