0

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(); but I can't access the variables using the reference that I make with that line. 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.

5
  • You can create a singleton in the BuilderPages_AutoGenerate class which would then have properties for both nameHolder and typeHolder members. Commented Jun 28, 2013 at 20:23
  • Hi Andy, it looks you don't understand the concept of classes and scope. Because even though you get your refence to work, it will still be emptyh. I suggest you start with a book about how to program: amazon.com/Microsoft-Programming-For-Absolute-Beginner/dp/… Commented Jun 28, 2013 at 20:25
  • what do you need to do this for? what is your real requirement? Commented Jun 28, 2013 at 20:26
  • A static singleton is not right since multiple classes will be created when serving different requests. You better off by creating that class when you need it and pass it in the parameters you need. Commented Jun 28, 2013 at 20:26
  • @sam Thank you I'll actually be taking a c# class soon! Commented Jun 28, 2013 at 20:35

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this is what I was trying to do!
0

In the constructor of your testing class, have it accept two parameters, one for _names and other for _types, like this:

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
}

2 Comments

What is the need for making the string arrays public properties?
Good point, not needed, my mind started on one track and switched mid answer. Updated and removed. Good catch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.