明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1415|回复: 6

[基础] C#如何实现变量重复利用?

[复制链接]
发表于 2011-5-6 15:14 | 显示全部楼层 |阅读模式
本帖最后由 carrot1983 于 2011-5-6 15:22 编辑

简单写了个LISP的例子,如例子中的 pt 变量。
(setq pt (polar pt 0 20))

  1. (defun c:tt ()
  2.   (setq pt (getpoint "\n指定插入点: "))
  3.   (repeat 10
  4.     (setq pt (polar pt 0 20))
  5.     (command "._CIRCLE" "_NON" pt 5)
  6.   )
  7.   (princ)
  8. )



想要实现在循环语句中,变量重复利用,但是在C#下面,一直没开窍。
错误提示:
不能在此范围内声明名为“insertPoint”的局部变量,因为这样会使“insertPoint”具有不同的含义,而它已在“父级或当前”范围中表示其他内容了 (CS0136) - D:\Sharp\work\cs20110505a\cs20110505a\MyClass.cs:62,13

C#代码如下:
            PromptPointResult pPtRes;
            PromptPointOptions pPtOpts = new PromptPointOptions("");
            // 提示开始点
            pPtOpts.Message = "\n指定插入点: ";
            pPtRes = doc.Editor.GetPoint(pPtOpts);
            Point3d insertPoint = pPtRes.Value;
            // 如果用户按了 ESC 键或取消了命令就退出
            if (pPtRes.Status == PromptStatus.Cancel) return;
            
            for (int i = 0; i < 10;i++)
            {
                Point3d insertPoint = PolarPoint(insertPoint,0,20); //这里出错了。。。如何修改才是对的。
                ObjectId entId = AddCircle(insertPoint, 5);
            }


// Polar
        public static Point3d PolarPoint(Point3d pPt, double dAng, double dDist)
        {
            return new Point3d(pPt.X + dDist * Math.Cos(dAng),
                               pPt.Y + dDist * Math.Sin(dAng),
                               pPt.Z);
        }



// 将图形对象加入到模型空间的函数.
        public static ObjectId AppendEntity(Entity ent)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            ObjectId entId;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                entId = btr.AppendEntity(ent);
                trans.AddNewlyCreatedDBObject(ent, true);
                trans.Commit();
            }
            return entId;
        }
        
        // 由圆心和半径创建圆的函数.
        public static ObjectId AddCircle(Point3d cenPt, double radius)
        {
            Circle ent = new Circle(cenPt, Vector3d.ZAxis, radius);
            ObjectId entId = AppendEntity(ent);
            return entId;
        }

发表于 2011-5-6 19:18 | 显示全部楼层
Point3d insertPoint = PolarPoint(insertPoint,0,20); 改为
insertPoint = PolarPoint(insertPoint,0,20)
 楼主| 发表于 2011-5-6 21:27 | 显示全部楼层
chmenf087 发表于 2011-5-6 19:18
Point3d insertPoint = PolarPoint(insertPoint,0,20); 改为
insertPoint = PolarPoint(insertPoint,0,20)

谢谢指点。。。
发表于 2011-5-7 12:22 | 显示全部楼层
你在外面定义了该点,又在循环体里面定义了,你不用定义,直接使用就可以了
发表于 2011-5-7 12:23 | 显示全部楼层
或者使用其他变量名
发表于 2011-5-10 18:40 | 显示全部楼层
语言基础不扎实哦,呵```
 楼主| 发表于 2011-5-10 20:19 | 显示全部楼层
pyt5208 发表于 2011-5-10 18:40
语言基础不扎实哦,呵```

没有基础。。。呵呵
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-17 10:19 , Processed in 0.273209 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表