0

I have this piece of code

 if ( x.str_NombreVariable == "User::v_sRutaArchivo")
                            x.str_ValorVariable += str_Archivo;
                        else if ( x.str_NombreVariable == "User::v_IdBitacoraCarga")
                        x.str_ValorVariable = _Bcarga.IdBitacoraCarga.ToString(); 

as you can see I do some comparissions: x.str_NombreVariable == "User::v_sRutaArchivo" or x.str_NombreVariable == "User::v_IdBitacoraCarga"

problem is that I have values into x.str_NombreVariable who doesn´t have "User::" and it don´t match with comparission. So I try to do something like:

"User::"+x.str_NombreVariable == "User::v_sRutaArchivo"

Problem is some x.str_NombreVariable come with "User::" as default so I get:

User::User::NombreVariable 

and it is wrong I only need User::NombreVariable

So in other words: How can I check if x.str_NombreVariable doesnt contains "User::" add it and if it have don´t do anything? it is possible? Regards

3 Answers 3

1

Just check if the string starts with User:: and if it does not add it:

if (!x.str_NombreVariable.StartsWith("User::"))
    x.str_NombreVariable = "User::" + x.str_NombreVariable;

Put this code above yours and you should be good.

Edit: I just add missing ')'

Sign up to request clarification or add additional context in comments.

Comments

0
if ("User::v_sRutaArchivo".Contains(x.str_NombreVariable))

This condition will work in any scenario.

2 Comments

but if add string "User::" if it dont have? I want to add it if not
This piece of code works even without modifying the value of the variable. If you need to add the "User::" even after successfully comparing the values, then look at @Timo's answer.
0

Another solution would be to remove the "User::" part from your variable and the hardcoded string:

var nombreVar = x.str_NombreVariable.Replace("User::", "");
if ( nombreVar == "v_sRutaArchivo")
    x.str_ValorVariable += str_Archivo;
else if ( nombreVar == "v_IdBitacoraCarga")
    x.str_ValorVariable = _Bcarga.IdBitacoraCarga.ToString(); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.