Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a problem with changing a variable from different class. Even tho I used access modifiers (get and set), compiler still sees it as read only, and I cannot change it within another class.
private float speed;
public float Speed
{
get {return speed;}
set {speed = value;}
}
I'm not sure if this matters, but this variable is from my main, abstract class and I use it in other classes. What is happening here, is that I assigned value to this variable in one class (Player.cs) and I'm trying to change it in another class by initializing its object (Physics.cs). They are both inheriting from that main, abstract class.
If you're only setting speed once, just use a readonly variable and set it in the constructor:
public class Physics
{
private readonly float speed;
public Physics()
{
this.speed = 5;
}
}
You can set a readonly variable exactly once.
If you really do need to change speed within the lifetime of your object, just use an auto property anyways for simplicity:
public float Speed {get; set;}
If you're trying to access speed from a child class, make sure that its defined as protected (not private) in the base class. private really is private. Nothing outside of the class where it is defined can access it. Protected allows classes that inherit to access it.
(If I understood your question correctly, that is. I'm confused, though... if you are trying to access the speed variable directly, then why have the public properties?)
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I tried to search for this question on the website however could not find an answer. I checked the MSDN document about auto-implemented properties and the examples provided are only public fields in classes. My example class:
public class Area
{
private bool enabled { get; set; }
}
Whenever I try to access the enabled field of this class, from another class, I get an error, "Area.enabled is inaccessible due to its protection level". I don't understand why it is not accessible as auto-implemented properties are written and even though my field is private, I have get and set?
What you have here is a private automatically implemented property (not a field).
Essentially it is similar to:
private bool _enabled; // the field
private bool Enabled // the property declaration
{
get { return _enabled; } // the property getter
set { _enabled = value; } // the property setter
}
(except the field name is not _enabled, but: something unpronounceable)
The field in an automatically implemented property is always private; here the property is also private. Having private properties is perfectly common. So: indeed, it will be inaccessible outside of Area.
If that wasn't intended: use a more permissive accessibility; protected, internal, public etc.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am developing an ASP/c# webform where I am using JQuery as well. I came into a scenario where I need to call. C# function from JQuery. In order to that, I found that function in c# has to be a static method (web method).
The problem is that I need to access all variables, arrays, etc which I used to populat some data and these are not stated c variables. Also, from the web method I need to re-use some the functions which are not static. I ended up gradually just changing all methods and variables to static.
I would like to know if the approach I am taking is correct, and whether there is any pitfall of using static variables/methods and what in simple words makes a difference between static/none-static.
Static variables can be called directly by using class names such as
public class IhaveStatic
{
public static string Hello = "Hello I am A";
}
When you use static this means this will be in memory for life time of your process.
now consider another class as
public class IhaveNoStatic
{
public string Hello = "Hello I am B"
}
public class C
{
Console.WriteLine(IhaveStatic.Hello); // Correct
IhaveNoStatic obj = new IhaveNoStatic();
Console.WriteLine(obj); // Correct
Console.WriteLine(IhaveNoStatic.Hello); // Compile time error
}
as you can see that you need to create object of that class "IhaveNoStatic" to access non-static variable. So, this will be in memory until there is an instance of that class exist.
So, basically it's on your requirement but it is good to use less static variable in your programs.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
using System;
namespace GridSim
{
class Program
{
static void Main()
{
//error is right before declaring public. "} expected"
public int h = 1;
CreateWorld.Create();
}
}
}
I have a another class to create a grid in a nested for loop. the class is public. declaring a public variable ANYWHERE IN MY PROJECT yields the same result.
"} expected" before the public variable.
"The name "(insert thing here)" does not exist in this context" for literally everything after the public variable.
I want to be able to set a part of a grid to a character from anywhere in my project, but this error is preventing me from doing that.
Inside a method, variables do not have access modifiers. Remove the public keyword.
If this variable is supposed to be a field, move it out of the method.
declaring a public variable ANYWHERE IN MY PROJECT yields the same result.
That's simply not true. You can declare variables inside methods, and those declarations cannot contain access modifiers. If you meant to declare this as a field, then that declaration can only exist at the class level, outside any methods.
You probably meant this;
using System;
namespace GridSim
{
class Program
{
static void Main()
{
int h = 1;
CreateWorld.Create();
}
}
}
You might also have meant this:
using System;
namespace GridSim
{
class Program
{
private int _h = 1;
public int H{
get{ return _h; }
set{ _h = value; }
}
static void Main()
{
CreateWorld.Create();
}
}
}
Coding standards vary but generally private variables that are class wide (declared in a class scope, not declared in a method scope) are named starting with an underscore. Public variables and properties generally start with a capital letter. We also tend not to use public variables these days; the preference being for properties. Indeed that form up there is rather archaic, declaring a private member and a get and set that access it; the shorthand public int H{ get; set; } is more common, with the rest of the code to make it work being filled in behind the scenes
Lastly, you have to remember that the errors you see are just the computer's best interpretation of parsing what you wrote. If you wrote something it didn't expect, then it can cause errors everywhere else; this isn't necessarily because there are errors everywhere else, simply that the first thing you wrote that was incorrect caused the compiler to treat the rest of the file incorrectly. Putting one too many } in a particular place, for example, can mean that all your code that was inside a method is suddenly inside a class instead, which will generate syntax errors all over the place
Expect these things while you're learning; it goes away as you acquire more experience and learn how the language is laid out
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am confused on how to create class can you please help me??
class AutomotiveManager
{
private bool isBeingTested
{
set
{
isBeingTested= false;
}
}
private dialogResult MessageBoxResult
{
///need help over here
}
}
It seems that there are a few different issues here.
The difference between properties and attributes
The meaning of the = in the attributes
The error (which error?) you get for the declaration of the messageBoxResult
Private Attributes are just private Attributes.
So the implementation of a private attribute like the isBeingTested would be
private bool isBeingTested;
The = in the attributes means the default value. So that would be the value you assign in the constructor or at the declaration of the attribute. For the isBeingTested you could add that to the declaration like this:
private bool isBeingTested = false;
But since in C# the default value for a boolean is false anyway you don't even need to specify it. The way you programmed it you will en up in an infinite loop the moment you try to use the setter for isBeingTested
The «Property» presumably indicates that you are meant to create a Property rather then an Attribute, and I guess the {get;} indicates that you only need to implement a getter, not a setter for this property. (I'm guessing here since none of that is defined in UML)
The error you are getting is probably because the compiler doesn't know the type dialogResult. It may know the type DialogResult (notice the uppercase) if you add System.Windows.Forms as a reference to your project, and add a using statement like this
using System.Windows.Forms;
PS. You also failed to implement the static keyword on the class.
To convert your UML class diagram into a C# class, you'll need to understand what each of the symbols represent in your UML class diagram. This image from tutorialspoint.com is useful:
From there, it's a matter of writing the equivalent C# code based on the symbols provided in your example.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a problem with a example of my book. From what i have read non static methods cant be used without instance a object of the class. So is this ok ?
public partial class TempAgencyForm : Form
{
public TempAgencyForm()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
...
setVisibility(false);
}
private void setVisibility(bool visibilityValue)
{
...
}
}
Yes, it is fine. One non-static method can call another non-static method.
The call:
setVisibility(false);
can also be written:
this.setVisibility(false);
but the this qualifier is redundant.
However, if you had tried to call a non-static method without instance qualification from inside a static member, that would have been a problem (compile-time error).
I assume you are talking about calling setVisibility(false);. Yes it is fine, neither it or the method calling it are static.
This will all happen within an instance of TempAgencyForm
Yes this is okay, because it's called from within another member.
You're correct, since setVisibility() is not static, it always has to be called in the context of some object of the parent class (TempAgencyForm in this example).
However, btnCalculate_Click() is another member of TempAgencyForm, as such you're able to access the current/local object using the this keyword (this.setVisibility()), which is optional if there's no disambiguity.
static or non static doesnt matter here, you are calling a member function declared within the same object. so short answer is "fine"
how you call TempAgencyForm members may be what you are referring to
in this case (as you have defined) instantiations is required
TempAgencyForm taf = new TempAgencyForm()
taf.setVisibility(false);
however if you have your class definition itself as static, i.e.
public static partial class TempAgencyForm
then,
TempAgencyForm.setVisibility(false);
is suffice (without instantiating the object) as the object is already loaded on stack at the time of application start