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
533 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

706 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

19 Experts available now in Live!

Get 1:1 Help Now