36 Replies
You forgot the
()
after ToString
Oh😮
can you put anything into those brackets
well if the method takes arguments
but ToString doesn't
su what is the reason for the brackets?
to indicate that its a method call
in more complex scenarios u want to reference the method instead of calling it directly
eg
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 itselfand you summarize my.object with presenter for easier understanding and writing of code or?
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
thanks
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
why this does not work
but they must have different parameters
it confuses me a bit(to much)😅
kk, then forget about that
can u show the whole code of that method? and please as $code blocks
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/(my lungs are crying for a smoke, brb)
$codegif
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
} } } $codegif
```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
} } } $codegif
```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
} } } weeel
are u watching the whole gif
you need the last three ``` at the end as well
i clearly dont know how to do this
jesus
It's not rocket science
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 in the same line wrote it in a different way🤣
back in days my english was crap, i made the same mistake ;p
The council?
Do you have to convert numbers to string in a messagebox?
MessageBox do it implicitly, I think.
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
MessageBox.Show Method (System.Windows.Forms)
Displays a message box.
Thanx.
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.