Cannot fully use returned string value from my function.

dimaomni

Новичок
Регистрация
24.11.2018
Сообщения
11
Благодарностей
0
Баллы
1
Hi,
I use a function in one C# block like
Код:
/* Function to find a selller   */
project.Context["CheckSeller"] = (Func<string>)(() => {
   
    //get brand name and counter from project
    ILocalVariable brand_name = project.Variables["brand_name"];

    int counter = int.Parse(project.Variables["counter"].Value);

    // All Departments.   Check if brand is a seller in All Departments. If name is similiar, than it stored in Brands table F   

    string empty_string = "";
    var table_brands = project.Tables["brands"];
    string seller1 = "";
    string to_return = ""; //return value
    string pattern = @brand_name.Value; //for regex


    string input = ""; //regex
    //ILocalVariable brand_is_seller = project.Variables["brand_is_seller"];

    HtmlElement seller1_xpath_result = instance.ActiveTab.FindElementByXPath("//h4[contains(., 'Seller')]/following-sibling::ul[1]/div/li[1]",0);
    seller1 = seller1_xpath_result.GetAttribute("innerText");

    //  if sellers' checkboxes appear in all Departmens after selecting Brand's checkbox, try to find sellere name 
    if (!seller1_xpath_result.IsVoid){
               
            input = seller1;
            //compare brand name to alternative brand name
             Match m = Regex.Match(input, pattern, RegexOptions.IgnoreCase);
           
                if(m.Value !=""){
                    //project.SendWarningToLog(m.Value, "The brand has the seller with name: ", true);
                    //table_brands.SetCell("F", counter, m.Value);
                    to_return =  m.Value;
                   
                } else {to_return = "";}

    } 
    return to_return;
     //return input;

});//end function CheckSeller
which returns a string value. I call it from another c# block like this
Код:
string is_seller_on_page = project.Context["CheckSeller"]();//function is in another block
And it does return a string, as I can output this value to log. However, when I try to use the variable is_seller_on_page in IF-clause, I get an error "Perform the CSharp OwnCode object reference not set to an instance of an object."

Is it a bug or something with my code?

I used
Код:
if (is_seller_on_page == "" ){
to check if I receive the empty string.

now I rewrote a bit
Код:
get_is_seller_on_page = is_seller_on_page.CompareTo("");
which works,
but I am curious, why I have an issue with object if the function returns a string?

Another question is it possible to get a row number when this kind of error happens? Because to find out it, I need to copy/paste code with output to log after every action in code, which is inconvenient.
Thanks.
 

EtaLasquera

Client
Регистрация
02.01.2017
Сообщения
526
Благодарностей
112
Баллы
43
Try to use project.Variables["CheckSeller"].Value instead project.Context["CheckSeller"]
Context uses IContext model, that represents a collection of shared objects that can be accessed by name.
Also to compare strings is more convenient to use:
Код:
string s = "";
if (s.Equals("")){
  s = "hello world";
}
if (!s.Equals(""){
  s = "goodbye world";
}
 

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)