So im trying to use the get / set properties on C# but I cant get my code to work ( it crashes my console app )
This is my textHandler.cs file as you can see the public static void method WriteInfo is using get / set properties but it crashes my app..
class TextHandler
{
public static void WriteInfo(String text)
{
var consoleText = new Text();
consoleText.text = text;
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(consoleText);
Console.ForegroundColor = ConsoleColor.White;
}
public static void WriteError(String text)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(text);
Console.ForegroundColor = ConsoleColor.White;
}
public static void WriteSuccess(String text)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(text);
Console.ForegroundColor = ConsoleColor.White;
}
public static void WriteText(String text, ConsoleColor color)
{
}
}
public class Text
{
public String text
{
get
{
return this.text;
}
set
{
this.text = value;
}
}
}
Here I call that method
TextHandler.WriteInfo("New client with version : " + message + " | current version : " + version);
If I remove that line the app doesnt crash anymore, dont know what Im doing wrong since I dont get any error.
Also if this is a bad method to do this please tell me I would like to improve
Thanks
The code that creates infinit recursion is:
public String text
{
get
{
return this.text;
}
set
{
this.text = value;
}
}
In set you assign this.text = value to itself, creating infinit recursion, so StackOverflow soon or later.
Seems that you do no need a field, so change your code to:
public String Text {get;set} //PROPERTIES ARE UPPERCASE BY MS STANDART
You need to separate the "backing" field from the public property:
public class Text
{
private string text;
public String TheText
{
get
{
return this.text;
}
set
{
this.text = value;
}
}
}
In the above example, TheText is the a "badly named" public property and text is the backing field. At the moment, your code is addressing the same field for both, causing recursion. Usually the convention would be to have a capital property Text and a lowercase backing field text.
However, in your code you have named the class Text so it is confusing to address text.Text.
There is no need to create the "Text" class. Just pass the string to Console.WriteLine. Also, you didn't specify the nature of the application. This will work fine in a console app, but may not work for a web application or other app that is not bound to the SdtOut
So,
You are setting the into the property that is calling again the set method until you get a StackOverflow exception.
To avoid this try this
public class Text
{
string _text = null;
public String text
{
get
{
return this.text;
}
set
{
_text = value;
}
}
}
Or empty get set methods
public class Text
{
public string text { get; set; }
}
Related
I want to use a variable, here i'm using TextName as the variable.
then use it AS a property. Of course i cannot because it's a string, but how do i get a variable/string to be treated as the text in the string it's holding? In google sheets this is very similuar to something called an INDIRECT, to be able to be the data you are referring to.
// Code I like to write:
TextName = "richTextBox";
TextName.Text = "text for richTextBox";
I want that code to be treated as
richTextBox.Text = "text for richTextBox";
but with richTextBox.Text in full or part to be a variable, so that I can put this entire thing in a method, and only change the variable, and not have an entire method all over the code over and over.
I'm using .NET6.0 if it matters.
I suppose you are going to do this in a Form. In this case, you can add this code into your form:
public string this[string name]
{
get
{
return this.Controls.ContainsKey(name) ?
this.Controls[name].Text :
null;
}
set
{
if (this.Controls.ContainsKey(name))
{
var control = this.Controls[name];
control.Text = value;
}
}
}
And get/set the text of any Control in this way:
this["richTextBox"] = "text for richTextBox";
var text = this["richTextBox"];
this is your form.
If you are going to use in multiple forms, you can create an extension methods:
public static class FormExtends
{
public static string GetText(this Form form, string name, string defaultText = null)
{
return form.Controls.ContainsKey(name) ?
form.Controls[name].Text :
(defaultText ?? string.Empty);
}
public static void SetText(this Form form, string name, string text)
{
if (form.Controls.ContainsKey(name))
{
var control = form.Controls[name];
control.Text = text;
}
}
}
And simplify the code in each form:
public string this[string name]
{
get { return this.GetText(name); }
set { this.SetText(name, value); }
}
I am having an issue passing properties from one class to another.
I'm getting an error saying that an object reference is required for all of the Game properties in the second class. They are highlighted at the bottom.
this is my first class (Game):
class Game
{
private string verb= "";
private string noun= "";
private string adjective= "";
private string panimal= "";
private string pnoun= "";
public string Verb
{
get {return verb; }
set {verb = value; }
}
public string Noun
{
get {return noun; }
set {noun = value; }
}
public string Adjective
{
get {return adjective;}
set {adjective = value; }
}
public string Panimal
{
get {return panimal; }
set {panimal = value; }
}
public string Pnoun
{
get {return pnoun; }
set {pnoun = value; }
}
public void InScreen()
{
Console.WriteLine("First, give me a past tense VERB: ");
Verb = Console.ReadLine();
Console.WriteLine("\nNow, give me a NOUN: ");
Noun = Console.ReadLine();
Console.WriteLine("\nNext, I will need an ADJECTIVE: ");
Adjective = Console.ReadLine();
Console.WriteLine("\nNow, I will need an ANIMAL(plural): ");
Panimal = Console.ReadLine();
Console.WriteLine("\nFinally, I neeed a plural NOUN: ");
Pnoun = Console.ReadLine();
}
My second class (InsertFunOOUI)
public void Poem()
{
Console.WriteLine("Humpty Dumpty " + **Game.Verb** + "on a " +
**Game.Noun**
);
Console.WriteLine("Humpty Dumpty had a " + **Game.Adjective** + "
fall"
);
}
...you get the picture.
Game is a Type. A class. You need to create an instance of it:
Game g = new Game();
and only then use:
g.Verb
etc.
You need to setup an instance of Game in order to use it, unless it you intended for it to be a static class.
Game game = new Game();
game.InScreen();
Something like that?
First create an instance of your game like #ispiro said. You need to do this once (in the main if possible). Then create an instance of your class poem. Use dependency injection and pass the properties of the game object in as parameters instead of the whole object if possible. I would even create a method inside your Poem class that handles the Console Output. Adapt this code to your liking:
public void main()
{
var game = new Game();
var poem = new Poem();
poem.Output(game.Verb, game.Adjective);
}
public class Poem()
{
public void Output(string verb, string adjective)
{
// your console writeline code
}
}
I am trying to learn C# and I am up to an example that uses a boolean. For the life of me I cant figure out why the program isnt noticing that I am trying to pass a value of true to the boolean. Here is the code in the Form.cs:
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
HappyBirthday birthdayMessage = new HappyBirthday();
string returnedMessage;
birthdayMessage.PresentCount = 5;
birthdayMessage.MyProperty = "Adam";
birthdayMessage.hasParty = true;
returnedMessage = birthdayMessage.MyProperty;
MessageBox.Show(returnedMessage);
}
}
}
Here is the Class that I created:
class HappyBirthday
{
//====================
// CLASS VARIABLES
//====================
private int numberOfPresents;
private string birthdayMessage;
private bool birthdayParty;
//===========================
// DEFAULT CONSTRUCTOR
//===========================
public HappyBirthday()
{
numberOfPresents = 0;
//birthdayParty = false;
}
//===========================
// METHOD
//===========================
private string getMessage(string givenName)
{
string theMessage;
theMessage = "Happy Birthday " + givenName + "\n";
theMessage += "Number of presents = ";
theMessage += numberOfPresents.ToString() + "\n";
if (birthdayParty == true)
{
theMessage += "Hope you enjoy the party!";
}
else
{
theMessage += "No party = sorry!";
}
return theMessage;
}
//================================
// READ AND WRITE PROPERTY
//================================
public string MyProperty
{
get { return birthdayMessage; }
set { birthdayMessage = getMessage(value); }
}
//================================
// WRITE-ONLY PROPERTY
//================================
public int PresentCount
{
set { numberOfPresents = value; }
}
public bool hasParty
{
set { birthdayParty = value; }
}
}
Now I set the initial value to false (even though if my understanding is correct that should be the default value), but when I try to set it = true, the program does not recognize it. Am I supposed to pass a boolean differently then I would a string or int?
You're setting MyProperty before you're setting hasParty. getMessage() is not being called every time MyProperty is polled.
The way MyProperty works is confusing, because the set and get deal with different values (you set the name, and then get the whole message, which is confusing). I'd replace it with a GivenName property and then make the GetMessage() (or expose it as a read-only property Message) public.
Also, you can make your code much simpler by using auto-properties (you can use private gets to keep the write-only behavior, though in the real world write-only properties are very rare, and you should probably just make them public like the sets). And since the default int value is 0, you don't need to specify your default constructor. Here's how the code looks now:
class HappyBirthday
{
public string Message
{
get
{
string theMessage;
theMessage = "Happy Birthday " + GivenName + "\n";
theMessage += "Number of presents = ";
theMessage += PresentCount.ToString() + "\n";
if (HasParty)
{
theMessage += "Hope you enjoy the party!";
}
else
{
theMessage += "No party = sorry!";
}
return theMessage;
}
}
public string GivenName { private get; set; }
public int PresentCount { private get; set; }
public bool HasParty { private get; set; }
}
I have a bunch of business class with autoproperties :
public class A {
public int Id { get; set; }
public string Title { get; set;}
}
Because the application evolves, there is a new requirement to enable tracking the changes of the properties, in order to send to the backing store only changed data.
In order to reach this goal, I have to convert ALL properties to field + property like this :
public class A {
private int m_Id;
public int Id {
get { return m_Id; }
set {
if(m_Id != value){
SetChanged("Id");
m_Id = value;
}
}
}
private string m_Title;
public string Title
{
get { return m_Title; }
set {
if(m_Title != value){
SetChanged("Title");
m_Title = value;
}
}
}
protecte void SetChanged(string propertyName) {
// Not important here
}
}
Is there a way to quickly refactor my code to avoid having to manually change the properties ?
There's no way in the IDE to do this, but if you need to replace all X properties, I would write a short console application to do it.
The process would be:
Iterate over all files in directory matching *.cs
Foreach file, regex find and replace old property for new property syntax
Using regex to match is very powerful. Regex can be used in VS2010 to do a find/replace operation. If you try finding this (with regex enabled)
{(public|private|internal|protected)}:b{[a-zA-Z0-9]+}
:b{[a-zA-Z0-9]+}:b\{ get; set; \}
It will match properties like this
public Type Foo { get; set; }
In your console application find all lines of code that match the above, then start splitting them up into Modifier, Type, Property Name and finally replacing the whole block with something like this
// PS: this is pseudocode ;-) or could be your new property template
private [Type] m_[PropertyName].ToPascaleCase
public [Type] PropertyName
{
get { return m_[PropertyName].ToPascaleCase; }
set
{
if(m_[PropertyName].ToPascaleCase != value){
SetChanged([PropertyName]);
m_[PropertyName].ToPascaleCase = value;
}
}
}
Finally I would advocate taking a backup of your code or running this test offline and testing before checking in!!
You can always just create a generic method that will do the assignment and call SetChange
void SetChangeIfNeeded<T>(ref T field, T value, string propertyName)
{
if (!EqualityComparer<T>.Default.Equals(field, value))
{
field = value;
SetChanged(property);
}
}
You would still need to have a private back field. Your class would look something like:
public class A {
private int m_id
public int Id
{
get { return m_id };
set { SetChangeIfNeeded<int>(ref m_id, value, "Id"); }
}
}
ReSharper can do this, but wouldn't modify setter.
public string Title {
get { return m_title; }
set { m_title = value; }
}
There is probably no direct way of doing this with refraction. If this was my problem. I would make code to generate this:
public string MakePropertyBigger(string varName, string propName, string dataType)
{
string output = "";
output += string.Format("private {0} {1};", dataType, varName) + Environment.NewLine;
output += string.Format("public {0} {1}", dataType, propName) + Environment.NewLine;
output += "{" + Environment.NewLine;
output += string.Format("get { return {0}; }", varName) + Environment.NewLine;
output += string.Format("set { if({0} != value){ SetChanged(\"{1}\");", varName, propName) + Environment.NewLine;
output += string.Format("{0} = value; }", varName) + Environment.NewLine;
output + "}" + Environment.NewLine + "}";
Now just plug this in and chug it out.
It seems that, due to an unknown cause, I am now unable to edit anything in my DataGridView. The DGV's ReadOnly property value is false, and all columns except for one all have the ReadOnly property set to false as well.
I'm beginning to think that it may be due to a special value I tried adding to one of my classes, one that I only wanted to be modified within the class, but still read only to the public. I don't think that value is messing with anything else, but none the less, here is the relevant portion of my code:
private void loaderWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
loadingBar.Value = e.ProgressPercentage;
if (e.UserState != null)
{
savefiles.Add((SaveFile)e.UserState);
}
}
Where savefiles is a BindingList, and where SaveFile is my class:
public class SaveFile
{
private string d_directory;
private int d_weirdnumber;
private bool d_isautosave;
private string d_fullname;
private string d_datatype;
private string d_owner;
private bool d_isquicksave;
private string d_title;
private string d_gametime;
public SaveFile() { }
public SaveFile(string directory, int weirdnumber, bool isautosave, string fullname, string datatype, string owner, bool isquicksave, string title)
{
d_directory = directory;
d_weirdnumber = weirdnumber;
d_isautosave = isautosave;
d_fullname = fullname;
d_datatype = datatype;
d_owner = owner;
d_isquicksave = isquicksave;
d_title = title;
}
public string Gametime
{
get { return d_gametime; }
}
public string Datatype
{
get { return d_datatype; }
set { d_datatype = value; }
}
public string Title
{
get { return d_title; }
set { d_title = value; }
}
public bool IsQuickSave
{
get { return d_isquicksave; }
set { d_isquicksave = value; }
}
public bool IsAutoSave
{
get { return d_isautosave; }
set { d_isautosave = value; }
}
public string Directory
{
get { return d_directory; }
set { d_directory = value; }
}
public string FullName
{
get { return d_fullname; }
set
{
d_fullname = value;
string[] split = value.Split(new char[]{'-'});
foreach (string str in split)
{
if (System.Text.RegularExpressions.Regex.IsMatch(str, "^\\d\\d:\\d\\d:\\d\\d$"))
{
d_gametime = str;
}
}
}
}
public int Weirdnumber
{
get { return d_weirdnumber; }
set { d_weirdnumber = value; }
}
public string Owner
{
get { return d_owner; }
set { d_owner = value; }
}
}
Gametime is that special property I mentioned earlier. It doesn't have a set function, but according to this, I should be in the clear, right?
Can anyone then tell me why I may not be able to edit any of the DGV cells?
EDIT: I just found out that not setting AutoGenerateColumns to false allows me to edit again, but I still don't know why.
After several hours, a friend finally took a look at it over Remote Desktop. He wrote a function to force all columns to have a non read-only status, and go figure, it worked. So we looked at the column properties in the editor, and somehow... I don't know why... they were all set to Read only. I swear I checked them 4 times before.
The lesson of this story (I guess): When in doubt, check your settings. When not in doubt, become doubtful. Otherwise, file a bug report to Microsoft :\