C
C#12mo ago
Masimos

❔ Why does not my .To string work?

36 Replies
Thinker
Thinker12mo ago
You forgot the () after ToString
Masimos
Masimos12mo ago
Oh😮 can you put anything into those brackets
Thinker
Thinker12mo ago
well if the method takes arguments but ToString doesn't
Masimos
Masimos12mo ago
su what is the reason for the brackets?
cap5lut
cap5lut12mo ago
to indicate that its a method call in more complex scenarios u want to reference the method instead of calling it directly eg
Func<string> presenter = myObject.ToString;
// and then later
Console.WriteLine(presenter());
Func<string> presenter = myObject.ToString;
// and then later
Console.WriteLine(presenter());
here the method "itself" is stored in presenter and then will be called later Func<string> is an abstract type to describe a method that takes no parameters and returns a string (like a ToString() method does) basically with SomeMethod() u execute that method, with out the parenthesis SomeMethod u mean the method itself
Masimos
Masimos12mo ago
and you summarize my.object with presenter for easier understanding and writing of code or?
cap5lut
cap5lut12mo ago
that was just some example code with meaningful names for that example basically that stuff with referencing the method itself is some advanced stuff u do not have to care about yet. it was only to display what the difference is, if u have the parenthesis or not
Masimos
Masimos12mo ago
thanks
cap5lut
cap5lut12mo ago
i hope the following doesnt confuse u too much: the error message talks about method groups that is because that u can have multiple methods with the same name
Masimos
Masimos12mo ago
why this does not work
cap5lut
cap5lut12mo ago
but they must have different parameters
public string ToString() { return "blah"; }
public string ToString(int x) { return "some X:" + x; }
public string ToString() { return "blah"; }
public string ToString(int x) { return "some X:" + x; }
Masimos
Masimos12mo ago
it confuses me a bit(to much)😅
cap5lut
cap5lut12mo ago
kk, then forget about that can u show the whole code of that method? and please as $code blocks
MODiX
MODiX12mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
cap5lut
cap5lut12mo ago
(my lungs are crying for a smoke, brb)
Masimos
Masimos12mo ago
$codegif
Masimos
Masimos12mo ago
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Coding_Giants_Lesson_7_String_Comparison { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void OnCheckedClicked(object sender, EventArgs e) { var text1 = txtText1.Text; var text2 = txtText2.Text; int text1Lenght = text1.Length; int text2Lenght = text2.Length; if (text1Lenght > text2Lenght) { MessageBox.Show("Text1 is larger than text2"); } else if (text1Lenght == text2Lenght) { MessageBox.Show("Text1 equal to text2"); } else { MessageBox.Show("Text2 is larger than text1"); } MessageBox.Show(text1Lenght - text2Lenght).ToString());
} } } $codegif
Masimos
Masimos12mo ago
```cs namespace Coding_Giants_Lesson_7_String_Comparison { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void OnCheckedClicked(object sender, EventArgs e) { var text1 = txtText1.Text; var text2 = txtText2.Text; int text1Lenght = text1.Length; int text2Lenght = text2.Length; if (text1Lenght > text2Lenght) { MessageBox.Show("Text1 is larger than text2"); } else if (text1Lenght == text2Lenght) { MessageBox.Show("Text1 equal to text2"); } else { MessageBox.Show("Text2 is larger than text1"); } MessageBox.Show(text1Lenght - text2Lenght).ToString());
} } } $codegif
Masimos
Masimos12mo ago
```cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Coding_Giants_Lesson_7_String_Comparison { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void OnCheckedClicked(object sender, EventArgs e) { var text1 = txtText1.Text; var text2 = txtText2.Text; int text1Lenght = text1.Length; int text2Lenght = text2.Length; if (text1Lenght > text2Lenght) { MessageBox.Show("Text1 is larger than text2"); } else if (text1Lenght == text2Lenght) { MessageBox.Show("Text1 equal to text2"); } else { MessageBox.Show("Text2 is larger than text1"); } MessageBox.Show(text1Lenght - text2Lenght).ToString());
} } } weeel
Anchy
Anchy12mo ago
are u watching the whole gif
phaseshift
phaseshift12mo ago
you need the last three ``` at the end as well
Masimos
Masimos12mo ago
i clearly dont know how to do this
Anchy
Anchy12mo ago
jesus
phaseshift
phaseshift12mo ago
It's not rocket science
Masimos
Masimos12mo ago
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Coding_Giants_Lesson_7_String_Comparison
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void OnCheckedClicked(object sender, EventArgs e)
{
var text1 = txtText1.Text;
var text2 = txtText2.Text;

int text1Lenght = text1.Length;
int text2Lenght = text2.Length;

if (text1Lenght > text2Lenght)
{
MessageBox.Show("Text1 is larger than text2");
}
else if (text1Lenght == text2Lenght)
{
MessageBox.Show("Text1 equal to text2");
}
else
{
MessageBox.Show("Text2 is larger than text1");
}
MessageBox.Show(text1Lenght - text2Lenght).ToString());




}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Coding_Giants_Lesson_7_String_Comparison
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void OnCheckedClicked(object sender, EventArgs e)
{
var text1 = txtText1.Text;
var text2 = txtText2.Text;

int text1Lenght = text1.Length;
int text2Lenght = text2.Length;

if (text1Lenght > text2Lenght)
{
MessageBox.Show("Text1 is larger than text2");
}
else if (text1Lenght == text2Lenght)
{
MessageBox.Show("Text1 equal to text2");
}
else
{
MessageBox.Show("Text2 is larger than text1");
}
MessageBox.Show(text1Lenght - text2Lenght).ToString());




}
}
}
Hmm
cap5lut
cap5lut12mo ago
MessageBox.Show(text1Lenght - text2Lenght).ToString());<-- this is an syntax error, the last ) has no opening ( generally the Show method expects a string, so no matter how but u have that type. also MessageBox.Show(something) is a void method, which means it returns nothing, thus u can not call ToString() on it so what u most likely want here is to convert the result of the subtraction into a string: (text1Lenght - text2Lenght).ToString(), the first parenthesis group the result of the subtraction as one value on which u call the ToString method this u want to pass to the MessageBox.Show method, resulting in MessageBox.Show((text1Lenght - text2Lenght).ToString()) (btw, its length, not lenght)
Masimos
Masimos12mo ago
in the same line wrote it in a different way🤣
cap5lut
cap5lut12mo ago
back in days my english was crap, i made the same mistake ;p
Masimos
Masimos12mo ago
HmmCouncilRTX1HmmCouncilRTX2HmmCouncilRTX3HmmCouncilRTX4HmmCouncilRTX5 The council?
GabyZ68
GabyZ6812mo ago
Do you have to convert numbers to string in a messagebox? MessageBox do it implicitly, I think.
cap5lut
cap5lut12mo ago
the only overload that takes one parameter takes a string, so either what u pass has an implicit conversion overload or u have to convert it urself https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.messagebox.show?view=windowsdesktop-7.0
GabyZ68
GabyZ6812mo ago
Thanx.
Accord
Accord12mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts