zoukankan      html  css  js  c++  java
  • 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]);
            }
     

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

  • 相关阅读:
    Vue的响应式
    让html上两个元素在一行显示
    linux的<<命令
    http-only,withCredentials
    axios跨域请求时 withCredentials:true 表示request携带cookie
    异步代码async await阻塞进程的误区——await的是Promise的resolve而不是语句块的执行结束
    理解状态机
    关于express返回值的问题
    axios基本的get/post请求使用方法
    【转】 前端笔记之Vue(四)UI组件库&amp;Vuex&amp;虚拟服务器初识
  • 原文地址:https://www.cnblogs.com/wwwzzg168/p/3569085.html
Copyright © 2011-2022 走看看