Equivalent of Fixed

C

Chris Dunaway

I was using .Net Reflector to look at some methods in the String class
and found this one:

Friend Sub AppendInPlace(ByVal value As Char*, ByVal count As Integer,
ByVal currentLength As Integer)
Dim local1 As Char*
Fixed local1 = AddressOf Me.m_firstChar
Dim num1 As Integer
For num1 = 0 To count - 1
local1((currentLength + num1)) = value(num1)
Next num1
End Fixed
Me.SetLength((currentLength + count))
Me.NullTerminate
End Sub

In C# is shows like this:

internal unsafe void AppendInPlace(char* value, int count, int
currentLength)
{
fixed (char* local1 = &this.m_firstChar)
{
for (int num1 = 0; num1 < count; num1++)
{
local1[currentLength + num1] = value[num1];
}
}
this.SetLength(currentLength + count);
this.NullTerminate();
}


fixed is apparently a statement in C# but I was curious about its use
in VB.Net. Specifically the Fixed .. End Fixed construct. This is
obviously invalid as it wont even compile in the IDE. So why is
Reflector showing its output? How would the C# code be translated to
VB.Net?
 
H

Herfried K. Wagner [MVP]

Chris,

Chris Dunaway said:
I was using .Net Reflector to look at some methods in the String class
and found this one:

Friend Sub AppendInPlace(ByVal value As Char*, ByVal count As Integer,
ByVal currentLength As Integer)
Dim local1 As Char*
Fixed local1 = AddressOf Me.m_firstChar
Dim num1 As Integer
For num1 = 0 To count - 1
local1((currentLength + num1)) = value(num1)
Next num1
End Fixed
Me.SetLength((currentLength + count))
Me.NullTerminate
End Sub

In C# is shows like this:

internal unsafe void AppendInPlace(char* value, int count, int
currentLength)
{
fixed (char* local1 = &this.m_firstChar)
{
for (int num1 = 0; num1 < count; num1++)
{
local1[currentLength + num1] = value[num1];
}
}
this.SetLength(currentLength + count);
this.NullTerminate();
}


fixed is apparently a statement in C# but I was curious about its use
in VB.Net. Specifically the Fixed .. End Fixed construct. This is
obviously invalid as it wont even compile in the IDE. So why is
Reflector showing its output? How would the C# code be translated to
VB.Net?

Something similar to 'fixed' can be archieved using
'System.Runtime.InteropServices.GCHandle.Alloc' and
'System.Runtime.InteropServices.GCHandle.AddressOfPinnedObject'. However,
as VB.NET doesn't support pointers the way C# does, a direct equivalent to
the C# code for VB.NET doesn't exist.
 
C

Chris Dunaway

I thought so. I was just curious as to why Reflector was outputting it
that way.
 
H

Herfried K. Wagner [MVP]

Chris Dunaway said:
I thought so. I was just curious as to why Reflector was outputting it
that way.

Mhm... I think it's better that Reflector outputs this code instead of
saying "No VB.NET version available" ;-).
 

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