Reputation: 31
Code1
public class Human
{
private string h_name = "";
private string h_gender = "Male";
private int h_age = 0;
public string Name
{
get { return h_name; }
set { h_name = value; }
}
public string Gender
{
get { return h_name; }
set { h_name = value; }
}
public int Age
{
get { return h_age; }
set { h_age = value; }
}
}
Code2
public class Here
{
public static void Main(String[] args)
{
Human hm = new Human();
hm.Name = "Bill";
hm.Gender = "Male";
hm.Age = 20;
}
}
Now, I'd like to use the variable "hm" as String where hm will return the Name property... something like:
string person = hm;
Console.WriteLine(person + " has greeted you!");
What will I do? Should I make an Extension or something?
I overridden the ToString() method, and yes, it works on Console.WriteLine()
but now, I want to store it as string in a string variable
string person = hm;
Console.WriteLine(person);
and I get this
Cannot implicitly convert type 'Human' to 'string'
I also want to use it in my VB.NET program, but it gives me this error when I concatenate it:
Operator '&' is not defined for types 'String' and 'Human'
Upvotes: 0
Views: 90
Reputation: 9981
vb.net
answer:
As state by others, you need to define a conversion operator. If you want to support string concatenation then you also need to define the &
operator.
Public Class Human
Public Property Name As String
Public Shared Widening Operator CType(value As Human) As String
Return value.Name
End Operator
Public Shared Operator &(left As Human, right As String) As String
Return (left.Name & right)
End Operator
Public Shared Operator &(left As String, right As Human) As String
Return (left & right.Name)
End Operator
Public Overrides Function ToString() As String
Return Me.Name
End Function
End Class
Upvotes: 0
Reputation: 1187
An implicit convention by adding the following to the Human
class would work
public static implicit operator string(Human h)
{
return h.Name;
}
Although i think the best way is to override the ToString
Method like so
public override string ToString()
{
return Name;
}
Upvotes: 1
Reputation: 5890
Override ToString()
method.
public class Human
{
public override ToString() { return Name }
private string h_name = "";
private string h_gender = "Male";
private int h_age = 0;
public string Name
{
get { return h_name; }
set { h_name = value; }
}
public string Gender
{
get { return h_name; }
set { h_name = value; }
}
public int Age
{
get { return h_age; }
set { h_age = value; }
}
}
Your code would then look like this:
Console.WriteLine(hm + " has greeted you!");
Upvotes: 0
Reputation: 53958
You could override the ToString
method:
public class Human
{
private string h_name = "";
private string h_gender = "Male";
private int h_age = 0;
public string Name
{
get { return h_name; }
set { h_name = value; }
}
public string Gender
{
get { return h_name; }
set { h_name = value; }
}
public int Age
{
get { return h_age; }
set { h_age = value; }
}
public override string ToString()
{
return Name;
}
}
Upvotes: 0
Reputation: 101681
You can declare an implicit
conversion from your type to string
using operator overloading. But it's a bad idea. I suggest you to override ToString
method in your class instead, then you can just output it like this:
Console.WriteLine(hm + " has greeted you!");
There is also a good documentation on how to override ToString
method
Upvotes: 5