Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > .NET 1.0 and Visual Studio.NET > VS.NET 2002/2003
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
VS.NET 2002/2003 Discussions about the Visual Studio.NET programming environment, the 2002 (1.0) and 2003 (1.1). ** Please don't post code questions here ** For issues specific to a particular language in .NET, please see the other forum categories.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VS.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 12th, 2003, 03:07 PM
Registered User
 
Join Date: Jun 2003
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default String array declaration

Hi,

I am new to C# programming language. I am developing small
project that is viewing digital photos as Slideshow.

I am getting syntax error that is "Syntax
error, ']'expected" while compiling the following code
snippet,

string[] path = new string[3];
path[0] = "c:\\digitalphotos\\Image1.jpg";
path[1] = "c:\\digitalphotos\\Image2.jpg";
path[2] = "c:\\digitalphotos\\Image3.jpg";

Can anyone tell me why i am getting such error?

Thanks inadvance.
Reply With Quote
  #2 (permalink)  
Old June 12th, 2003, 03:45 PM
Authorized User
 
Join Date: Jun 2003
Location: Gainesville, FL, USA.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello mkpriya.

I tried your code in VS.NET and got no error.
Where exctly is the error pointing to?

I copied and pasted the lines you posted in a simple console application: here's the full code I worked with:

Code:
using System;

namespace TestStringArray
{
  /// <summary>
  /// Summary description for Class1.
  /// </summary>
  class Class1
  {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
      string[] path = new string[3];
      path[0] = "c:\\digitalphotos\\Image1.jpg";
      path[1] = "c:\\digitalphotos\\Image2.jpg";
      path[2] = "c:\\digitalphotos\\Image3.jpg";
    }
  }
}
Maybe the ']' is missing in a line right before, or right after the lines you posted?

Hope this helps.
Reply With Quote
  #3 (permalink)  
Old June 12th, 2003, 05:19 PM
Registered User
 
Join Date: Jun 2003
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Olorin,

Thanks for your response.

The code snippet is working for console application but not for windows application in VS.net. Could you please try for windows application and suggest me the solution for this error.

Thanks.
Reply With Quote
  #4 (permalink)  
Old June 12th, 2003, 07:32 PM
Authorized User
 
Join Date: Jun 2003
Location: Gainesville, FL, USA.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

mkpriya,
I've opened a new Win Application solution, and plugged those lines in the default constructor:

Code:
    public Form1()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      string[] path = new string[3];
      path[0] = "c:\\digitalphotos\\Image1.jpg";
      path[1] = "c:\\digitalphotos\\Image2.jpg";
      path[2] = "c:\\digitalphotos\\Image3.jpg";
    }
It compiled just fine...
I must admit I'm semi-buffled...
When you try to compile, and get the error, double-clicking on it should bring you to the exact location where the ']' is expected. Which line, and which character get highlighted when you do that?

