Hi Guys
NP62 Method call
When you call the method, however, the arguments you send to it must match in both number and type with the arguments listed in the method declaration. This is a guide line has to follow in initiating a method call.
I got the following program from the website.
http://www.java2s.com/Tutorial/CSharp/0060__Operator/TypeoperatorsIs.htm
In the program
Test("Test", new Paper(),
new NonPrintablePaper());
is calling
Test(string sister, params object[] papers).
But it is not meeting the above guide line. Anyone knows please explain the reason.
Thank you
using System;
interface Printable
{
void print(string name);
}
class Paper : Printable
{
public void print(string name)
{
Console.WriteLine("Poking {0}", name);
}
}
class NonPrintablePaper
{
}
class MainClass
{
public static void Test(string sister, params object[] papers)
{
foreach (object o in papers)
{
if (o is Printable)
{
Printable p = (Printable)o;
p.print(sister);
}
}
}
public static void
{
Test("Test", new Paper(), new NonPrintablePaper());
}
}
/*
Poking Test
*/
Posted Jul 26, 2008, 4:25 PM
Hi Alan
Nowadays rarely see in the forum. Thank you for your explanation.
AlanPosted Jul 26, 2008, 2:13 PM
Hi Maha,
The following line throws an exception:
DisplayStrings("Thomas", new John());
because there's no conversion defined between an object of type John and string which is required by the foreach() statement in the DisplayStrings method.
If you were to change the John class to this:
class John
{
public override string ToString()
{
return "John";
}
}
and the foreach statement to this:
foreach (object person in people)
Console.Write("{0} ", person);
then there'd be no exception because:
1. The conversion to string is no longer needed because the person variable is now of type object rather than string.
2. When an object is used in a call to Console.Write (or WriteLine), then the underlying code looks to see whether the object overrides the ToString() method and, if it does, uses the value returned by that method. If it doesn't override it, then the name of the type itself would be printed. So, even if you commented out the ToString() method, "John" would still get printed out here :)
Posted Jul 26, 2008, 8:28 AM
The following program will executing after declearing empty class John because new John() is used in the program. I wish to know the reason for that. Please explain.
using System;
class John
{}
public class ParamsDemo
{
public static void Main()
{
string[] names = { "Mark", "Paulette", "Carol", "James" };
DisplayStrings("Ginger"); // one string argument
DisplayStrings("George", "Maria"); // three string argument
DisplayStrings(names); // array of string
DisplayStrings("Thomas", new John());
}
public static void DisplayStrings(params object[] people)
{
foreach (string person in people)
Console.Write("{0} ", person);
Console.WriteLine("\n-------------------");
}
}
/*
Ginger
-------------------
George Maria
-------------------
Mark Paulette Carol James
-------------------
Thomas
*/
Posted Jul 25, 2008, 5:25 PM
Now matter is clear thank you for your explanation, Satish Kumar Kathi
Satish KathiPosted Jul 25, 2008, 5:17 PM
In your program object type is "parameter array of object"
public static void Test(string sister, params object[] papers)
Alan also said the same
“So in this example we actually have a parameter array of objects which means we can pass any number of arguments of any type such as Paper and NonPrintablePaper and the compiler will be quite happy.”
Posted Jul 25, 2008, 5:08 PM
In your program object type is declared I agreed with that in my program where is the object type declaration?
Satish KathiPosted Jul 25, 2008, 4:52 PM
Posted Jul 25, 2008, 4:48 PM
One can say new Emp("1", "First Name", "Last Name") is equal to anything which will become an object after declearation of type.
Satish KathiPosted Jul 25, 2008, 4:42 PM
According to you new Emp("1", "First Name", "Last Name") is only equal to object “objEmp”
So then what is the type of objEmp?
Posted Jul 25, 2008, 4:32 PM
That mean you say “Emp objEmp” is automatically created when you declare EmployeeFullname(new Emp("1", "First Name", "Last Name"));. Which I won’t agree
in my view new Emp("1", "First Name", "Last Name") is only equal to object “objEmp” where is the declearation of type?
Satish KathiPosted Jul 25, 2008, 4:05 PM
What you said is correct, I will try
Here is sample code
public string EmployeeFullname(Emp objEmp)
{
string fullname = objEmp.LName + "," + objEmp.FName;
return fullname;
}
private void button5_Click(object sender, EventArgs e)
{
EmployeeFullname(new Emp("1", "First Name", "Last Name"));
}
According to you EmployeeFullName accepts Emp object so you want to create object and pass it
Like
Emp objEmp = new Emp("1", "First Name", "Last Name");
If you see my code above the left part “Emp objEmp” is created in the method signature public string EmployeeFullname(Emp objEmp)
And the right part “new Emp("1", "First Name", "Last Name");” creating an object is done when you are calling the method “EmployeeFullname(new Emp("1", "First Name", "Last Name"));”
In either way you are doing the same
You are declaring variable in method and allocating memory when you are calling the method
Posted Jul 25, 2008, 3:44 PM
A two-step process creates an object that is an instance of a class.
First, you supply a type and an identifier, just as when you declare any variable. Second, you allocate computer memory for that object.
Here in the above program new Paper()and new NonPrintablePaper()are not created according to rule. That means according to rule below is the perfect way of creating an instance of a class.
Paper print = new Paper();
NonPrintablePaper nonprintablepaper = new NonPrintablePaper()
But without this program is executing well. Anyone knows please explain the reason.
AlanPosted Nov 26, 2007, 5:20 AM
Hi Maha,
The following advice is a bit loose:
"When you call the method, however, the arguments you send to it must match in both number and type with the arguments listed in the method declaration."
In practice the arguments have either to be of the same type as, or of a type which is implicitly convertible to, the parameter type.
This means that, if you have a parameter of type 'object', then you can pass an argument of any type to the method in respect of that parameter because all types are implicitly convertible (i.e. a cast is not needed) to 'object'.
So in this example we actually have a parameter array of objects which means we can pass any number of arguments of any type such as Paper and NonPrintablePaper and the compiler will be quite happy.