本文介绍了由于保护级别,不能在同一名称空间中对公共类使用构造函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试映射很多频道,因此为了方便起见,制作了一个可以包含频道属性和字典的类,该类将ID映射到频道。字典在一个类上,而通道定义在它们自己的类中,但是在同一名称空间中。

I am currently trying to map alot of channel, and have thus for convenient sake, made a class that can contain the channel properties and a dictionary, that maps an ID to a channel. The dictioanary is on one class and the channel definition is in their own classes but, within the same namespace.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ParentId = System.String;
using ChildIds = System.Collections.Generic.List<int>;
using ChannelName = System.String;
using CatalogEntry = System.Tuple<System.String, System.Collections.Generic.List<int>, System.String>;


namespace example
{
    public class ChannelHandler
    {
        public ChannelName channelname { get; set; }
        public ParentId parentid { get; set; }
        public ChildIds list_of_childs { get; set; }
        public int id;

        ChannelHandler(ChannelName name, int id)
        {
            this.channelname = name;
            this.id = id;
        }

        ChannelHandler(ChannelName name, int id, ParentId parentid)
        {
            this.channelname = name;
            this.id = id;
            this.parentid = parentid;
        }

    }

    public class Class1
    {
        private Dictionary<int?, ChannelHandler> dictionary = new Dictionary<int?, ChannelHandler>();
        public void inser_into_dictionary(string path)
        {
            string[] separatingChars = { "  " };
            string[] splittedPath = path.Split(separatingChars, StringSplitOptions.RemoveEmptyEntries);
            int parentId = 0;
            ChannelName parentName = string.Empty;
            foreach (string channel_id in splittedPath)
            {

                ChannelName name = channel_id.Split('_').ElementAt(0);
                string id = channel_id.Split('_').ElementAt(1);
                int int_id = 0;
                if (int.TryParse(id, out int_id))
                {
                    int_id = int.Parse(id);
                }

                Tuple<string, int> pair_of_channel_and_id = new Tuple<string, int>(name, int_id);
                if (this.dictionary.ContainsKey(int_id))
                {
                    // Key exist, meaning this is a parent.
                    // Child entry has to be updated.
                    parentId = int_id;
                    parentName = name;
                }
                else
                {
                    if (parentId == 0)
                    {
                        ChannelHandler channel = new ChannelHandler(name, int_id); //ChannelHandler.ChannelHandler is inaccesible due to protection level.
                        this.dictionary.Add(int_id, channel);

                    }
                }
            }
        }
    }
}

我似乎无法创建对象 channel ,因为基于保护级别,该类的构造函数似乎无法使用?但是保护级别是什么-一切都是公开的?

I seem to be unable to create the object channel as the constructor for the class seem to inaccesible due to protection level? but what protection level - everything is public?

推荐答案

它可能看起来像:

public class ChannelHandler
{
    public ChannelName ChannelName { get; set; }
    public ParentId ParentId { get; set; }
    public List<ChildId> ChildIds { get; set; }
    public int Id;

    public ChannelHandler(ChannelName name, int id)
    {
        ChannelName = name;
        Id = id;
    }

    public ChannelHandler(ChannelName name, int id, ParentId parentid)
    {
        ChannelName = name;
        Id = id;
        ParentId = parentid;
    }

默认情况下,类的任何元素的访问级别为私人。您需要使构造函数成为公开内部,以便可以在类本身的外部进行访问。如果您看这篇文章在这里解释了访问修饰符如何影响代码的可访问性级别。

By default, the access level of any element of a class is private. You need to make the constructors either public or internal so that can be accessed externally to the class itself. If you look at this article here it explains how access modifiers affect the accessibility levels of your code.

我进行的另一项更改是更改成员的大小写以满足 c#编码约定。请参见

The other change I made was to change the case of your members to meet c# coding conventions. See here

这篇关于由于保护级别,不能在同一名称空间中对公共类使用构造函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:50