Jump to content

n00b Object Scope question

- - - - -

  • Please log in to reply
5 replies to this topic

#1
belch85

belch85

    Newbie

  • Members
  • Pip
  • 4 posts
Hi All,

I am fairly new to C# although have moderate experience in vb.NET. Trying to figure out why I can't access my object "insim" outside of the method it is called/declared in. Any comments are helpful.

STARTED WITH:


namespace Spark.Example1
{
/// <summary>
/// Example 1: Hello, InSim! Connects to InSim and sends the message 'Hello, InSim!' to the game
/// chat.
/// </summary>
class Program
{
static void Main(string[] args)
{
// Create InSim object.
using (var insim = new InSim())
{
// Connect to LFS.
insim.Connect("192.168.0.3", 29999);

// Initialize InSim.
insim.Send(new IS_ISI { IName = "^3Example", Admin = "abc123"});//string.Empty });

// Send message 'Hello, InSim!' to InSim.
insim.Send("Hello, InSim!");

// Stop program from exiting.
insim.Run();
}
}
}
}



Now trying to separate the connection commands and the message sending command (hello, insim) into two different methods. I will be adding more and more so I want to have one method to create / connection the insim, and then work with it elsewhere throughout my project.

After hours of googling & playing I have come up with a complicated:



namespace Spark.B85
{
public partial class Form_Main : Form
{
public Form_Main()
{
InitializeComponent();
}
public void InsimConnect()
{
//var insim = new Spark.InSim() ; /////TRYING TO FIND A WAY TO MAKE IT PUBLIC WITH THIS NEW LINE????
//insimconnection LFS = new insimconnection();


// Create InSim object.
using (var insim = new Spark.InSim())
{
// Connect to LFS.
LFS.insim.Connect(Text_IP.Text, System.Convert.ToInt32(Text_Port.Text));
// Initialize InSim.
LFS.insim.Send(new Spark.Packets.IS_ISI { IName = "^3Example", Admin = Text_Password.Text });//string.Empty });
}
}
private void Button_Connect_Click(object sender, EventArgs e)
{
// Update Status Light & Word
Label_Colour.BackColor = Color.Blue;
Label_Status.Text = "Connecting...";

InsimConnect();

// Update Status Light & Word
Label_Colour.BackColor = Color.Green;
Label_Status.Text = "Connected!";

// Send message 'Hello, InSim!' to InSim.
//insimconnection LFS = new insimconnection();
LFS.insim.Send("Hello, InSim!");





// Stop program from exiting.
LFS.insim.Run();

}
private void Button_Send_Click(object sender, EventArgs e)
{
insimconnection LFS = new insimconnection();
LFS.insim.Send("Hello, InSim!");

}
}
public class insimconnection
{
public Spark.InSim insim;
}

}



Sadly no matter what different combinations I have tried public blah blah - I can only seem to access the insim object from the "public void InsimConnect()" method.

Please assist? :)
Kind Regards,
Belch85

#2
belch85

belch85

    Newbie

  • Members
  • Pip
  • 4 posts
PS - Here is a printscreen of the "cleanest" code that would be best if a single line could be added somewhere to make it happen :)
Posted Image

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
I may be mistaken, but can you not return the insim object in your InsimConnect() function, and then call it like this in Button_Connect_Click()?
insim = InsimConnect();
In InsimConnect() you may need to change returning void to an object refernce so you can return a reference to the insim object out of scope -- I had not worked with C# before, but I know it must have similar ways to C/C++.

Edited by Alexander, 23 November 2010 - 05:39 AM.

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
belch85

belch85

    Newbie

  • Members
  • Pip
  • 4 posts
Sorry, I'm having a bit of trouble following your english.... Change it from insim from void?

Could you possibly provide an example (even if its rough english) i'll try and work with it?

Regards,
Belch85

#5
belch85

belch85

    Newbie

  • Members
  • Pip
  • 4 posts
Here is what I am currently up to. Am I on the right track?

Posted Image

#6
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
I wouldn't have declared it as static unless you want there to only be one _insim variable shared across all forms. The problem you are having now is that you never give _insim a value, you just declare something to hold an _insim object. Possibly change the using statement to
using (_insim = new Spark.InSim()) { ...





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users