Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I pass strings between voids ?

Posted on 2009-03-31
4
Medium Priority
?
204 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
[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
  • 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 1200 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

610 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