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

本文关键字:一个 另一个 数组 字符串 访问 | 更新日期: 2023-09-27 18:20:15

我想从一个类访问另一个类的字符串数组,但我不知道如何访问。我曾尝试使用BuilderPages_AutoGenerate reference = new BuilderPages_AutoGenerate();,但无法使用我用该行生成的reference访问变量。我还尝试将每个数组公开,但这是不允许的。这是代码:

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!!
    }
}

提前谢谢。

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

您的另一个方法应该接受它需要的数据作为参数,它不应该直接进入页面类来访问数据。

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

或者,如果合适,接受构造函数中的值并将其存储为实例字段。

BuilderPages_AutoGenerate创建testing的实例或调用适当的方法时,它可以传递所需的任何内容。

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
}