Solved

c# passing a multi dimension array to a function ***  Cannot apply indexing with [] to an expression of type 'System.Array'

Posted on 2011-09-14
6
544 Views
Last Modified: 2013-12-17
I am passing a multi dim array to a function and then operating on a string that is also passed to the function, to see if the value in the array is found in the passed to string. I cycle through the array and see if the data is found.
I have got this going with the array dimed in the called function, but when i moved the array to the  calling function and then try to pass it to the function then look for the string it throws up an error

Cannot apply indexing with [] to an expression of type 'System.Array'

the code below is the code that i am using, all i need to do is to pass the array, and the string then cycle through the array 1 to 13 and see if the string in the array eg   szTempvals[0,0] is found in the string that was passed.

but the line  if (szTemp.IndexOf(szTempvals[myInt, 0]) != -1) throws up the error

any help would be appreciated

********************************************************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void rundata_Click(object sender, EventArgs e)
        {
            string[,] szTempvals = new string[14, 3];
            szTempvals[0, 0] = "Type of client:";
            szTempvals[0, 1] = "16";
            szTempvals[1, 0] = "Title:";
            szTempvals[1, 1] = "7";
            szTempvals[2, 0] = "Name:";
            szTempvals[2, 1] = "6";
            szTempvals[3, 0] = "Position:";
            szTempvals[3, 1] = "10";
            szTempvals[4, 0] = "Phone:";
            szTempvals[4, 1] = "7";
            szTempvals[5, 0] = "Website:";
            szTempvals[5, 1] = "9";
            szTempvals[6, 0] = "Int search for:";
            szTempvals[6, 1] = "16";
            szTempvals[7, 0] = "Email:";
            szTempvals[7, 1] = "7";
            szTempvals[8, 0] = "Phone:";
            szTempvals[8, 1] = "7";
            szTempvals[9, 0] = "Int search for:";
            szTempvals[9, 1] = "16";
            szTempvals[10, 0] = "any questions:";
            szTempvals[10, 1] = "16";
            szTempvals[11, 0] = "provide something else:";
            szTempvals[11, 1] = "25";
            szTempvals[12, 0] = "Do you examine:";
            szTempvals[12, 1] = "16";
            string szTemp = "Test data send";
            findStrMatch(szTemp, szTempvals);


        }
        private void findStrMatch(string szTemp, Array szTempvals)
        {
            int myInt = 0;
           
            while (myInt < 13)
            {
                // szTempvals[myInt, 0];
                // MessageBox.Show(szTemp);

                if (szTemp.IndexOf(szTempvals[myInt, 0]) != -1)
                {

                }
            }

        }
    }
}
0
Comment
Question by:sydneyguy
6 Comments
 
LVL 6

Expert Comment

by:bartvd
ID: 36537752
Change this line:
private void findStrMatch(string szTemp, Array szTempvals)

into this link:

private void findStrMatch(string szTemp, string[,] szTempvals)
0
 
LVL 12

Expert Comment

by:viralypatel
ID: 36537759
you'll probably need to ItemAt() instead of specifying the index to the array.
0
 

Author Comment

by:sydneyguy
ID: 36537802
that works fine but how do i return the array to the calling function please have tried different syntax but throws up a

Cannot implicitly convert type 'System.Array' to 'string[*,*]'. An explicit conversion exists (are you missing a cast?)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void rundata_Click(object sender, EventArgs e)
        {
            string[,] szTempvals = new string[14, 3];
            szTempvals[0, 0] = "Type of client:";
            szTempvals[0, 1] = "16";
            szTempvals[1, 0] = "Title:";
            szTempvals[1, 1] = "7";
            szTempvals[2, 0] = "Name:";
            szTempvals[2, 1] = "6";
            szTempvals[3, 0] = "Position:";
            szTempvals[3, 1] = "10";
            szTempvals[4, 0] = "Phone:";
            szTempvals[4, 1] = "7";
            szTempvals[5, 0] = "Website:";
            szTempvals[5, 1] = "9";
            szTempvals[6, 0] = "Int search for:";
            szTempvals[6, 1] = "16";
            szTempvals[7, 0] = "Email:";
            szTempvals[7, 1] = "7";
            szTempvals[8, 0] = "Phone:";
            szTempvals[8, 1] = "7";
            szTempvals[9, 0] = "Int search for:";
            szTempvals[9, 1] = "16";
            szTempvals[10, 0] = "any questions:";
            szTempvals[10, 1] = "16";
            szTempvals[11, 0] = "provide something else:";
            szTempvals[11, 1] = "25";
            szTempvals[12, 0] = "Do you examine:";
            szTempvals[12, 1] = "16";
            string szTemp = "Test data send";
           szTempvals = findStrMatch(szTemp, szTempvals);


        }
       // private void findStrMatch(string szTemp, Array szTempvals)
        private Array findStrMatch(string szTemp, string[,] szTempvals)
        {
            int myInt = 0;
           
            while (myInt < 13)
            {
                // szTempvals[myInt, 0];
                // MessageBox.Show(szTemp);

                if (szTemp.IndexOf(szTempvals[myInt, 0]) != -1)
                {
                   
                }
                myInt++;
            }

            return szTempvals;

        }
    }
}
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 16

Accepted Solution

by:
SriVaddadi earned 250 total points
ID: 36538159
why do you want to use Array object as the function parameter instead use the string[,] as the function parameter as below.

private void findStrMatch(string szTemp, string[,] szTempvals)

This would solve both your indexing and returning issue
0
 
LVL 6

Assisted Solution

by:bartvd
bartvd earned 250 total points
ID: 36538876
You should replace this line:
private Array findStrMatch(string szTemp, string[,] szTempvals)

with this line:
private string[,] findStrMatch(string szTemp, string[,] szTempvals)
0
 

Author Closing Comment

by:sydneyguy
ID: 36562310
thanks for your answers result was a mixture of 2 answers thanks for your help
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question