For Karunakararo, re translate to C#

M

Morten Wennevik

hi Morten
i have problem with Datagrid paging .
After 10 pages it's giving Like 1 2 3 4 5 6 7 8 9 10 ...... these dot values iam converting to >> (like this) in VB.net
please see this function
Sub CustomPager(ByVal STCPagerGrid As Object)
Dim intCtr As Integer
Static intLastItem As Integer
Static intCount As Integer
Static intTotalPage, lintCurrentPage As Integer

For intCtr = 0 To STCPagerGrid.Controls.Count - 1

If STCPagerGrid.Controls(intCtr).GetType.ToString =
"System.Web.UI.WebControls.DataGridLinkButton" Then
If IsNumeric(STCPagerGrid.controls(intCtr).text) = True
And Left(STCPagerGrid.controls(intCtr).text, 1) <> "[" Then
STCPagerGrid.controls(intCtr).text = "[" &
STCPagerGrid.controls(intCtr).text & "]"
If STCPagerGrid.Controls(intCtr).Text = "..." And intCtr
= 0 Then
STCPagerGrid.Controls(intCtr).Text = "[<<]"
End If
If STCPagerGrid.Controls(intCtr).Text = "..." And intCtr
STCPagerGrid.Controls(intCtr).Text = "[>>]"
End If
End If
If STCPagerGrid.controls(intCtr).controls.count > 0 Then
CustomPager(STCPagerGrid.controls(intCtr))
End If
Next
End Sub



how to write in C#

Well, for one, you can't use STCPagerGrid for much as long as it is an object. You need to either change the parameter to something else, like Control, or cast it to something like Control inside the method.

You can't use method local static variables in C# (to my knowledge). If you need to use static variables declare them outside the method.

There is no IsNumeric method in C#. The closest equivalent is probably Double.TryParse which is slightly more complex, and in the code below I haven't checked if you can set the numberformatprovider to null as I did.

There is no Left method in C#, use String.SubString(0, number of characters).

The translated code ended up a bit messy so I took the liberty to rewrite it a bit. The functionality should be the same or even perhaps slightly better.

public void CustomPager(object STCPagerGrid)
{
if(!(STCPagerGrid is Control))
return;

Control ctr = (Control)STCPagerGrid;
for(int intCtr = 0; intCtr < ctr.Controls.Count; intCtr++)
{
Control c = (Control)ctr.Controls[intCtr];
if(c.GetType().ToString() == "System.Web.UI.WebControls.DataGridLinkButton")
{
double d;
bool isNumber = Double.TryParse(c.Text, System.Globalization.NumberStyles.Any, null, out d);

if(isNumber && c.Text.Substring(0, 1) != "[")
c.Text = "[" + c.Text + "]";
else if(c.Text == "..." && intCtr == 0)
c.Text = "[<<]";
else if(c.Text == "..." && intCtr > 0)
c.Text = "[>>]";
}

if(c.Controls.Count > 0)
CustomPager(c);
}
}


