none
Property - function RRS feed

  • Question

  • Hello,
     
    I want to create a property that returns a record to me?
    Return value
    - null
    - Not Included
    - right variable value.
     
    The function works as it should. That fits everything. I just want to get to
    know the other possibility.
    //public string _RefID;
    //public string RefID
    //{
    //    get => _RefID;
    //    set => ListMount.Count != 0 ?
    ListMount.FirstOrDefault().ListComponents.FirstOrDefault().RotId : "Not
    Included";
    // value? 
    //}
    
    
    public string GetRotID()
    {
    string ret = ""; 
    
    // ListMount.Clear(); 
    foreach (var item in ListMount)  // only test for null 
    { 
    item.ListComponents.Clear(); 
    } 
    
    ret = (ListMount.Count != 0) ? 
    ListMount.FirstOrDefault()?.ListComponents.FirstOrDefault()?.RotId : "Not
    Included #####";
    
    return ret; 
    }
     
    Thanks in advance.
     
    Greetings Markus

    Saturday, May 18, 2019 10:34 AM

Answers

  • I think i understand what you're trying to achieve.  Is the below what you are looking for?  

        public class Foo
        {
            public string RotID => GetRotID();
    
            private string GetRotID()
            {
                //do stuff
                var rotId = DoStuffAndGetResults();
    
                return string.IsNullOrEmpty(rotId)
                       ? "NOT FOUND"
                       : rotId;
            }
        }


    ck

    Thursday, May 23, 2019 1:55 AM
  • Hi Markus,
    Yes, it does take some getting used to the new syntax (spelling, as you say ... but the correct word is actually syntax).

    The example you showed with xx ?? -1 is the same as checking for null with an if statement, you are correct.

    The example you showed with Action<int> display s => Console.WriteLine(s) works fine (obviously), but I wouldn't describe the => operator to mean "GoesTo". I mean, it's called a "Lambda expression", but that is hard to define with a single phrase (like Goes To), because if you look at some of the various ways it can be used, I can't think of a single simple word or phrase that defines everything. You've probably already seen this link (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-operator).

    Anyway, I *will* have a great weekend because I am off on a much needed 4 days of vacation! It's long overdue.
    I hope *you* have a great weekend too!!


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, June 1, 2019 1:19 AM
    Moderator

