• c# asp.net 多数组索引的解决方法


    本人今天做了一个功能 需要在一个类里用多个数组,

    数组需要索引器来调用  一个数组

    我查了msdn 一个类里面只能有一个this 索引器

    那这么多数组如何构造索引呢

    我在坛子里找到了解决之道

    view plaincopy to clipboardprint?
    using System;  
     
    namespace TestUse  
    {  
        /// <summary>  
        /// Summary description for Muliti.  
        /// </summary>  
        public class Muliti  
        {  
            public Muliti()  
            {  
                //  
                // TODO: Add constructor logic here  
                //  
            }  
     
            private string[] test1;  
            private object[] test2;  
            private int[]    test3;  
     
            public object this[string arrname,int index]{  
                get{  
                    switch(arrname){  
                        case "test1":return test1[index];  
                        case "test2":return test2[index];  
                        case "test3":return test3[index];  
                        default:return null;  
                    }  
                }  
                set{  
                    switch(arrname)  
                    {  
                        case "test1":test1[index]=value.ToString();break;  
                        case "test2":test2[index]=value;break;  
                        case "test3":test3[index]=(int)value;break;  
                        default:break;  
                    }  
                }  
            }  
     
            public void setUpArray(){  
                test1 = new string[3];  
                test2 = new object[2];  
                test3 = new int[4];  
            }  
        }  

    using System;

    namespace TestUse
    {
        /// <summary>
        /// Summary description for Muliti.
        /// </summary>
        public class Muliti
        {
            public Muliti()
            {
                //
                // TODO: Add constructor logic here
                //
            }

            private string[] test1;
            private object[] test2;
            private int[]    test3;

            public object this[string arrname,int index]{
                get{
                    switch(arrname){
                        case "test1":return test1[index];
                        case "test2":return test2[index];
                        case "test3":return test3[index];
                        default:return null;
                    }
                }
                set{
                    switch(arrname)
                    {
                        case "test1":test1[index]=value.ToString();break;
                        case "test2":test2[index]=value;break;
                        case "test3":test3[index]=(int)value;break;
                        default:break;
                    }
                }
            }

            public void setUpArray(){
                test1 = new string[3];
                test2 = new object[2];
                test3 = new int[4];
            }
        }
    }
     

    view plaincopy to clipboardprint?
    private void button1_Click(object sender, System.EventArgs e)  
      {  
          Muliti testm = new Muliti();  
          testm.setUpArray();  
          testm["test1",0]="test1-0";  
          testm["test2",0]= "test2-0";  
          testm["test3",0]= 3;  
          MessageBox.Show((string)testm["test1",0]);  
          MessageBox.Show((string)testm["test2",0]);  
          MessageBox.Show("" + testm["test3",0]);  
      } 
          private void button1_Click(object sender, System.EventArgs e)
            {
                Muliti testm = new Muliti();
                testm.setUpArray();
                testm["test1",0]="test1-0";
                testm["test2",0]= "test2-0";
                testm["test3",0]= 3;
                MessageBox.Show((string)testm["test1",0]);
                MessageBox.Show((string)testm["test2",0]);
                MessageBox.Show("" + testm["test3",0]);
            }
     

    这样 加个判断就行了 有时候头脑还真的迷糊 想了半天还是没想起来。

  • 相关阅读:
    centos7 关闭firewall安装iptables并配置
    Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
    URL地址下载图片到本地
    IDEA常用快捷键
    电商的支付前、中、后这3个流程都是怎么设计的?
    jenkins的部署
    mysql 授权用户 主从和备份
    windows下利用iis建立网站网站并实现局域共享
    nginx反向代理 和部分优化
    LNMP的搭建 及地址转换
  • 原文地址:https://www.cnblogs.com/wwwzzg168/p/3569085.html
Copyright © 2020-2023  润新知