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
542 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

829 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