【问题标题】:The name 'Fix' does not exist in the current context当前上下文中不存在名称“修复”
【发布时间】:2020-11-08 03:00:01
【问题描述】:

在我的项目中,我将一些 vb.net 转换为 c#,我来到了这一行:

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

但我得到了错误:

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

我该如何解决这个问题?我不明白为什么 Fix() 不存在。 但是,如果我改用Math.Truncate(),那将不起作用,因为thisdigitint。 我怎么能改变它?

这是我原来的 vb.net 代码:

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

这是我要转换的内容的链接: https://www.developerfusion.com/code/3734/aspnet-graphical-page-hit-counter/

该代码适用于我的 vb.net 项目。我已经通过调试器运行了转换后的代码,我唯一能看到任何问题的地方就是这一行。

我也想出了这个:

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

【问题讨论】:

  • 修复在 VB.net 代码中有什么作用?你为什么要写这段代码?
  • 无论如何你都不应该在 VB 中使用Fix。这是 VB6 的保留,这就是它特定于 VB.NET 而不是 C# 的原因。如果您一开始就编写了“正确的”VB.NET 代码,那么您几乎可以附加一个分号并完成 C# 转换。你应该做的是正确理解你的代码实际在做什么,在 VB 中编写最好的 .NET 代码来做到这一点,然后将其转换为 C#。
  • 在 Microsoft.VisualBasic 命名空间下的 Microsoft .VisualBasic.dll 库中有一堆类型和成员,它们基本上是对 VB6 类型和成员的重新实现。它们的存在主要是为了让 VB6 代码无需大量更改即可升级到 VB.NET,也让 VB6 开发人员对 VB.NET 感到更加熟悉。如果您引用该库,则可以在 C# 中使用这些类型和函数,但这通常是个坏主意。 Fix实际上是Microsoft.VisualBasic.Conversion模块的成员,在C#中可以看作是一个静态类。
  • 原代码不是我写的……
  • @JohnG 呃,字符串中的所有数字都已经是int。该行的问题是,如果我使用Math.Truncate() 而不是Fix(),它将不起作用,因为Math.Truncate() 仅适用于double

标签: c# vb.net code-conversion


【解决方案1】:

我真的不明白为什么这么复杂..

原始代码似乎画了一个计数器,一次一位数:

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

但肯定会更容易做这样的事情:


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));

或许:


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;
}

【讨论】:

  • 我发布了我想出的东西,效果很好。但是,您提出了类似的建议,因此我将您的解决方案标记为解决方案,因为我不喜欢接受自己的答案...
  • 我还想说,从本质上讲,VB 代码看起来像 723421 这样的 int 并将其从左到右分成 7、2、3、4、2、1 的数字进行十的幂的整数除法.. 但是如果你想像这样在音乐上这样做,它可能会使更简单的代码重复修改/除以 10 并从右到左工作,也可以通过在 X 处绘画从右到左绘画= 60,然后 X = 50 ... 所以 for 循环看起来像“绘制模 10,除以 10..”
【解决方案2】:

在 C# Discord 中 Aidy 的帮助下,我完成了这个。非常简单干净。

//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);
}

但是,我还能够获得原始代码来处理 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);

这是我的 github 页面的链接,如果有人真的感兴趣,我已经在其中发布了 entire working project。 (这里不是自我推销——只是分享;我不在乎是否有人使用它)。

【讨论】:

    猜你喜欢
    • 2014-04-18
    • 2019-06-23
    • 2019-09-08
    • 2013-10-02
    • 2014-04-22
    • 2017-06-17
    • 2015-09-11
    • 1970-01-01
    相关资源
    最近更新 更多