Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在c中从一个类访问另一个类的字符串数组_C#_Asp.net - Fatal编程技术网

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

C# 如何在c中从一个类访问另一个类的字符串数组,c#,asp.net,C#,Asp.net,我想从一个类到另一个类访问字符串数组,但不知道如何访问。我尝试使用BuilderPages\u AutoGenerate reference=newbuilderpages\u AutoGenerate;但是我不能使用我用那条线做的引用来访问变量。我还尝试将每个数组公开,但这是不允许的。代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We

我想从一个类到另一个类访问字符串数组,但不知道如何访问。我尝试使用BuilderPages\u AutoGenerate reference=newbuilderpages\u AutoGenerate;但是我不能使用我用那条线做的引用来访问变量。我还尝试将每个数组公开,但这是不允许的。代码如下:

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

感谢您的高级支持。

您的其他方法应该接受它所需的数据作为参数,它不应该直接进入page类来访问数据

public class testing 
{
    public static void Foo(string[] nameHolder)
    {
         //do stuff with nameHolder
    }
}
或者,如果合适,接受构造函数中的值并将其存储为实例字段


当BuilderPages\u AutoGenerate创建测试实例或调用适当的方法时,它可以传递任何需要的信息。

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

public class testing 
{
    public static void Foo(string[] nameHolder)
    {
         //do stuff with nameHolder
    }
}
或者,如果合适,接受构造函数中的值并将其存储为实例字段


当BuilderPages\u AutoGenerate创建测试实例或调用适当的方法时,它可以根据需要传递任何内容。

在测试类的构造函数中,让它接受两个参数,一个用于\u名称,另一个用于\u类型,如下所示:

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
}

在测试类的构造函数中,让它接受两个参数,一个用于_名称,另一个用于_类型,如下所示:

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
}

您可以在BuilderPages\u AutoGenerate类中创建一个单例,该类将同时具有nameHolder和typeHolder成员的属性。因为即使你让你的参考文献去工作,它仍然是空的。我建议你从一本关于如何编程的书开始:你为什么需要这样做?您真正的需求是什么?静态单例是不正确的,因为在服务不同的请求时会创建多个类。您最好在需要时创建该类,并将其传递给所需的参数。@sam谢谢您,我很快就要学习c类了!您可以在BuilderPages\u AutoGenerate类中创建一个单例,该类将同时具有nameHolder和typeHolder成员的属性。因为即使你让你的参考文献去工作,它仍然是空的。我建议你从一本关于如何编程的书开始:你为什么需要这样做?您真正的需求是什么?静态单例是不正确的,因为在服务不同的请求时会创建多个类。您最好在需要时创建该类,并将其传递给所需的参数。@sam谢谢您,我很快就要学习c类了!谢谢,这就是我想做的!谢谢,这就是我想做的!什么是使字符串数组成为公共属性的必要条件?很好的一点,不是必要的,我的思想从一个轨道开始,然后切换到中间答案。已更新并删除。很好的理解。什么是使字符串数组成为公共属性的必要条件?很好的一点,不是必要的,我的思维从一个轨道开始,然后切换到中间答案。已更新并删除。抢手货