0

I have the following dictionary:

 var dict = new Dictionary<string, string> {{"Name", "John"}, {"Age", "28"}};

I want to convert it to a FormField object array as follows:

public class FormField 
{
   public string FieldName { get; set;}
   public string FieldValue { get; set;}
}

I can achieve this by doing the following:

dict.Select(p => new FormField { FieldName = p.Key, FieldValue = p.Value.ToString() }).ToArray()

Now I need to assign this to a view model which has a property as follows:

 FormField[] FormData { get; set;}

What is the correct syntax: the following doesnt work?

var viewModel = new MyViewModel 
{
   FormData = new FormFeild[0] dict.Select(p => new FormField { FieldName = p.Key, FieldValue = p.Value.ToString() }).ToArray()

}

What is the correct syntax to initialise and asign the dictionary to FormData viewmodel which is of type array?

adam78
  • 9,668
  • 24
  • 96
  • 207
  • There is a little bit of context missing here. To use `dict` in such way it would have to be initialized *first*. But we cannot see enough of your class design to suggest how to make that sure. – Fildor Sep 18 '17 at 08:59
  • @CodeCaster. This is a specific question about a null reference. I don't think it's fair to call it a duplicate – Yair Halberstadt Sep 18 '17 at 08:59
  • 3
    @YairHalberstadt: The linked duplicate explains what a null reference is, and how to fix it. This seems like it should solve the OPs problem... Null reference exceptions are really easy to debug and you debug them all pretty much the same. The only time I would argue that the linked duplicate might not apply is if you have code outside of your control (eg framework code) throwing the exception where it is out of your control. This doesn't seem to be the case here. – Chris Sep 18 '17 at 09:03
  • 1
    @YairHalberstadt: I agree with you, but the question currently doesn't really address that specific situation. The OP even explicitly formulates his question: "**How do I initialise** and asign the dictionary ...", and this is a trivial issue that is solved by looking up _any_ fix for a nullreference exception (like the duplicate that was linked) – Flater Sep 18 '17 at 09:04
  • @YairHalberstadt To add to Flater's comment: OP obviously knows *what* to do even! But we cannot see enough of his code to tell him where to do it. So only he himself can figure that out. And honestly- it shouldn't be *that* hard. – Fildor Sep 18 '17 at 09:06
  • 1
    Alright. I suppose it isn't a great question. – Yair Halberstadt Sep 18 '17 at 09:07
  • @Fildor I know what a a null reference is - my question is how do I initialise the `FormData` before assigning the dictionary that is converted to an array? – adam78 Sep 18 '17 at 09:07
  • Exactly that is what we are saying. You already know what is wrong and how to fix it. We cannot tell you. Where does the data come from? And when? All these questions are not addressed in your question. Even if we wanted, we couldn't answer you. – Fildor Sep 18 '17 at 09:10
  • @adam78: Why do you want to intialize FormData before you have the dictionary (I think that's what you are saying)? Surely it makes sense to wait until you have the dictionary. Otherwise you just need to assign it again once you have the dictionary... – Chris Sep 18 '17 at 09:18
  • @Chris when I assign it again it I get the object reference error, which means I need to initialise `FormData` before assigning the dictionary. I think my systax is incorrect though which is what the question is really about – adam78 Sep 18 '17 at 09:20
  • Though if you want to initialize FormData before you have the dictionary the answer is `FormData = new FormField[0]`. – Chris Sep 18 '17 at 09:20
  • Can you give a complete example of what you are talking about? You don't need to initialize an object to be able to assign to it later. If you want to use it (ie `viewModel.FormData[0] = foo`) then it must be assigned or you'll get the null reference exception. My understanding is that your assignment though will be `viewModel.FormData = dict.Select(p => new FormField { FieldName = p.Key, FieldValue = p.Value.ToString() }).ToArray()` which doesn't require FormData to be initialized first. – Chris Sep 18 '17 at 09:23
  • `new MyViewModel { FormData = dict.Select(new FormField { ... }).ToArray() }` should just work. If it doesn't, debug to the point where you can find what is null. I would suspect `p.Value.ToString()` to be the only thing to throw. We can't debug this for you. – CodeCaster Sep 18 '17 at 09:27
  • @CodeCaster's assessment seems good. I hadn't noticed the `p.Value.ToString()`. Given the dictionary is of strings to strings the `ToString` isn't even needed there though since `p.Value` will be a string already (and could be null). – Chris Sep 18 '17 at 09:30
  • @Chris you are right one of the values was null which is what was throwing the error. Thanks – adam78 Sep 18 '17 at 10:32
  • @adam78: And this is why you should have read the linked answer and its explanations of how to find the cause of a null reference exception. It would have saved a lot of other people's time. :( – Chris Sep 18 '17 at 10:38
  • @adam78 I know this code - I answered [here](https://stackoverflow.com/questions/46213943/net-convert-map-list-into-object-and-vis-versa) and even included a dictionary initializer! And the call to `ToDictionary()`! But then, I noticed *an identical* question [here](https://stackoverflow.com/questions/46234697/automapper-dictionary-to-object-mapping-not-working). Please don't post the same question over and over, it only created noise. If you get downvoted, fix the questions. As for the syntax to a dictionary initializer, google `dictionary initializer` – Panagiotis Kanavos Sep 18 '17 at 10:55
  • @adam78 as for the syntax error - it should be obvious. You call `.ToArray()`, what's the point of putting `new FormField[0]` in front of it? If you started from an array, why are you trying to store the *dictionary* instead of the original array? – Panagiotis Kanavos Sep 18 '17 at 11:01

0 Answers0