In this program , the word "value" is not defined and where does it come from, and can another word be used instead? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 3

In this program , the word "value" is not defined and where does it come from, and can another word be used instead?

using System; using System.Collections.Generic; namespace Code_Coach_Challenge { Ā Ā Ā  class Program Ā Ā Ā  { Ā Ā Ā Ā Ā Ā Ā  static void Main(string[] args) Ā Ā Ā Ā Ā Ā Ā  { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā  string postText = Console.ReadLine(); Ā Ā Ā Ā Ā Ā Ā  Post post = new Post(); Ā Ā Ā Ā Ā Ā Ā Ā post.Text = postText; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā  post.ShowPost(); Ā Ā Ā Ā Ā Ā Ā  } Ā Ā Ā  } Ā Ā Ā  class Post Ā Ā Ā  { Ā Ā Ā Ā Ā Ā Ā  private string text; Ā Ā Ā Ā Ā Ā Ā  Ā Ā Ā Ā  public Post (){ Console.WriteLine ("New post");Ā Ā Ā Ā Ā Ā Ā Ā  Ā Ā Ā Ā  }Ā Ā  //write a constructor here Ā Ā Ā Ā Ā Ā Ā  Ā Ā Ā Ā Ā Ā Ā  public void ShowPost() Ā Ā Ā Ā Ā Ā Ā  { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā  Console.WriteLine(text); Ā Ā Ā Ā Ā Ā Ā  } Ā Ā Ā Ā Ā Ā Ā  public string Text { Ā Ā Ā Ā  get {return text ;} Ā Ā Ā Ā  set { text =value;}//I am stuck at this point, please someone explain }Ā Ā Ā Ā Ā Ā  Ā Ā Ā Ā Ā Ā Ā  Ā Ā Ā  } }

9th Aug 2023, 3:52 AM
safir safir1
safir safir1 - avatar
3 Respostas
+ 3
safir safir1 Continue... The `value`contextual keyword that is used inside the `set` block of a property to represent the value that is being assigned to the property. For example:- If you use any other word like`tuk` instead of `value`.. You will got an error because `tuk` is not a reserved keyword in C# and is not a valid identifier. When you use `set { text = tuk; }`, the compiler does not know what `tuk` means and gives you an error. instead of `tuk`use the `value`keyword to assign the value to the `text`field see this... https://mvcwebapp.wordpress.com/2016/01/12/value-keyword-in-c/ https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/value
9th Aug 2023, 4:21 AM
Darpan kesharwanišŸ‡®šŸ‡³[InactivešŸ“š]
Darpan kesharwanišŸ‡®šŸ‡³[InactivešŸ“š] - avatar
+ 2
The "value" is a part of the get/set accessor in a property, which works like getter and setter in other programming languages. The Text in the context is a property, so when calling it with the object we instantiated in the class "Text", it outputs the VALUE inside the text variable(of that object), if there aren't any rules applied in the property. This is because we used the keyword "value" in the set accessor. So basically, the keyword "value" actually sets the value of the given "object".. I hope I get that right. BONUS: When creating a property, if you want it to not have any specific rules on it, you can use this shortcut: public [Data Types] [Class name] {get; set;} Example: public string Name {get; set;}
9th Aug 2023, 4:10 AM
Dragon RB
Dragon RB - avatar
0
Thanks to Dragon RB and Darpan kesharwani
9th Aug 2023, 5:07 AM
safir safir1
safir safir1 - avatar