隐式类型的局部变量必须初始化 [英] Implicitly-typed local variables must be initialized

查看:145
本文介绍了隐式类型的局部变量必须初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

public void GetMenu(ref List<Menu> getmenu, int clientIdentity)
        {
            clsSqlServer server = null;
            DataSet set = new DataSet();
            try
            {
                try
                {
                    CurryCupTrace.Verbose(TraceType.DAL, "GETMENU - Started");
                    CurryCupTrace.Verbose(TraceType.DAL, "GETMENU -  Calling the GetCountry Sp using DsQueryStoredProc");
                    server = new clsSqlServer();
                    SqlParameter[] parms = new SqlParameter[] { server.MakeParameter("@CLIENT_IDENTITY", SqlDbType.Int, clientIdentity) };
                    set = server.DsQueryStoredProc("GETMENU", parms);
                    if ((set != null) && (set.Tables[0].Rows.Count > 0))
                    {
                        var source = set.Tables[0].AsEnumerable().Select(r => new
                        {
                            CategoryName = r.Field<string>("CategoryName"),
                            CategoryDescription = r.Field<string>("CategoryDescription"),
                            ParentCategoryId = r.Field<int>("ParentCategoryId"),
                            DisplayOrder = r.Field<int>("DisplayOrder"),
                            LanguageType = r.Field<string>("LanguageType")
                        }).Distinct();
                        List<Menu> list = new List<Menu>();
                        using (var enumerator = source.Distinct().GetEnumerator())
                        {
                            var predicate ;
                            var item ;

                            
                            while (enumerator.MoveNext())
                            {
                                item = enumerator.Current;
                                Menu menu = new Menu
                                {
                                    CategoryName = item.CategoryName,
                                    CategoryDescription = item.CategoryDescription,
                                    DisplayOrder = item.DisplayOrder,
                                    ParentCategoryId = item.ParentCategoryId,
                                    LanguageType = item.LanguageType,
                                    MenuID = Guid.NewGuid().ToString()
                                };
                                if (predicate == null)
                                {
                                    predicate = r => r.CategoryName == item.CategoryName;
                                }
                                var list2 = set.Tables[0].AsEnumerable().Select(r => new
                                {
                                    productname = r.Field<string>("productname"),
                                    SHORTDESCRIPTION = r.Field<string>("SHORTDESCRIPTION"),
                                    ParentCategoryId = r.Field<int>("ParentCategoryId"),
                                    DisplayOrder = r.Field<int>("DisplayOrder"),
                                    CategoryName = r.Field<string>("CategoryName"),
                                    THUMBIMG1 = r.Field<string>("THUMBIMG1"),
                                    THUMBIMG2 = r.Field<string>("THUMBIMG2")
                                }).Where(predicate).ToList();
                                menu.products = new List<prodcutdetails>();
                                foreach (var type in list2)
                                {
                                    prodcutdetails prodcutdetails = new prodcutdetails
                                    {
                                        productname = type.productname,
                                        SHORTDESCRIPTION = type.SHORTDESCRIPTION,
                                        ParentCategoryId = type.ParentCategoryId,
                                        DisplayOrder = type.DisplayOrder,
                                        THUMBIMG1 = type.THUMBIMG1,
                                        THUMBIMG2 = type.THUMBIMG2
                                    };
                                    prodcutdetails.ProductDetailsID = Guid.NewGuid() + (((list2.Count() - 1) + 0x7b)).ToString();
                                    menu.products.Add(prodcutdetails);
                                }
                                getmenu.Add(menu);
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    CurryCupTrace.Verbose(TraceType.DAL, "GetTableBooking Method Failed for the following Reason: " + exception.Message);
                    throw exception;
                }
            }
            finally
            {
            }
        }


当我运行项目时,我在 var谓词行出现构建错误; var item;  错误为 必须初始化隐式类型的局部变量"

when i run the project im getting build error at the line var predicate ; var item ; error as "Implicitly-typed local variables must be initialized"

我在另一台计算机上运行了相同的代码,在那儿它不会提示此错误.

I ran the same code in another computer, there it wont prompt this error.

我可以知道如何解决此问题.

may i know how to resolve this issue.

预先感谢


推荐答案

看看

Have a look here for what "var" actually means.

基本上,而不是自己指示变量的类型,例如"int i",您是在告诉编译器自己进行编译.但是,只有将变量初始化为某个值,它才能做到这一点.

Basically, instead of indicating the type of the variable yourself, e.g. "int i", you are telling the compiler to work it out for itself. But it can only do that if you also initialise the variable to some value.

例如

var i = 0;

如果不想初始化,则必须直接指定类型,而不要使用var关键字

If you don't want to initialise, then you must specify the type directly and not use the var keyword


这篇关于隐式类型的局部变量必须初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