Initialize variable string

4

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 Presenter's constructor 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, I get the following error:

As you will see from the TextChanged event, what would be the solution?

Thanks to everyone for answering, I promise to improve the quality of my questions and devote more time to elaborate them. Thanks to all of you, I managed to solve it in 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 Vista (form)

private void ProveedorView_Load(object sender, EventArgs e)
    {
        Presenter.InicializarNodelo();
    }
    
asked by Pedro Ávila 03.06.2016 в 02:58
source

4 answers

2

Your question seems to be related to Implementing pattern of MVP design so I will answer taking into account that context.

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 the Presenter.OnRazonSocialChanged() method. You can do that easily with the following expression:

Presenter.OnRazonSocialChanged(txtRazonSocial.Text = "");

or

Presenter.OnRazonSocialChanged(txtRazonSocial.Text = String.Empty);

However according to the second code of the other question.

public class ProveedorPresente
{
    private Proveedor _model;
    private ProveedorView _view;

    ...

    public void OnRazonSocialChanged(string razonSocial)
    {
        _model.RazonSocial = razonSocial;
    }

    ...
}

and its use here

public partial class ProveedorView : Form
{
    public ProveedorPresente Presenter { get; set; }
    public ProveedorView()
    {
        InitializeComponent();
    }

    ...

    private void txtRazonSocial_TextChanged(object sender, EventArgs e)
    {
        Presenter.OnRazonSocialChanged(txtRazonSocial.Text);
    }

    ...
}

It is not clear to me why you want to pass the empty string in the handler every time the Social Reason is modified, the goal of the txtRazonSocial_TextChanged method is to notify the Presenter that the value of the control has changed and pass it the new value so that this in turn can set it in Model .

In any case if you want to initialize the control as an empty string you should set it in Proveedor (model) before initializing the controls with the values of the model.

    
answered by 03.06.2016 / 09:47
source
5

You can use null coalescing operator ??

Presenter.OnRazonSocialChanged(txtRazonSocial.Text ?? "");

In case the property Text is null , it will use the empty string "" .

    
answered by 03.06.2016 в 05:23
2

I think you want to validate the content of TextBox :

        if (string.IsNullOrWhiteSpace(txtRazonSocial.Text)) {
            // contenido del TextBox Vacio !
        }else{
Presenter.OnRazonSocialChanged(txtRazonSocial.Text);
        }
    
answered by 03.06.2016 в 03:33
1

Here is my answer, based on the other answers ( enrique7mc and Carlos ) (I can not see the image)

I understand that you want to prevent the string that you send per parameter to the OnRazonSocialChanged(string) function from being null.

Let's start by saying that you have this:

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;
}

Where are you assigning your parameter model of type Proveedor to internal field _model of class ProveedorPresenter and then assign all values to "" What practically does not make sense (At least for me) , the most correct thing would be to create a constructor of the following form:

public ProveedorPresenter(ProveedorView view, Proveedor model = null)
{
    _model = (model != null) ? model : new Proveedor();
    // ...
}

Where you assign a parameter model with a default value null so that if you do not specify it in the call to the constructor assign the field _model the value of a new provider, so that the values are going to be initialized to "" (If you specified it in the empty constructor of Proveedor )

Then, in your method OnRazonSocialChanged(string) you do this:

public void OnRazonSocialChanged(string s)
{
    _model.RazonSocial = (string.IsNullOrEmpty(s) || string.IsNullOrWhiteSpace(s)) ? "" : s;
}

And you'll have what you need so you do not leave the string as null .

    
answered by 03.06.2016 в 18:32