Solved

How do I pass strings between voids ?

Posted on 2009-03-31
4
186 Views
Last Modified: 2013-12-17
I have a hard time understanding the whole passing strings thing in C#.
I thought that simply by putting example:
public string IMAGELOC;
at the begining of code made it Public to everything everywhere in that .cs file.
But that seems not to be the case.
I have tried to read some passing strings tutorials but I still have hard time understanding them.

Anyone care to show me how to get my IMAGELOC string from 1 place to another and maybe nlighten me a simple explanation ?

Thanks !

public void ListBox1_MouseDoubleClick(object sender, MouseEventArgs e)

        {

              string IMAGELOC = "mypath to file";

        }
 

public void pictureBox2_Click(object sender, EventArgs e)

        {

            Form2 newForm2 = new Form2();

            newForm2.ShowDialog();

        }

public class Form2 : Form

        {

            private PictureBox picbox2 = new PictureBox();
 
 

            public Form2()

            {

                MessageBox.Show();

                AutoSize = true;

                AutoSizeMode = AutoSizeMode.GrowAndShrink;

                picbox2.SizeMode = PictureBoxSizeMode.AutoSize;

                picbox2.ImageLocation = IMAGELOC

                this.Controls.Add(picbox2);

            }
 

        }

Open in new window

0
Comment
Question by:Smyken
  • 2
  • 2
4 Comments
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 24035358
I thought that simply by putting example:
public string IMAGELOC;
at the begining of code made it Public to everything everywhere in that .cs file.
Actually, making it public makes it available to anything that uses the class, as well as available to anything in the class.  If you just want it available within the class, use private.

It looks like you're wanting to pass the string between classes ... this should do it for you:

private String IMAGELOC;
 

public void ListBox1_MouseDoubleClick(object sender, MouseEventArgs e)

        {

              IMAGELOC = "mypath to file";

        }

 

public void pictureBox2_Click(object sender, EventArgs e)

        {

            Form2 newForm2 = new Form2();

            newForm2.SetImageLoc(IMAGELOC);

            newForm2.ShowDialog();

        }

public class Form2 : Form

        {

            private PictureBox picbox2 = new PictureBox();

 

 

            public Form2()

            {

                MessageBox.Show();

                AutoSize = true;

                AutoSizeMode = AutoSizeMode.GrowAndShrink;

                picbox2.SizeMode = PictureBoxSizeMode.AutoSize;

                

                this.Controls.Add(picbox2);

            }
 

            public SetImageLoc(String loc){

               picbox2.ImageLocation = loc;

            } 

                 

        }

Open in new window

0
 
LVL 1

Author Comment

by:Smyken
ID: 24035414
hmm

tried your solution and got this:

Error      1      Method must have a return type

pointing at :

public SetImageLoc(String loc){
               picbox2.ImageLocation = loc;
            }
0
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 300 total points
ID: 24035451
sorry, omitted the void return type.

public void SetImageLoc(String loc){

               picbox2.ImageLocation = loc;

            }

Open in new window

0
 
LVL 1

Author Comment

by:Smyken
ID: 24035583
Worked like a charm.

Thanks friend !
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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

12 Experts available now in Live!

Get 1:1 Help Now