For instance, I removed the ']' from the assignment to path[0], so that the line was now:
path[0 = "c:\\digitalphotos\\Image1.jpg";
When I tried to compile, I received the error you mentioned, and the cursor was placed at the end of that line.

Let me know if this helps, or if you have a larger sniplet I could try for you.
Reply With Quote
  #5 (permalink)  
Old June 12th, 2003, 07:33 PM
Authorized User
 
Join Date: Jun 2003
Location: Gainesville, FL, USA.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

PS: Also, do you get any other error message when compiling this, or is it the only error you get?
Reply With Quote
  #6 (permalink)  
Old June 12th, 2003, 09:08 PM
Registered User
 
Join Date: Jun 2003
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Olorin,

The following is my code,

The index gets highlighted (path[0], # 0) when i double click the error line.

and also i want to know how do i add array to the controls
for eg., to the PictureBox class.


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace String
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Button
btn_ShowImage;
        private System.Windows.Forms.PictureBox
pictureBox_EnlargeImage;
        private System.Windows.Forms.PictureBox
pictureBox_Image;
        private System.Windows.Forms.Panel
pnl_Images;
                private
System.ComponentModel.IContainer components;

        public Form1()
        {
            //
            // Required for Windows Form
Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code
after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool
disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose
();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated
code
        /// <summary>
        /// Required method for Designer support -
do not modify
        /// the contents of this method with the
code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btn_ShowImage = new
System.Windows.Forms.Button();
            this.pictureBox_EnlargeImage = new
System.Windows.Forms.PictureBox();
            this.pictureBox_Image = new
System.Windows.Forms.PictureBox();
            this.pnl_Images = new
System.Windows.Forms.Panel();
            this.pnl_Images.SuspendLayout();
            this.SuspendLayout();
            //
            // btn_ShowImage
            //
            this.btn_ShowImage.Location = new
System.Drawing.Point(16, 16);
            this.btn_ShowImage.Name
= "btn_ShowImage";
            this.btn_ShowImage.TabIndex = 1;
            this.btn_ShowImage.Text = "Show
Image";
            this.btn_ShowImage.Click += new
System.EventHandler(this.btn_ShowImage_Click);
            //
            // pictureBox_EnlargeImage
            //

    this.pictureBox_EnlargeImage.Location = new
System.Drawing.Point(160, 72);
            this.pictureBox_EnlargeImage.Name
= "pictureBox_EnlargeImage";
            this.pictureBox_EnlargeImage.Size
= new System.Drawing.Size(464, 312);

    this.pictureBox_EnlargeImage.SizeMode =
System.Windows.Forms.PictureBoxSizeMode.StretchIma ge;

    this.pictureBox_EnlargeImage.TabIndex = 3;

    this.pictureBox_EnlargeImage.TabStop = false;
            //
            // pictureBox_Image
            //
            this.pictureBox_Image.BorderStyle
= System.Windows.Forms.BorderStyle.FixedSingle;
            this.pictureBox_Image.Location =
new System.Drawing.Point(16, 16);
            this.pictureBox_Image.Name
= "pictureBox_Image";
            this.pictureBox_Image.Size = new
System.Drawing.Size(72, 48);
            this.pictureBox_Image.SizeMode =
System.Windows.Forms.PictureBoxSizeMode.StretchIma ge;
            this.pictureBox_Image.TabIndex = 0;
            this.pictureBox_Image.TabStop =
false;
            this.pictureBox_Image.Visible =
false;
            this.pictureBox_Image.Click += new
System.EventHandler(this.pictureBox_Image_Click);
            //
            // pnl_Images
            //
            this.pnl_Images.AutoScroll = true;
            this.pnl_Images.Controls.AddRange
(new System.Windows.Forms.Control[] {



this.pictureBox_Image});
            this.pnl_Images.Location = new
System.Drawing.Point(144, 392);
            this.pnl_Images.Name
= "pnl_Images";
            this.pnl_Images.Size = new
System.Drawing.Size(480, 80);
            this.pnl_Images.TabIndex = 4;
            //
            // Form1
            //
            this.AutoScaleBaseSize = new
System.Drawing.Size(5, 13);
            this.ClientSize = new
System.Drawing.Size(664, 485);
            this.Controls.AddRange(new
System.Windows.Forms.Control[] {this.pnl_Images,
this.pictureBox_EnlargeImage, this.btn_ShowImage});
            this.Name = "Form1";
            this.Text = "Form1";
            this.pnl_Images.ResumeLayout
(false);
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the
application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        string[] path = new string[4];
        path[0] = "c:\\digitalphotos\\Image1";
                path[1] = "c:\\digitalphotos\\Image1";
            path[2] = "c:\\digitalphotos\\Image2";
                path[3] = "c:\\digitalphotos\\Image3";
        private void btn_ShowImage_Click(object
sender, System.EventArgs e)
        {
            for(int i=0; i<4; i++)
            {
                if(File.Exists(path[i]))
                {
                    Bitmap image = new
Bitmap(path[i]);

    this.pictureBox_Image.Visible = true;

    this.pictureBox_Image.Image = image; //(how do i
add array to picturebox class control?

                }
            }
        }
}
Reply With Quote
  #7 (permalink)  
Old June 12th, 2003, 10:15 PM
Authorized User
 
Join Date: Jun 2003
Location: Gainesville, FL, USA.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

mkpriya,

I tried your code, and the first error I receive is:
" } expected"
the editor brings me to the last line of the code, so we might have a missing '}' to take care of first.

In any case, it looks to me that your string[] is declared outside any method. Hence, it is a class field (I guess public?). While you can declare an array of strings as a class field, and while you can initialize simple data fields, I don't think you can initialize array elements like that.

I believe you want to move the lines initializing the array elements in the class constructor (or maybe in a new static constructor?).

So you would have something like this:

Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace String
{
public class Form1 : System.Windows.Forms.Form
{
  private string[] path = new string[4];

public Form1()
{
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent();

  path[0] = "c:\\digitalphotos\\Image1";
  path[1] = "c:\\digitalphotos\\Image1";
  path[2] = "c:\\digitalphotos\\Image2";
  path[3] = "c:\\digitalphotos\\Image3"; 
}
//End Constructor

}
//End class

}
//End namespace
*** BY THE WAY: I'd suggest changing the name of the namespace to something less prone to trouble :-) ***

The code above defines the array as a private field of the object (and instanciates it too!). Then, the constructor fills in the elements.

In regard to your other wuestion, on how to create an array of PictureBoxes, I did something like that not long ago...
Basically you define an array of PictureBoxes as a class field, and then instanciate each element (and its characteristics) in the class constructor. You also need Bitmaps to stick in the boxes.
For instance:

Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace Sample
{
public class Form1 : System.Windows.Forms.Form
{
  private static int NUM_BOXES = 4;

  private System.Windows.Forms.PictureBox[] pboxes =
    new System.Windows.Forms.PictureBox[NUM_BOXES];

  private string[] sources = new string[4];

  private Bitmap[] bmaps = new Bitmap[NUM_BOXES];

public Form1()
{
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent();

  //Set the sources:
  sources[0] = "c:\\digitalphotos\\Image1";
  sources[1] = "c:\\digitalphotos\\Image1";
  sources[2] = "c:\\digitalphotos\\Image2";
  sources[3] = "c:\\digitalphotos\\Image3";

  //Set the bitmaps:
  for(int i=0; i<NUM_BOXES; i++)
    bmap[i] = new Bitmap[sources[i];

  //Set the picture boxes:
  //We also add them to the form, one by one.
  //(we could use the AddRange to do the same at the end)
  this.SuspendLayout();

  for(int i=0; i<NUM_BOXES; i++)
  {
    this.pboxes[i] = new System.Windows.Forms.PictureBox();
    //Set the image displayed:
    pboxes[i].Image = bmaps[i];

    //Set any of the other attributes you want, e.g.:

this.pboxes[i].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;

this.pboxes[i].Location = new System.Drawing.Point(16, 16);
this.pboxes[i].Name = "Image_"+(i.ToString());
this.pboxes[i].Size = new System.Drawing.Size(72, 48);
this.pboxes[i].SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pboxes[i].TabIndex = i;
this.pboxes[i].TabStop = false;
if(i==0)  this.pboxes[i].Visible = true;  //Set the first one to
else      this.pboxes[i].Visible = false; //visible!
this.pboxes[i].Click += new System.EventHandler(this.picBox_Click);
this.Controls.Add(pboxes[i]);
  }//FOR
  this.ResumeLayout(false);  
}
//End Constructor

}
//End class

}
//End namespace

I think that should do it, or get you closer :-)
Hope you'll forgive me if I don't test it right now...

Good Luck! And let me know if that doesn't help at all!
Reply With Quote
  #8 (permalink)  
Old June 13th, 2003, 02:26 PM
Registered User
 
Join Date: Jun 2003
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Olorin,

Thank you very much for your patience and the time you have taken to explain me in detail. Lemme correct my code as you have suggested and i will update the message if i have any questions.
Reply With Quote
  #9 (permalink)  
Old June 13th, 2003, 07:21 PM
Authorized User
 
Join Date: Jun 2003
Location: Gainesville, FL, USA.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No problem at all :-) Hope it all works out.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convering a String Array to an Integer array nkrust C# 9 November 17th, 2010 12:02 PM
Dynamic declaration of an array tenkasian C# 2 September 5th, 2007 10:14 PM
String to array ajit Javascript 5 January 20th, 2007 04:02 PM
Casting String array to string Samatha ASP.NET 1.0 and 1.1 Professional 1 December 5th, 2006 07:46 AM
String to Array Psion PHP How-To 1 September 5th, 2006 11:36 AM



All times are GMT -4. The time now is 06:40 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.