C
C#•4d ago
DrinkWater623

Null Warnings

I am making a class with 3 overloaded constructor, but I cannot bring myself to put the same code in 3 constructors, so I did it like this:
public enum InsertMode
{
Append = 0,
Truncate = 1,
ReCreate = -1
}
public CsvFileInfo File { get; private set; } //init keyword - see if useful here
public SqlTable Tb { get; private set; }
public string ErrMsg { get; private set; }
public InsertMode AppendMode { get; set; }

private void Initialize ( FileInfo fileInfo, SqlTable tableObj, InsertMode insertMode, bool throwExceptions )
{
ErrMsg = "";
AppendMode = insertMode;
File = new CsvFileInfo(fileInfo, 100);
Tb = tableObj;

if ( !InitialValidation() )
if ( throwExceptions ) throw new Exception($"Error: {ErrMsg}");
else return;

}
//============================================================================================
public Text2Sql ( FileInfo fileInfo, SqlTable tableObj, InsertMode insertMode, bool throwExceptions )
{
Initialize(fileInfo, tableObj, insertMode, throwExceptions);

}
public Text2Sql ( FileInfo fileInfo, SqlTable tableObj, bool throwExceptions )
{
Initialize(fileInfo, tableObj, InsertMode.Append, throwExceptions);

}
public Text2Sql ( FileInfo fileInfo, SqlTable tableObj ) => Initialize(fileInfo, tableObj, InsertMode.Append,true);
public enum InsertMode
{
Append = 0,
Truncate = 1,
ReCreate = -1
}
public CsvFileInfo File { get; private set; } //init keyword - see if useful here
public SqlTable Tb { get; private set; }
public string ErrMsg { get; private set; }
public InsertMode AppendMode { get; set; }

