How can I initialize an empty string whose value is "" but in this line of code:
Presenter.OnRazonSocialChanged(txtRazonSocial.Text);
I had solved it in the constructor of the Presenter in the following way:
public ProveedorPresenter(Proveedor model, ProveedorView view)
{
_model = model;
_view = view;
_model.RazonSocial = "";
_model.DocumentoIdentidad = "";
_model.Direccion = "";
_model.Fijo = "";
_model.Celular = "";
_model.Representante = "";
_model.Email = "";
_view.Presenter = this;
}
Without initializing the variables as empty, it gives me the following error:
As you will see I depend on the TextChanged event, what would be the solution?
Thank you all for answering, I promise to improve the quality of my questions and spend more time preparing them. Thanks to all of you I managed to solve it this way.
Create a method in the Presenter called Initialize Model
public void InicializarNodelo()
{
_model.RazonSocial = string.Empty;
_model.DocumentoIdentidad = string.Empty;
_model.Direccion = string.Empty;
_model.Fijo = string.Empty;
_model.Celular = string.Empty;
_model.Representante = string.Empty;
_model.Email = string.Empty;
}
And I call it in the load of my View(form)
private void ProveedorView_Load(object sender, EventArgs e)
{
Presenter.InicializarNodelo();
}
You can use null coalescing operator
??
In case the property
Text
isnull
the empty string will be used""
.It seems to me that what you want is to validate the content of the
TextBox
:Your question seems to be related to Implementing the MVP Design Pattern so I'll answer it with that context in mind.
It seems that what you are trying to do is assign the empty string to
txtRazonSocial.Text
and at the same time pass it to thePresenter.OnRazonSocialChanged()
. You can easily achieve this with the following expression:either
However according to the following code from the other question.
and its use here
It is not clear to me why you want to pass the empty string in the handler every time the Company Name is modified, the objective of the method
txtRazonSocial_TextChanged
is to notify thePresenter
control that the value has changed and pass the new value so that it in turn can set it in theModel
.In any case, if you want to initialize the control as an empty string, you should set it to
Proveedor
(model) before initializing the controls with the model values.Here is my answer, based on the other answers ( enrique7mc and Carlos ) (I can't see the image)
I understand that you want to prevent the
string
one you send as a parameter to the function fromOnRazonSocialChanged(string)
being null.Let's start by saying you have this:
Where you are assigning your
model
type parameterProveedor
to the internal field_model
of the classProveedorPresenter
and then you assign all the values to""
Which practically does not make sense (at least for me) , the most correct thing would be to create a constructor in the following way:Where you assign a parameter
model
with a default valuenull
so that if you do not specify it in the constructor call, it will assign_model
the value of a new provider to the field, so that the values will be initialized to""
(If you specified it in the empty constructor ofProveedor
)Then in your method
OnRazonSocialChanged(string)
you do this:And you will have what you need so that the how does not leave
string
younull
.