❔ 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
Uh, this looks like an
img
element with base-64 image...?
Where's the error?
Where's the code?sorry! here it is
The contextual keyword `var' may only appear within a local variable declaration
Well, yeah, that's true
You can use
var
only for local variables
Not for fields or propertiesI dont think I get the difference :/
$structure
Fields and properties are class members
Locak variables are inside of methods
oh
so I should create a method if I want to use var this way?
or give it a specific type like Section and dont use var
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 askingI 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
List<Sections> sections = new List<Sections>
{
new Section(),
new Section(),
};
Did u try that?
fill the new section inside like it fits to u
Or
if you don't want to be repeating the type
I didn't want to give out a straight answer but aight, fine
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?
imagine if u have fields on class level, they usually need a type when getting initialized and var doesnt provide that
which is what I was trying to do?
and they also cant get modifiers like private, public and stuff and that doesnt work out as well
how come var cant get those modifiers?
sorry if thats a stupid question
It's not that var can't get those modifiers
It's that var cannot be used on the class-level
Mh I'm not a expert on this, I just think it's designed like that, maybe @ZZZZZZZZZZZZZZZZZZZZZZZZZ knows that?
never seen that
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 mostwell, 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?
You created a field, yes
It's a list of
Section
s
And contains multiple instances of Section
classand if I want to add a constructor
then I need to leave out this list right?
What do you mean?
im not sure 😂 just feeling confused
what are you trying to do?
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
It's apparently because it would introduce possible recursion when binding fields. Eg.
Oooh, makes sense
Well, you... do have a field that contains objects
Or do you mean, having a list of
Foo
s inside of the class Foo
?Yes, that last part😅 about Foo
Then, yeah, using a constructor would probably be best
That really confuses me
And passing the list there
Maybe exposing the list as a property, so it can be added to at any time
that makes sense, I think
Anyhow. Thanks once again! Im going to resume to coding 😄
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.