Re: print("my name is {name}, and {age}-year old {gender}", name, age, gender);
- From: "VisionSet" <spam@xxxxxxxxxxxx>
- Date: Sat, 31 Dec 2005 01:59:07 GMT
"moopT" <samhng@xxxxxxxxx> wrote in message
news:1135993357.311630.210260@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Hi,
> I want to make a print statement as the title appears. The point is,
> the print function can understand which variable corresponds to the
> desired parameter name inside the pattern.
>
> In C# print functions, the syntax is like
> print("my name is{0}, and {1}-year old {2}", name, age, gender);
>
> But I want to do a little further to make the print function knows
> which argument is going to fill the slot in the pattern string, even
> the arguments are not ordered in place:
> print("my name is {name}, and {age}-year old {gender}", age, gender,
> name);
>
> or it will have exception burst out when there is no suitable
> paramenter:
> print("hello, {name}", age);
>
> can we do that? Thx!
No because methods are predefined to take a fixed number of arguments.
The closest you could get would be to overload a method with all the number
of parameters you are likely to need and then you would have to have those
parameter arguments as some Type probably Object.
Objects toString() method could then be called to populate the message.
But as to order? how? unless you used some type instead of Object that had a
key field with which to match up the message tokens.
public void print(String message, Object param1);
public void print(String message, Object param1, Object param2);
public void print(String message, Object param1, Object param2, Object
param3);
etc
Now just implement them :-)
or you could do it with an array of parameters then you'd only need 1 method
public void print(String message, Object[] params);
eg
print("My name is {name}, my age is {age}", new Param[] {new Param("age",
"5"), new Param("name", "tom" )});
--
Mike W
.
- Follow-Ups:
- References:
- Prev by Date: print("my name is {name}, and {age}-year old {gender}", name, age, gender);
- Next by Date: Our Solution Guide and material is better than Ten (10) Mock tests in exa
- Previous by thread: print("my name is {name}, and {age}-year old {gender}", name, age, gender);
- Next by thread: Re: print("my name is {name}, and {age}-year old {gender}", name, age, gender);
- Index(es):