PROBLEM in public class

J

Jassim Rahma

Hi,

I have a problem with the public class. I am trying to save the last
identifier which I am getting correctrly but when I try to read I am
getting 0. The public class is like this:


public class global_var

{
private int record_id;

public int int_record_id

{
set {record_id = value;}
get {return record_id;}
}

}




and the code to read is:


global_var GlobalVariables = new global_var();

MessageBox.Show(GlobalVariables.int_record_id.AsString());



to set the value I am using:

GlobalVariables.int_record_id = some_int_figure_here;




many Thanks,
Jassim Rahma
 
J

Joanna Carter \(TeamB\)

I have a problem with the public class. I am trying to save the last
identifier which I am getting correctrly but when I try to read I am
getting 0. The public class is like this:

public class global_var

{
private int record_id;

public int int_record_id

{
set {record_id = value;}
get {return record_id;}
}

}

and the code to read is:

global_var GlobalVariables = new global_var();

MessageBox.Show(GlobalVariables.int_record_id.AsString());

to set the value I am using:

GlobalVariables.int_record_id = some_int_figure_here;

My guess would be that the instance you create is being garbage collected
and/or you are creating a new instance more than once.

If you want such a concept as a "global var" then you should use static
properties rather than instance ones.

e.g.

public class GlobalVariables
{
private static int recordId;

public static int RecordId

{
get {return recordId;}
set {recordId = value;}
}
}

Then you don't need to call the constructor, just address the class (static)
properties directly :

{
GlobalVariables.RecordId = 25;
...
MessageBox.Show(GlobalVariables.RecordId.AsString());
}

Joanna
 
J

Jassim Rahma

Hi Joanna,

I tried this code:
public class global_var
{
private static int record_id;
public static int int_record_id
{
set {record_id = value;}
get {return record_id;}
}
}


but getting this error:


'global_var.record_id' is inaccessible due to its protection level




Many Thanks,
Jassim Rahma
 
J

Joanna Carter \(TeamB\)

'global_var.record_id' is inaccessible due to its protection level

That's because you are trying to acces the private field, not the public
property.

I used the MS recommended syntax for property and field names, your
non-standard convention has caused you to misread my code example :)

Joanna

--
Joanna Carter (TeamB)

Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
 
J

Jon Skeet [C# MVP]

Jassim Rahma said:
Hi Joanna,

I tried this code:
public class global_var
{
private static int record_id;
public static int int_record_id
{
set {record_id = value;}
get {return record_id;}
}
}


but getting this error:


'global_var.record_id' is inaccessible due to its protection level

Yes, you should be using the property (which is public) rather than the
variable (which is private).
 
J

Jassim Rahma

yes it's working now..


i just wanted to say than you very much.



Many Thanks,
Jassim Rahma
 

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