struct reference in a method question

A

A. Burch

I'm having trouble understanding why I can't reference a struct in a method
If I have the following which works. I need to pass a reference to a method
in the namespace simple namespace. I thought where the >---right here is-->
would be the place to put a SimpleStrut ss definition that could be
referenced anywhere in the namespace. How do I define a reference to
"SimpleStrut ss and just use ss in Main with the definition above it.

Thanks.......


using System;
namespace simple
{
struct SimpleStruct
{
private int xval;
public int X
{
get
{
return xval;
}
set
{
if (value < 100)
xval = value;
}
}
public void DisplayX()
{
Console.WriteLine("The stored value is: {0}", xval);
}
}


class simple
{
//-----right here------> SimpleStruct ss;
[STAThread]

static void Main(string[] args)
{
SimpleStruct ss = new SimpleStruct();
ss.X = 5;
ss.DisplayX();
}
}
}
 
M

Miha Markic

Hi,

You should declare ss as static since you are accessing it from within
static method.
static SimpleStruct ss;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top