Happy coding!
Morten Wennevik [C# MVP]
 
N

Nicholas Paldino [.NET/C# MVP]

Morten,

You can use static IsNumeric method by setting a reference to
Microsoft.VisualBasic.dll and then calling the method on the Information
class in the Microsoft.VisualBasic namespace. You can access the Left
function as well on the Strings class, but since there is such an obvious
translation, I wouldn't bother. IsNumeric, however, has some very specific
logic which is not easily replicated with TryParse.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Morten Wennevik said:
hi Morten

i have problem with Datagrid paging .
After 10 pages it's giving Like 1 2 3 4 5 6 7 8 9 10 ...... these dot values iam converting to >> (like this) in VB.net
please see this function
Sub CustomPager(ByVal STCPagerGrid As Object)
Dim intCtr As Integer
Static intLastItem As Integer
Static intCount As Integer
Static intTotalPage, lintCurrentPage As Integer

For intCtr = 0 To STCPagerGrid.Controls.Count - 1

If STCPagerGrid.Controls(intCtr).GetType.ToString =
"System.Web.UI.WebControls.DataGridLinkButton" Then
If IsNumeric(STCPagerGrid.controls(intCtr).text) = True
And Left(STCPagerGrid.controls(intCtr).text, 1) <> "[" Then
STCPagerGrid.controls(intCtr).text = "[" &
STCPagerGrid.controls(intCtr).text & "]"
If STCPagerGrid.Controls(intCtr).Text = "..." And intCtr
= 0 Then
STCPagerGrid.Controls(intCtr).Text = "[<<]"
End If
If STCPagerGrid.Controls(intCtr).Text = "..." And intCtr
STCPagerGrid.Controls(intCtr).Text = "[>>]"
End If
End If
If STCPagerGrid.controls(intCtr).controls.count > 0 Then
CustomPager(STCPagerGrid.controls(intCtr))
End If
Next
End Sub



how to write in C#

Well, for one, you can't use STCPagerGrid for much as long as it is an
object. You need to either change the parameter to something else, like
Control, or cast it to something like Control inside the method.
You can't use method local static variables in C# (to my knowledge). If
you need to use static variables declare them outside the method.
There is no IsNumeric method in C#. The closest equivalent is probably
Double.TryParse which is slightly more complex, and in the code below I
haven't checked if you can set the numberformatprovider to null as I did.
There is no Left method in C#, use String.SubString(0, number of characters).

The translated code ended up a bit messy so I took the liberty to rewrite
it a bit. The functionality should be the same or even perhaps slightly
better.
public void CustomPager(object STCPagerGrid)
{
if(!(STCPagerGrid is Control))
return;

Control ctr = (Control)STCPagerGrid;
for(int intCtr = 0; intCtr < ctr.Controls.Count; intCtr++)
{
Control c = (Control)ctr.Controls[intCtr];
if(c.GetType().ToString() == "System.Web.UI.WebControls.DataGridLinkButton")
{
double d;
bool isNumber = Double.TryParse(c.Text,
System.Globalization.NumberStyles.Any, null, out d);
if(isNumber && c.Text.Substring(0, 1) != "[")
c.Text = "[" + c.Text + "]";
else if(c.Text == "..." && intCtr == 0)
c.Text = "[<<]";
else if(c.Text == "..." && intCtr > 0)
c.Text = "[>>]";
}

if(c.Controls.Count > 0)
CustomPager(c);
}
}


Happy coding!
Morten Wennevik [C# MVP]
 
M

Morten Wennevik

Morten,

You can use static IsNumeric method by setting a reference to
Microsoft.VisualBasic.dll and then calling the method on the Information
class in the Microsoft.VisualBasic namespace. You can access the Left
function as well on the Strings class, but since there is such an obvious
translation, I wouldn't bother. IsNumeric, however, has some very specific
logic which is not easily replicated with TryParse.

The documents say it will try to convert an object of type Short, Integer, Long, Decimal, Single, or String to a Double so I thought of using Double.TryParse, now realising it will only parse strings. However using ToString, won't that give it the exact same functionality, and more? In this context the parameter will always be string (Control.Text)

object o1 = 1;
object o2 = "2";
object o3 = "0x3af9"; // fails
object o4 = 0.5f;

double d;
Double.TryParse(o#.ToString(), NumberStyles.Any, null, out d) + " : " + d.ToString());

o1, o2, o4 works, but o3 fails. Does this work with IsNumeric?
 
N

Nicholas Paldino [.NET/C# MVP]

Morten,

0x3af9 will return false with IsNumeric. However, a VB programmer will
typically not use 0x3af9 to represent a hex number, they will use &h3af9,
which IsNumeric will return true on. VB6 returns false as well using
0x3af9, while returning true for &h3af9.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Morten Wennevik said:
The documents say it will try to convert an object of type Short, Integer,
Long, Decimal, Single, or String to a Double so I thought of using
Double.TryParse, now realising it will only parse strings. However using
ToString, won't that give it the exact same functionality, and more? In
this context the parameter will always be string (Control.Text)
 

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