All replies

  • Hi Markus Freitag,

    Thank you for posting here.

    For your question, the code you provide include some variables without definitions. I could not test it.

    Please provide more details.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 20, 2019 7:30 AM
    Moderator
  • Hello Wendy,
    My goal is to use a property to get the value.
    ListMount
      -- Component1
      -- Component2
     
    My idea was to use a property to get the value. 
    I want to return this function as getter. GetRotID()
    //public string _RotID;
    //public string RotID
    //{
    //    get => _RotID;
    //    set => ListMount.Count != 0 ?
    ListMount.FirstOrDefault().ListComponents.FirstOrDefault().RotId : "Not
    Included";
    // value? 
    //}
    
    public string GetRotID()
    {
     string ret = ""; 
     ret = (ListMount.Count != 0) ? 
     ListMount.FirstOrDefault()?.ListComponents.FirstOrDefault()?.RotId : "NotIncluded #####";
     return ret; 
    }
    Thanks in advance.
     
    Greetings Markus


    Wednesday, May 22, 2019 5:08 PM
  • I think i understand what you're trying to achieve.  Is the below what you are looking for?  

        public class Foo
        {
            public string RotID => GetRotID();
    
            private string GetRotID()
            {
                //do stuff
                var rotId = DoStuffAndGetResults();
    
                return string.IsNullOrEmpty(rotId)
                       ? "NOT FOUND"
                       : rotId;
            }
        }


    ck

    Thursday, May 23, 2019 1:55 AM
  • Hello,

    public string RotID => GetRotID();
    
    Right
    I did not know that I could write that. That fits.
    
    
    ListMount
      -- Component1
          RotID, xRef, yRef, zRef
      -- Component2
          RotID, xRef, yRef, zRef
      -- Component3
          RotID, xRef, yRef, zRef
      ...
      -- ComponentN
          RotID, xRef, yRef, zRef
    I have a list of components.
    
    I have to return the first found RotID.
    
    Check 1
      ListMount.Count != 0
     
    Check 2
      ListMount.FirstOrDefault() != null
      
    Check 3
      if (ListMount.FirstOrDefault().ListComponents.FirstOrDefault() != null )
          if ( ListMount.FirstOrDefault().ListComponents.FirstOrDefault().RotID  != null )
    	      return ListMount.FirstOrDefault().ListComponents.FirstOrDefault().RotID;
    		  
    Is that sufficient?
    	  var rotId = ListMount.FirstOrDefault().ListComponents.FirstOrDefault().RotId;
    	  return string.IsNullOrEmpty(rotId)
    			   ? "NOT FOUND"
    			   : rotId;
    			   
    I am not clear about the spelling.
    Is that with the? right?  
    public string GetRotID()
    {
     string ret = ""; 
     ret = (ListMount.Count != 0) ? 
     ListMount.FirstOrDefault()?.ListComponents.FirstOrDefault()?.RotId : "NotIncluded #####";
     return ret; 
    }
    
    public class Foo
    {
    	public string RotID => GetRotID();
    
    	private string GetRotID()
    	{
    		//do stuff
    		var rotId = DoStuffAndGetResults();
    
    		return string.IsNullOrEmpty(rotId)
    			   ? "NOT FOUND"
    			   : rotId;
    	}
    }
    
    


    Thanks in advance.

    Greetings Markus


    Thursday, May 23, 2019 5:35 PM
  • Hi Markus Freitag,

    Thank you for feedback.

    Do you mean you want to know about the ? : in the code?

    If yes, you could refer to the MSDN document about ?: operation.

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 29, 2019 5:53 AM
    Moderator
  • Do you mean you want to know about the ? : in the code?

    If yes, you could refer to the MSDN document about ?: operation.

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator


    Hello Wendy,

    Thanks for your response.

    I want to know 

     ret = (ListMount.Count != 0) ? 
     ListMount.FirstOrDefault()?.ListComponents.FirstOrDefault()?.RotId : "NotIncluded #####";
     return ret;

    is that the same

    if ( ListMount.FirstOrDefault() != null )

    if ( ListComponents.FirstOrDefault() != null )

    ListComponents.FirstOrDefault().RotId


    Thanks for your answer, for your help.

    With best regards Markus

    Thursday, May 30, 2019 10:44 AM
  • Hi Markus,
    Yes, the way you've written the parts with the ?. is the same. However, the : part is wrong. The ?. operator is short-circuiting, meaning if anything is null the following code won't be executed. I think what you actually want is to use, instead of : is the null-coalescing operator, which is ??:

    ret = (ListMount.Count != 0) ?
        ListMount.FirstOrDefault()?.ListComponents.FirstOrDefault()?.RotId ?? "NotIncluded #####";
    

    See this link:
    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Thursday, May 30, 2019 1:40 PM
    Moderator
  • Hello Bonnie,
    //Operator
    ret = (ListMount.Count != 0) ? ListMount.FirstOrDefault()?.ListComponents.FirstOrDefault()?.RotId ?? "NotIncluded #####";
    if (ListMount.Count != 0)
     if (ListMount.FirstOrDefault() != null)
      if (ListComponents.FirstOrDefault() != null)
       if (ListComponents.FirstOrDefault().RotId != null)
        ret = ListComponents.FirstOrDefault().RotId;
       else
        ret = "NotIncluded #####";
        
        int? x = null;
        
    Right. Now it is equal, right?
            // Set y to the value of x if x is NOT null; otherwise,
            // if x == null, set y to -1.
            int y = x ?? -1;
    
    The new spelling takes some getting used to.
    ///---------------------
    Action<int> display = s => Console.WriteLine(s);
    var numbers = new List<int>();
    numbers.Add(10);
    numbers.Add(17);
    display(numbers.Count);   // output: 2
    //-----------------------


    There are tools. Is that correct => we could say "GoesTo" for =>
    Have a nice weekend.
        Markus
    Friday, May 31, 2019 3:49 PM
  • Hi Markus,
    Yes, it does take some getting used to the new syntax (spelling, as you say ... but the correct word is actually syntax).

    The example you showed with xx ?? -1 is the same as checking for null with an if statement, you are correct.

    The example you showed with Action<int> display s => Console.WriteLine(s) works fine (obviously), but I wouldn't describe the => operator to mean "GoesTo". I mean, it's called a "Lambda expression", but that is hard to define with a single phrase (like Goes To), because if you look at some of the various ways it can be used, I can't think of a single simple word or phrase that defines everything. You've probably already seen this link (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-operator).

    Anyway, I *will* have a great weekend because I am off on a much needed 4 days of vacation! It's long overdue.
    I hope *you* have a great weekend too!!


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, June 1, 2019 1:19 AM
    Moderator