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
537 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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.

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now