Calling Convension in .NET.,

G

Guest

Hi all.,
I got some info from this iink
where u can find.,

Public Class LibWrap
' Visual Basic does not support varargs, so all arguments must be
' explicitly defined. CallingConvention.Cdecl must be used since the stack
' is cleaned up by the caller.
' int printf( const char *format [, argument]... )
<DllImport("msvcrt.dll", CallingConvention := CallingConvention.Cdecl)> _
Overloads Shared Function printf ( _
format As String, i As Integer, d As Double) As Integer
End Function
<DllImport("msvcrt.dll", CallingConvention := CallingConvention.Cdecl)> _
Overloads Shared Function printf ( _
format As String, i As Integer, s As String) As Integer
End Function
End Class 'LibWrap
Public Class App
Public Shared Sub Main()
LibWrap.printf(ControlChars.CrLf + "Print params: %i %f", 99,
99.99)
LibWrap.printf(ControlChars.CrLf + "Print params: %i %s", 99, _
"abcd")
End Sub 'Main
End Class 'App

if VB.NET does't support VarArgs then how come
Console.Write("Test{0}{1}..",3,4) is works?


Regards Balamurali C
 
R

Richard Grimes [MVP]

Balamurali C said:
if VB.NET does't support VarArgs then how come
Console.Write("Test{0}{1}..",3,4) is works?

There are lots of overloads of Write: one takes one object, another takes
two and another three (other than the format string). Take a look at an
example of this call in ILDASM, C# and VB.NET call this method:

call void [mscorlib]System.Console::Write(string, object, object)

now try this:

Console.Write("Test{0}{1}{2}{3}",3,4,5,6)

If you look at ILDASM you'll see that the following method is called:

call void [mscorlib]System.Console::Write(string, object[])

The compile creates an array of the parameters to Write before calling the
method.

If you try to call this same line in managed C++ you'll see that it calls:

call vararg void [mscorlib]System.Console::Write(string, object, object,
object, object)

In other words, only C++ calls vararg methods, and C++ can only call them,
it cannot write them. To write a vararg method you have to use ILASM.

Richard
 
G

Guest

hi Richard Grimes
Ya thanks for your reply. still i hve some queries.,
I asked some c# people they said we can use Param (nearly equl to this name) Object to get number of parameter & paramter values too.
this is conflicts me.,


and one more thing., is it good way or writting overload function code which take 1 to n paramter, where n is big number.

like
write(string,object);
write(string,object,object);
............
...........
write(string,object,object................);


Richard Grimes said:
Balamurali C said:
if VB.NET does't support VarArgs then how come
Console.Write("Test{0}{1}..",3,4) is works?

There are lots of overloads of Write: one takes one object, another takes
two and another three (other than the format string). Take a look at an
example of this call in ILDASM, C# and VB.NET call this method:

call void [mscorlib]System.Console::Write(string, object, object)

now try this:

Console.Write("Test{0}{1}{2}{3}",3,4,5,6)

If you look at ILDASM you'll see that the following method is called:

call void [mscorlib]System.Console::Write(string, object[])

The compile creates an array of the parameters to Write before calling the
method.

If you try to call this same line in managed C++ you'll see that it calls:

call vararg void [mscorlib]System.Console::Write(string, object, object,
object, object)

In other words, only C++ calls vararg methods, and C++ can only call them,
it cannot write them. To write a vararg method you have to use ILASM.

Richard
 
R

Richard Grimes [MVP]

Ya thanks for your reply. still i hve some queries.,
I asked some c# people they said we can use Param (nearly equl to this
name) Object to get number of parameter & paramter values too.
this is conflicts me.,

Sorry, I wasn't clear in the reply. The version of Write that has an array
of objects is the one that uses the params keyword. The C# params keyword
has *nothing* to do with varargs, all it means is that there follows an
array of parameters and the method has the responsibility of reading the
parameters out of the array.
and one more thing., is it good way or writting overload function code
which take 1 to n paramter, where n is big number.
like
write(string,object);
write(string,object,object);
...........
..........
write(string,object,object................);

Why not? You can write a single method that takes n parameters, and then the
other methods take 1 to n-1 parameters and just call the n parameter version
passing null in appropriate parameters. The methods with 1 to n-1 parameters
are just there for convenience and its a design decision as to which ones
are useful.

Richard
 
G

Guest

Hi Richard Grimes very thx for ur reply.,
What might be the n value., as per microsoft definision.,

bcoz., supposing that can I give
let n = 1000
Console.write("{0}{1}..{n} ",1,2............n);

regards
Balamurali c
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top