简体   繁体   English

如何在C#中从一个类访问另一个类的字符串数组

[英]How can I access a string array from one class to another in c#

I want to access a string array from one class to another but I have no idea how. 我想从一个类访问另一个类的字符串数组,但我不知道如何。 I have tried to use BuilderPages_AutoGenerate reference = new BuilderPages_AutoGenerate(); 我试图使用BuilderPages_AutoGenerate reference = new BuilderPages_AutoGenerate(); but I can't access the variables using the reference that I make with that line. 但我无法使用该行中的reference来访问变量。 I've also tried to make each of the arrays public but that is not allowed. 我也尝试将每个数组公开,但这是不允许的。 Here is the code: 这是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data;
using System.Data.Common;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;

public partial class BuilderPages_AutoGenerate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int i = 0;
        string[] nameHolder = new string[6];
        string[] typeHolder = new string[6];
        Console.WriteLine("Hello World");

        string connection = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
        SqlConnection conn = null;
        conn = new SqlConnection(connection);
        conn.Open();
        DataTable schema = null;
        using (var con = new SqlConnection(connection))
        {
            using (var schemaCommand = new SqlCommand("SELECT * FROM TestTable;", con))
            {
                con.Open();
                using (var reader = schemaCommand.ExecuteReader(CommandBehavior.SchemaOnly))
                {
                    schema = reader.GetSchemaTable();
                }
            }
        }
        foreach (DataRow col in schema.Rows)
        {
            Console.WriteLine("ColumnName={0}", col.Field<String>("ColumnName"));
            nameHolder[i] = col.Field<string>("ColumnName");
            typeHolder[i] = col.Field<Type>("DataType").ToString();
            i++;
        }
        /* Testing if the name holder is getting the names of the columns in the given table */
        test.Text = nameHolder[0];
        test2.Text = nameHolder[1];
        test3.Text = nameHolder[2];
        test4.Text = nameHolder[3];
        test5.Text = nameHolder[4];
        test6.Text = nameHolder[5];

        /* Testing to see if the type holders is getting the type of the columns */
        type.Text = typeHolder[0];
        type2.Text = typeHolder[1];
        type3.Text = typeHolder[2];
        type4.Text = typeHolder[3];
        type5.Text = typeHolder[4];
        type6.Text = typeHolder[5];
    }
    public class testing
    {
        //I want to use nameHolder and typeHolder here!!
    }
}

Thank you in advanced. 在此先感谢您。

Your other method should accept the data that it needs as parameters, it shouldn't be reaching into the page class directly to access the data. 您的其他方法应将所需的数据作为参数接受,不应直接进入页面类以访问数据。

public class testing 
{
    public static void Foo(string[] nameHolder)
    {
         //do stuff with nameHolder
    }
}

Or, if appropriate, accept the values in the constructor and store it as an instance field. 或者,如果合适,接受构造函数中的值并将其存储为实例字段。

When the BuilderPages_AutoGenerate either creates an instance of testing or calls an appropriate method it can pass whatever it needs. BuilderPages_AutoGenerate创建testing实例或调用适当的方法时,它可以传递任何所需的信息。

In the constructor of your testing class, have it accept two parameters, one for _names and other for _types , like this: testing类的构造函数中,让它接受两个参数,一个用于_names ,另一个用于_types ,如下所示:

public class testing
{
    private string[] names;
    private string[] types;

    public testing(string[] _names, string[] _types)
    {
        names = _names;
        types = _types;
    }

    // Add methods here that do something with names and types arrays
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中从IDataReader Func调用的另一个属性中访问类属性 - How can i access a class properties from within another one called from a IDataReader Func in C# 如何在 C# winforms 中将数组从一个类访问到另一个类? - how to access array from one class to another class in C# winforms? 如何从另一类的一个类访问变量? [C#] - How to Access Variable From One Class in Another Class? [C#] 如何访问从公共 class C# 继承的另一个 class 中的值? - How can I access values in another class inherited from public class C#? 如何从同一 class C# 中的另一种方法访问一种方法的变量? - How do I access the variable of one method from another method within the same class C#? 我如何从另一个类访问一个类数组 - How can I access a class array from another class 如何从 c# 中的另一个 class 访问私人列表,以便我可以比较两个列表字段? - How to access private list from another class in c# so that I can compare two list fields? 如何从C#中打开的窗口访问另一个窗口 - How can I access another window from an opened one in c# 如何在主类C#中将值从一个子类传递到另一个子类? - How can I pass value from one subclass to another subclass in a main class C#? 如何从 c# 中的字符串数组访问值? - How do I access values from string array in c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM