M.N. Ahmed Sahib

My Declaration

string[][] MyJagAry= new string[115][];

MyJagAry [1] = new string[20];

MyJagAry [2] = new string[40];

-

-

-

-

-

MyJagAry [114] = new string[60];

MyJagAry [1][5] = "Windows Vista Business is designed to help keep PCs running smoothly and securely so you are less reliant on dedicated IT support. ";

MyJagAry [2][10] = "Windows Vista Enterprise is the premium edition of Windows Vista, with all the features in Windows Vista Business plus extra capabilities that can help lower IT costs and improve data protection.";

-

-

-

-

MyJagAry [114][15] = "Windows Vista Ultimate is the most comprehensive edition of Windows Vista.";

My target:

I have a jagged array of string as mentioned as above. My aim is to search the word "Vista", whether it is exists or not. If the word vista found on any of the array element, then I have to display the entire contents of the particular elements on a rich text box respectively.

Can anyone help me

Regards

Ahmed Sahib




Re: Visual C# General Search a String (Jagged Array)

James Curran

There no "simple" way of doing it beyond brute force:



foreach
(string[] array in MyJagAry)
{
foreach(string str in array)
{
if (str.Contains("Vista"))
Console.WriteLine(str);
}
}






Re: Visual C# General Search a String (Jagged Array)

RizwanSharp

Ofcourse what james has suggested you need to iterate through each element and check whether it containes the required search string or not. If yes then display it. Code provided by James :D.

Best Regards,

Rizwan aka RizwanSharp






Re: Visual C# General Search a String (Jagged Array)

M.N. Ahmed Sahib

Mr.James

Thanks for your reply.

I checked your code, but NullReferenceException was handled while processing ' foreach (String Str in array)'. ie Object reference not set to an instance of an object (on in operator). Is there any idea

Regards

Ahmed Sahib






Re: Visual C# General Search a String (Jagged Array)

Matthew Watson

Add a check that array is not null:


foreach(string[] array in MyJagAry)
{
if (array != null)
{
foreach(string str in array)
{
if (str.Contains("Vista"))
Console.WriteLine(str);
}
}
}








Re: Visual C# General Search a String (Jagged Array)

James Curran

string[][] MyJagAry= new string[115][];

MyJagAry [1] = new string[20];

-

MyJagAry [1][5] = "Windows Vista Business is designed to help keep PCs running smoothly and securely so you are less reliant on dedicated IT support. ";

Array start with [0]. If you don't assign anything to MyJAgAry[0] (and MyJagAry[ i ][0]) you'll get the NullReferenceException.






Re: Visual C# General Search a String (Jagged Array)

M.N. Ahmed Sahib

Mr. James

Thanks for your useful reply. It is true that array starts with index 0. I don't like to initialize anything with 0 index, Infact I need only 114 indices. To omit Zeroth entry only I initialized MyJagAry as115 (ie I am using 1 to114 indices). So How could we manage

on string str in array, while processing, system search for new keyword. Is it neccessary to iniliaze 'array' with new keyword

Regards

Ahmed Sahib






Re: Visual C# General Search a String (Jagged Array)

M.N. Ahmed Sahib

Mr.James

I solved this. Its all about assigning values (and initializing) Zero th entry of the array. Thanks for your reply

Regards

Ahmed Sahib