private void Initialize ( FileInfo fileInfo, SqlTable tableObj, InsertMode insertMode, bool throwExceptions )
{
ErrMsg = "";
AppendMode = insertMode;
File = new CsvFileInfo(fileInfo, 100);
Tb = tableObj;

if ( !InitialValidation() )
if ( throwExceptions ) throw new Exception($"Error: {ErrMsg}");
else return;

}
//============================================================================================
public Text2Sql ( FileInfo fileInfo, SqlTable tableObj, InsertMode insertMode, bool throwExceptions )
{
Initialize(fileInfo, tableObj, insertMode, throwExceptions);

}
public Text2Sql ( FileInfo fileInfo, SqlTable tableObj, bool throwExceptions )
{
Initialize(fileInfo, tableObj, InsertMode.Append, throwExceptions);

}
public Text2Sql ( FileInfo fileInfo, SqlTable tableObj ) => Initialize(fileInfo, tableObj, InsertMode.Append,true);
But I get CS8618 non-nullable property warnings. How do I get rid of the warnings? Or is there a better way?
15 Replies
Angius
Angius•4d ago
Make your properties required
DrinkWater623
DrinkWater623•4d ago
I am not sure what you mean. I changed the optional part on all the constructor, and the warning is the same. It does not know that the method called handles all of the initialization. Can you show me what you mean?
Angius
Angius•4d ago
public required string ErrMsg { get; private set; }
DrinkWater623
DrinkWater623•4d ago
Thanks, Any other solutions. I am on version 10.0 and that option is not available.
Angius
Angius•4d ago
Ah, oof In that case, I'd just throw away the Initialize method and just use constructors
Jimmacle
Jimmacle•4d ago
is there a reason you aren't calling the other constructors with this? yeah
Jimmacle
Jimmacle•4d ago
SetsRequiredMembersAttribute Class (System.Diagnostics.CodeAnalysis)
Specifies that this constructor sets all required members for the current type, and callers do not need to set any required members themselves.
Angius
Angius•4d ago
public Foo(int a, string b, bool c)
{
// ...
}
public Foo(int a, string b) : this(a, b, true){}
public Foo(int a) : this(a, "hello", false){}
public Foo(int a, string b, bool c)
{
// ...
}
public Foo(int a, string b) : this(a, b, true){}
public Foo(int a) : this(a, "hello", false){}
: base will be of use uh
DrinkWater623
DrinkWater623•4d ago
I tried to call the other constructors first, but it said, cannot call it like a method
Jimmacle
Jimmacle•4d ago
right, you have to call it like Z showed
Angius
Angius•4d ago
: this
DrinkWater623
DrinkWater623•4d ago
THANK YOU...., it is happy now
public Text2Sql ( FileInfo fileInfo, SqlTable tableObj, InsertMode insertMode, bool throwExceptions )
{
ErrMsg = "";
AppendMode = insertMode;
File = new CsvFileInfo(fileInfo, 100);
Tb = tableObj;

if ( !InitialValidation() )
if ( throwExceptions ) throw new Exception($"Error: {ErrMsg}");
else return;

}
public Text2Sql ( FileInfo fileInfo, SqlTable tableObj, bool throwExceptions ) : this(fileInfo, tableObj, InsertMode.Append, throwExceptions) { }
public Text2Sql ( FileInfo fileInfo, SqlTable tableObj ) : this(fileInfo, tableObj, InsertMode.Append, true){}
public Text2Sql ( FileInfo fileInfo, SqlTable tableObj, InsertMode insertMode, bool throwExceptions )
{
ErrMsg = "";
AppendMode = insertMode;
File = new CsvFileInfo(fileInfo, 100);
Tb = tableObj;

if ( !InitialValidation() )
if ( throwExceptions ) throw new Exception($"Error: {ErrMsg}");
else return;

}
public Text2Sql ( FileInfo fileInfo, SqlTable tableObj, bool throwExceptions ) : this(fileInfo, tableObj, InsertMode.Append, throwExceptions) { }
public Text2Sql ( FileInfo fileInfo, SqlTable tableObj ) : this(fileInfo, tableObj, InsertMode.Append, true){}
JP
JP•4d ago
Instead of using an initializer function, I'd use the all-params constructor, calling it from the other ctor implementations:
public class Text2Sql
{
public enum InsertMode
{
Append = 0,
Truncate = 1,
ReCreate = -1
}
public CsvFileInfo File { get; private set; } //init keyword - see if useful here
public SqlTable Tb { get; private set; }
public string ErrMsg { get; private set; }
public InsertMode AppendMode { get; set; }

//============================================================================================
public Text2Sql ( FileInfo fileInfo, SqlTable tableObj, InsertMode insertMode, bool throwExceptions )
{
ErrMsg = "";
AppendMode = insertMode;
File = new CsvFileInfo(fileInfo, 100);
Tb = tableObj;

if (throwExceptions && !InitialValidation()) {
throw new Exception($"Error: {ErrMsg}");
}
}

public Text2Sql ( FileInfo fileInfo, SqlTable tableObj, bool throwExceptions )
: this(fileInfo, tableObj, InsertMode.Append, throwExceptions)
{ }

public Text2Sql(FileInfo fileInfo, SqlTable tableObj)
: this(fileInfo, tableObj, InsertMode.Append, true)
{ }
}
public class Text2Sql
{
public enum InsertMode
{
Append = 0,
Truncate = 1,
ReCreate = -1
}
public CsvFileInfo File { get; private set; } //init keyword - see if useful here
public SqlTable Tb { get; private set; }
public string ErrMsg { get; private set; }
public InsertMode AppendMode { get; set; }

//============================================================================================
public Text2Sql ( FileInfo fileInfo, SqlTable tableObj, InsertMode insertMode, bool throwExceptions )
{
ErrMsg = "";
AppendMode = insertMode;
File = new CsvFileInfo(fileInfo, 100);
Tb = tableObj;

if (throwExceptions && !InitialValidation()) {
throw new Exception($"Error: {ErrMsg}");
}
}

public Text2Sql ( FileInfo fileInfo, SqlTable tableObj, bool throwExceptions )
: this(fileInfo, tableObj, InsertMode.Append, throwExceptions)
{ }

public Text2Sql(FileInfo fileInfo, SqlTable tableObj)
: this(fileInfo, tableObj, InsertMode.Append, true)
{ }
}
er, I don't know how to scroll, evidently oh, the conversation happened while I was typing it 😆
DrinkWater623
DrinkWater623•4d ago
Since I have you all here... should I put that enum outside of the class? Not sure how I can call it outside of the class... should or can I add Static to it? but I want the user (me) to indicate one of the 3
JP
JP•4d ago
enum should be able to be referenced like Text2Sql.InsertMode Where it lives is kind of your preference imo I'm also pretty sure enums are implicitly static in the way c# uses the term
Want results from more Discord servers?
Add your server