hi,
i am a newbie in C#. I am having a variable named stone inside a private function named rock. a variable stone is defined in a structure called sand and is accessed only as sand.stone inside the function rock. Now, I must access this variable called stone in another function...how can it be done...please help me guys.

Recommended Answers

All 10 Replies

Declare that sand variable as a field.

class Sample{
     Sand sand;
     void method1() { }
     .....
 }

cant get you...can you explain it a little more???

class Sample{
     Sand sand=new Sand();
     void method1() { sand.stone ...... }
     void method2() { sand.stone ...... }
     .....
 }

thanks. now i am able to access the variable inside every function. btw how can the variable stone inside the private function rock be accessed in another function hill...

here s the code:

private void ExtractAnchor(string[] line, List<Anchor> SLTPAnchors, int i)
{
Anchor a;
 a.Version = line[i + 2].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[1];
}

I want to access this a.Version inside another function called

private void fetchButton_Click(object sender, EventArgs e)

and anchor is the structure name defined as

struct Anchor
    {
       public string Version;
    }
Anchor a;
 private void ExtractAnchor(string[] line, List<Anchor> SLTPAnchors, int i)
{

 a.Version = line[i + 2].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[1];
}

thanks

how can the variable named a.Version be returned from the function private void ExtractAnchor() and used in private void fetchButton_Click()???

private void fetchButton_Click(object sender, EventArgs e)
{
  MessageBox.Show(a.Version);
}

thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.