1

How do I init more than 1 variable in C#?

class file_list_output
{
   public file_list_output(ListView v, const int max) => veiw = v => max_ext_allowed = max;
}

cant quite figure it out

Alex K.
  • 171,639
  • 30
  • 264
  • 288
joe blogs
  • 142
  • 4
  • 16

1 Answers1

1

You need to do it like this:

So your class has two properties:

public int Max_ext_allowed {get; private set;}
public ListView View {get; private set;}

//To do an expression bodied constructor
//The fat arrow => is replacing the typical { }
//And the next portion (View, Max_ext_allowed) is saying these are the properties being initialized
//Finally the last portion (v, max) is the set of values to set the properties to
public file_list_input(ListView v, int max) => (View, Max_ext_allowed) = (v, max); 
Ryan Wilson
  • 10,223
  • 2
  • 21
  • 40
  • thanks Ryan, not sure im a big fan of c# yet, time will tell – joe blogs Jun 06 '18 at 13:50
  • @joeblogs You're welcome. I updated this to explain it better with more comments. IF you are happy with my answer, please mark it as the accepted answer. :) – Ryan Wilson Jun 06 '18 at 13:51
  • cheers for this, public int max_ext_allowed { get; private set; }. didnt no about that one – joe blogs Jun 06 '18 at 13:57
  • @joeblogs Yeah, its basically short hand for having a private variable with just a get method, the value can only be set at initialization. – Ryan Wilson Jun 06 '18 at 14:03
  • where may this error be coming from. System>ValueTuple2 is not defined or imported ------(v, max); is showing as the culprit – joe blogs Jun 06 '18 at 14:10
  • @joeblogs That last portion is using what is called a ValueTuple, you need to be using a newer version of .Net or import a package through Nuget, here is a post which explains how to do that (https://stackoverflow.com/questions/38382971/predefined-type-system-valuetuple%C2%B42%C2%B4-is-not-defined-or-imported) – Ryan Wilson Jun 06 '18 at 14:13
  • :) 4.6.1, windows all new to me. if i write a program and want it to run on win7, all i have to do is download 4.7.?? net version on host computer for it to work prior to running. is that correct – joe blogs Jun 06 '18 at 14:17
  • Depending on the version of .Net your application is targeting (or using), yes the target computer will need to have that version of .Net installed. With the new .Net Core, I believe alot of those issues should go away. – Ryan Wilson Jun 06 '18 at 14:21
  • thanks, could you send me a link as how to format posts?? – joe blogs Jun 06 '18 at 14:24
  • @joeblogs What do you mean? Like format your code on a StackOverflow post? – Ryan Wilson Jun 06 '18 at 14:26
  • yup newbie ask. 4.7.1 solved the problem – joe blogs Jun 06 '18 at 14:28
  • @joeblogs Either place 4 whitespace characters before a line of text or you can highlight a portion of your text while writing in the box and use the {} icon button to do the indentation. – Ryan Wilson Jun 06 '18 at 14:29
  • cheers, thanks man. – joe blogs Jun 06 '18 at 14:34
  • @joeblogs Anytime. Happy coding! :P – Ryan Wilson Jun 06 '18 at 14:34