Reputation: 6170
I have a "Debug" class, which simply prints information to the console etc. From the rest of the code I want to be able to call the methods within it, but so far it's only partly working.
Calling dc.Print()
works fine, but as soon as I call dc.Print(dc.GetEventsLogged())
I get a red line with the message
"The best overloaded method match has some invalid arguments" as well as Argument 1: cannot convert from 'int' to 'string'.
Basically: Why are my arguments to dc.Print wrong? Also, what can I do about "cannot convert from int to string? I tried .ToString but that didn't work either.
This is my "Debug.cs" class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
public class Debug
{
private int events_logged;
public Debug()
{
events_logged = 0;
}
public void Print(string Message)
{
Console.WriteLine("[" + DateTime.UtcNow + "] " + Message);
events_logged++;
}
public int GetEventsLogged()
{
return events_logged;
}
}
}
And in my "Program.cs" class I have:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Debug dc = new Debug();
dc.Print("Test");
}
}
}
Upvotes: 2
Views: 273
Reputation: 21
Your method Print
expects an argument of type String
. When you call dc.Print(dc.GetEventsLogged())
, your actually give an int
because your method GetEventsLogged()
returns an int
.
Upvotes: 1
Reputation: 1505
try dc.Print(dc.GetEventsLogged().toString())
because GetEventsLogged()
is of int type and Print(string Message)
looking for string input.
Upvotes: 1
Reputation: 10140
public string GetEventsLogged()
{
return events_logged.ToString();
}
Upvotes: 0
Reputation: 8147
The reason you are seeing the error is because GetEventsLogged()
returns an int
whereas Print()
expects you to pass in a string
. Therefore you need to the return from int
to string
and you were on the right track with ToString()
. This will do what you want to achieve:
dc.Print(dc.GetEventsLogged().ToString());
Upvotes: 7
Reputation: 12956
dc.Print()
wants a string argument and dc.GetEventsLogged()
returns an int
. You need to ToString()
the int
so that the types match.
int numberOfEventsLogged = dc.GetEventsLogged();
string numberOfEventsLoggedAsString = numberOfEventsLogged.ToString();
dc.Print(numberOfEventsLoggedAsString)
Upvotes: 2