专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > C++ Builder

C#程序出错(委托类型)解决方案

发布时间:2011-06-28 14:06:43 文章来源:www.iduyao.cn 采编人员:星星草
C#程序出错(委托类型)
C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication5
{
    class Program
    {
        delegate double ptocessDelegate(double paraml, double param2);
        static double Multiply(double param1, double param2)
        {
            return param1 * param2;
        }
        static double Divide(double param1, double param2)
        {
            return param1 / param2;
        }
        static void Main(string[] args)
        {
            ptocessDelegate ptocess;
            Console.WriteLine("Enter 2 numbers separated with a comma:");
            string input = Console.ReadLine();
            int commaPos = input.IndexOf(',');
            double param1 = Convert.ToDouble(input.Substring(0, commaPos));
            double param2 = Convert.ToDouble(input.Substring(commaPos + 1, input.Length - commaPos - 1));
            Console.WriteLine("Enter M to multiply or D to divide:");
            input = Console.ReadLine();
            if (input == "M")
                ptocess = new ptocessDelegate(Divide);
            Console.WriteLine("Result: {0}", ptocess(param1, param2));
            Console.ReadKey();
        }
    }
}


错误信息:
使用了为赋值的局部变量"ptocess"
初学者书上的代码。最好帮忙解释下程序

------解决方案--------------------
if (input == "M")
ptocess = new ptocessDelegate(Divide);


这个的问题,这么改就可以
if (input == "M")
ptocess = new ptocessDelegate(Divide);
else
ptocess = new ptocessDelegate(Multiply);
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: