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
546 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

717 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