locked
convert string[] array to var in c# RRS feed

  • Question

  • User736276958 posted

    how to convert string[] array to var in c#

    my code is

    List<string> list = new List<string>();

    for (int i = 0; i < dt.Rows.Count; i++)
    {
    dr = dt.Rows[i];

    //String selectedGID = dr["gid"].ToString();

    //gid = selectedGID;
    String columnAsString = dr["gid"].ToString();
    list.Add(columnAsString);

    }

    string[] array = list.ToArray();

    now how to convert array to var

    Wednesday, March 18, 2015 6:39 AM

Answers

  • User2103319870 posted

    i got it

    but i got another problem ,the parameters are passing like this "[1,2,3,4,5,6,]"

    but i want like this [1,2,3,4,5,6,] ie without double codes

    plz tell me this

    Try with the bellow code

    List<string> list = new List<string>();
    
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dr = dt.Rows[i];
    
                        //String selectedGID = dr["gid"].ToString();
    
                        //gid = selectedGID;
                        String columnAsString = dr["gid"].ToString();
                        list.Add(columnAsString);
    
                    }
    
                    string[] array = list.ToArray();
    
                    var result = "[" + string.Join(",", array) + "]";
    
                    ClientScript.RegisterStartupScript(this.GetType(), "highlightSelected", "highlightSelected(" + result + ");", true);
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 18, 2015 7:52 AM

All replies

  • User2103319870 posted

    now how to convert array to var

    You can directly assign the result to a var variable 

    var array = list.ToArray();

    Compete Code

    List<string> list = new List<string>();
    
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dr = dt.Rows[i];
    
                        //String selectedGID = dr["gid"].ToString();
    
                        //gid = selectedGID;
                        String columnAsString = dr["gid"].ToString();
                        list.Add(columnAsString);
    
                    }
    
                   var array = list.ToArray();

    Wednesday, March 18, 2015 6:43 AM
  • User-1965857832 posted

    Looking to join? Use String.Join, check this link for details

    https://msdn.microsoft.com/en-us/library/57a79xd0%28v=vs.110%29.aspx

    Wednesday, March 18, 2015 6:44 AM
  • User2103319870 posted

    string[] array = list.ToArray();

    now how to convert array to var

    You could also diretly assign the string array values to var variable

    List<string> list = new List<string>();
    
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dr = dt.Rows[i];
    
                        //String selectedGID = dr["gid"].ToString();
    
                        //gid = selectedGID;
                        String columnAsString = dr["gid"].ToString();
                        list.Add(columnAsString);
    
                    }
    
                    string[] array = list.ToArray();
                    //Convert the stringarray to var variable
                    var res = array;

    Wednesday, March 18, 2015 6:46 AM
  • User736276958 posted

    hi thanks for ur reply

    string[] array = list.ToArray();

    in the above line ,the data is displaying like a list

    as

    [0]  "1"

    [1]  "2"

    [2]  "3"

    [3]  "4"

    [4]  "5"

    [5]  "6"

    [6]  "7"

    [7]  "8"

    but i want this to display as [1,2,3,4,5,6,7,8]

    plz tell me the code

    Wednesday, March 18, 2015 7:05 AM
  • User2103319870 posted

    rishe28

    but i want this to display as [1,2,3,4,5,6,7,8]

    Do you just want to display this like an output to a label or something, if so then you might consider using the below code

    string[] array = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "6" };
    
                    var result = "[" + string.Join(",", array) + "]";

    Wednesday, March 18, 2015 7:10 AM
  • User736276958 posted

    no i want to pass that as a parameter into a function which is in js page

    Wednesday, March 18, 2015 7:12 AM
  • User2103319870 posted

    no i want to pass that as a parameter into a function which is in js page

    You can use the below code to get the value as per your requirement

    string[] array = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "6" };
    
                    var result = "[" + string.Join(",", array) + "]";

    result variable will have values in format which you need. Pass that value to your function.

    Wednesday, March 18, 2015 7:15 AM
  • User736276958 posted

    those values are not hardcoded

    values are coming from db

    how to pass these values into the function

    iam calling the function like this

    Response.Write(String.Format(@"<script>window.parent.highlightSelected('{0}');</script>",array));--------------i didnt understand how to pass the values here

    my function is

    function highlightSelected(array) {
    highlight.params.gid = array.join(',');-----------------------------here i have to use the values
    highlight.redraw();
    }

    Wednesday, March 18, 2015 7:17 AM
  • User2103319870 posted

    rishe28

    those values are not hardcoded

    values are coming from db

    This wont be a problem the code which i provided above will format in the correct format irrespecitive of how many values are available in array.

    rishe28

    how to pass these values into the function

    You can use Page.ClientScript.RegisterStartupScript  to call the Javascript function from code bedhind

    try with the below code

    string[] array = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "6" };
    
    var result = "[" + string.Join(",", array) + "]";
    
    ClientScript.RegisterStartupScript(this.GetType(), "highlightSelected", "highlightSelected('" + result + "');", true);

    Wednesday, March 18, 2015 7:23 AM
  • User736276958 posted
    string[] array = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "6" };

    then how to write this line without hardcoding the values
    Wednesday, March 18, 2015 7:28 AM
  • User2103319870 posted

    string[] array = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "6" };
    
    then how to write this line without hardcoding the values

    List<string> list = new List<string>();
    
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dr = dt.Rows[i];
    
                        //String selectedGID = dr["gid"].ToString();
    
                        //gid = selectedGID;
                        String columnAsString = dr["gid"].ToString();
                        list.Add(columnAsString);
    
                    }
    
                    string[] array = list.ToArray();
                   
                    var result = "[" + string.Join(",", array) + "]";
    
                    ClientScript.RegisterStartupScript(this.GetType(), "highlightSelected", "highlightSelected('" + result + "');", true);
                 

    Wednesday, March 18, 2015 7:41 AM
  • User736276958 posted

    i got it

    but i got another problem ,the parameters are passing like this "[1,2,3,4,5,6,]"

    but i want like this [1,2,3,4,5,6,] ie without double codes

    plz tell me this

    Wednesday, March 18, 2015 7:42 AM
  • User2103319870 posted

    i got it

    but i got another problem ,the parameters are passing like this "[1,2,3,4,5,6,]"

    but i want like this [1,2,3,4,5,6,] ie without double codes

    plz tell me this

    Try with the bellow code

    List<string> list = new List<string>();
    
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dr = dt.Rows[i];
    
                        //String selectedGID = dr["gid"].ToString();
    
                        //gid = selectedGID;
                        String columnAsString = dr["gid"].ToString();
                        list.Add(columnAsString);
    
                    }
    
                    string[] array = list.ToArray();
    
                    var result = "[" + string.Join(",", array) + "]";
    
                    ClientScript.RegisterStartupScript(this.GetType(), "highlightSelected", "highlightSelected(" + result + ");", true);
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 18, 2015 7:52 AM