I have the following C#-code:
while (...)
{
...(out var information);
dynamic obj = JsonConvert.DeserializeObject(information);
if (obj.Some_Property != null)
{
As you see, it creates an object, based on a string, called information
.
But, that information sometimes is incomplete, so I need to split this function, and I arrive at this:
string partial_information = "";
dynamic obj;
while (...)
{
... (out information);
if (partial_information == "")
{
try
{
obj = JsonConvert.DeserializeObject(information);
}
catch (Newtonsoft.Json.JsonReaderException ex)
// 'information' only contains the first part of the actual information
{
partial_information = information;
}
}
else
{
obj = JsonConvert.DeserializeObject(partial_information + information);
// in the previous loop, some 'information' was written to 'partial_information'.
// Now the concatenation of both is used for deserialising the info.
partial_information = ""; // don't forget to re-initialise afterwards
}
if (obj.Some_Property != null) // <-- Compiler error (!!!)
{
This, however, does not compile: the line if (obj.Some_Property != null)
generates a CS1065
compiler error: Use of unassigned local variable 'obj'
.
In my opinion, this makes no sense, as obj
is declared even outside of the whole while
-loop.
How can I handle this?
Thanks in advance