C
C#9mo 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
Angius9mo ago
Uh, this looks like an img element with base-64 image...? Where's the error? Where's the code?
Emelie
Emelie9mo ago
sorry! here it is
Emelie
Emelie9mo ago
No description
Emelie
Emelie9mo ago
The contextual keyword `var' may only appear within a local variable declaration
Angius
Angius9mo ago
Well, yeah, that's true You can use var only for local variables Not for fields or properties
Emelie
Emelie9mo ago
I dont think I get the difference :/
Angius
Angius9mo ago
$structure
MODiX
MODiX9mo 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
Angius9mo ago
Fields and properties are class members Locak variables are inside of methods
Emelie
Emelie9mo ago
oh so I should create a method if I want to use var this way?
Exi
Exi9mo ago
or give it a specific type like Section and dont use var
Angius
Angius9mo 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
Emelie9mo 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
Exi9mo 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
Angius9mo 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
Emelie9mo 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
Exi9mo ago
imagine if u have fields on class level, they usually need a type when getting initialized and var doesnt provide that
Emelie
Emelie9mo ago
which is what I was trying to do?
Exi
Exi9mo ago
and they also cant get modifiers like private, public and stuff and that doesnt work out as well
Emelie
Emelie9mo ago
how come var cant get those modifiers? sorry if thats a stupid question
Angius
Angius9mo ago
It's not that var can't get those modifiers It's that var cannot be used on the class-level
Exi
Exi9mo 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
Angius9mo 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
Emelie9mo 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
Angius9mo ago
You created a field, yes It's a list of Sections And contains multiple instances of Section class
Emelie
Emelie9mo ago
and if I want to add a constructor then I need to leave out this list right?
Angius
Angius9mo ago
What do you mean?
Emelie
Emelie9mo ago
im not sure 😂 just feeling confused
Exi
Exi9mo ago
what are you trying to do?
Emelie
Emelie9mo 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
Thinker9mo 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
Angius9mo 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
Emelie9mo ago
Yes, that last part😅 about Foo
Angius
Angius9mo ago
Then, yeah, using a constructor would probably be best
Emelie
Emelie9mo ago
That really confuses me
Angius
Angius9mo ago
And passing the list there Maybe exposing the list as a property, so it can be added to at any time
Emelie
Emelie9mo ago
that makes sense, I think Anyhow. Thanks once again! Im going to resume to coding 😄
Accord
Accord9mo 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.