简体   繁体   English

当前上下文中不存在名称“Fix”

[英]The name 'Fix' does not exist in the current context

In my project I am converting some vb.net to c# and I came to this line:在我的项目中,我将一些 vb.net 转换为 c#,然后我来到了这一行:

int thisdigit = Fix(countervalue / (Math.Pow(10, (numdigits - j - 1)))) - Fix(countervalue / (Math.Pow(10, (numdigits - j)))) * 10;

But I get the error:但我收到错误:

The name 'Fix' does not exist in the current context

How do I fix this?我该如何解决? I can't understand why Fix() wouldn't exist.我不明白为什么 Fix() 不存在。 But if I used Math.Truncate() instead, well, that doesn't work because thisdigit is an int .但是,如果我使用Math.Truncate()代替,那将不起作用,因为thisdigit是一个int How could I change that?我怎么能改变呢?

Here is my original vb.net code:这是我原来的 vb.net 代码:

dim dg as int
dg = Fix(value / (10 ^ (digits - j - 1))) - Fix(value / (10 ^ (digits - j))) * 10

Here is a link to what I'm trying to convert: https://www.developerfusion.com/code/3734/aspnet-graphical-page-hit-counter/这是我尝试转换的链接: https : //www.developerfusion.com/code/3734/aspnet-graphical-page-hit-counter/

The code works in my vb.net projects.该代码适用于我的 vb.net 项目。 I've run my converted code through the debugger, and the only place I can see any problem is with this line.我已经通过调试器运行了转换后的代码,唯一能看出任何问题的地方就是这一行。

I came up with this, too:我也想出了这个:

double thisdigit = Math.Truncate((double)(countervalue / (10 ^ (numdigits - j - 1)))) - Math.Truncate(((double)(countervalue / (10 ^ (numdigits - j))) * 10));

I don't really understand why this is so convoluted..我真的不明白为什么这如此令人费解..

The original code seems to draw a counter, one digit at a time:原始代码似乎在绘制一个计数器,一次一位:

dim j as Integer, dg as Integer
for j = 0 to (digits-1)
   ' Extract digit from value
   dg = fix(value / (10^(digits - j - 1))) - fix(value / (10^(digits - j)))*10
   ' Add digit to the output graphic
   g.drawimage(i, New rectangle(j*dgwidth, 0, dgwidth, dgheight), New rectangle(dg*dgwidth, 0, dgwidth, dgheight), GraphicsUnit.Pixel)
   
next j

But surely it would just be easier to do something like:但肯定会更容易做这样的事情:


int pageCounter = 7234283;

string toDraw = pageCounter.ToString();

for(int i = 0; i < toDraw.Length; i++)
    someGraphics.DrawString(toDraw.Substring(i, 1), someFont, someBrush, new PointF(i * 10.0f, 0));

Or perhaps:也许:


int pageCounter = 7234283;

string toDraw = pageCounter.ToString();

PointF p = new PointF(0.0f, 0.0f);

foreach(char c in toDraw){
    someGraphics.DrawString(c.ToString(), someFont, someBrush, p);
    p.X += 10.0f;
}

With some help from Aidy in the C# Discord, I ended up with this.在 C# Discord 中 Aidy 的帮助下,我最终得到了这个。 Very simple and clean.非常简单和干净。

//Get the number of digits to display in the output graphic
//If the countervalue is 16 then "16".ToString("D5") converts it to "00016".  
//ToCharArray() turns that into an array of characters ['0', '0', '0', '1', '6']. 
//We loop through that list and convert the char back to int and we get 0, 0, 0, 1 and 6.
//Thanks to @Aidy in the C# Discord for help on this

int numdigits = Convert.ToInt32(Request.QueryString["digits"]);
var digits = countervalue.ToString("D" + numdigits.ToString()).ToCharArray();

//Create an output object
Bitmap imageoutput = new Bitmap(digitwidth * digits.Length, digitheight, PixelFormat.Format24bppRgb);  //should be 5*15 = 75 for digits.gif
Graphics graphic = Graphics.FromImage(imageoutput);  //here is our black box

//digits.gif is 150 x 20px; 
//So, if our countervalue = 16, and numdigits = 5, we want to display 00016.

for(int j = 0; j < digits.Length; j++) {
    //We loop through that digits and convert the char back to int and we get 0, 0, 0, 1 and 6.
    int thisdigitX = int.Parse(digits[j].ToString());

    //add the digit to the output graphic 
    graphic.DrawImage(digitpix, new Rectangle(j * digitwidth, 0, digitwidth, digitheight), new Rectangle(thisdigitX * digitwidth, 0, digitwidth, digitheight), GraphicsUnit.Pixel);
}

However, I was also able to get the original code to work with the conversion of int and double and importing the Visual Basic assembly.但是,我还能够获得原始代码来处理 int 和 double 的转换以及导入 Visual Basic 程序集。

using Microsoft.VisualBasic;
  int thisdigitX = Conversion.Fix(countervalue / ((int)Math.Pow(10, (double)(numdigits - j - 1)))) - ( Conversion.Fix(countervalue / ((int)Math.Pow(10,(double)(numdigits - j)))) * 10);

Here is a link to my github page where I've posted the entire working project if anyone is really interested.这是我的 github 页面的链接,如果有人真的感兴趣,我已经在其中发布了整个工作项目 (not self-promoting here - just sharing; I don't care if anyone uses it or not). (这里不是自我推销——只是分享;我不在乎是否有人使用它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM