乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 99|回复: 6

C#如何实现变量重复利用?

[复制链接]

52

主题

380

帖子

11

银币

中流砥柱

Rank: 25

铜币
588
发表于 2011-5-6 15:14:00 | 显示全部楼层 |阅读模式
简单写了个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;
        }
回复

使用道具 举报

8

主题

138

帖子

5

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
170
发表于 2011-5-6 19:18:00 | 显示全部楼层
Point3d insertPoint = PolarPoint(insertPoint,0,20); 改为
insertPoint = PolarPoint(insertPoint,0,20)
回复

使用道具 举报

52

主题

380

帖子

11

银币

中流砥柱

Rank: 25

铜币
588
发表于 2011-5-6 21:27:00 | 显示全部楼层

谢谢指点。。。
回复

使用道具 举报

19

主题

154

帖子

5

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
230
发表于 2011-5-7 12:22:00 | 显示全部楼层
你在外面定义了该点,又在循环体里面定义了,你不用定义,直接使用就可以了
回复

使用道具 举报

19

主题

154

帖子

5

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
230
发表于 2011-5-7 12:23:00 | 显示全部楼层
或者使用其他变量名
回复

使用道具 举报

0

主题

4

帖子

2

银币

初来乍到

Rank: 1

铜币
4
发表于 2011-5-10 18:40:00 | 显示全部楼层
语言基础不扎实哦,呵```
回复

使用道具 举报

52

主题

380

帖子

11

银币

中流砥柱

Rank: 25

铜币
588
发表于 2011-5-10 20:19:00 | 显示全部楼层

没有基础。。。呵呵
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-6-29 12:37 , Processed in 1.707166 second(s), 67 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表