I wrote the code below and i want to access the private varibale in another class, i created instance of the class and tried to access it but couldn't. can someone point out what I did wrong in the code below?
using System;
namespace lab_first
{
public class AccessModifiers
{
private int Abc { get; set; }
private int bcd { get; set; }
}
class Program
{
static void Main(string[] args)
{
var acc = new AccessModifiers();
Console.WriteLine(acc.Abc)
}
}
}
You make members private so that nobody outside the class can access them.
This goes inline with the principle of information hiding.
Your example should look like this:
public class AccessModifiers
{
// You can only access this inside of the class AccessModifiers
private int Abc { get; set; }
internal void SetValue(int x){
// Access possible, because SetValue() is inside the same class
Abc = x;
}
internal int GetValue(){
// Access possible, because GetValue() is inside the same class
return Abc;
}
}
class Program
{
static void Main(string[] args)
{
var acc = new AccessModifiers();
// Abc is never modified directly, only indirectly.
acc.SetValue(5);
Console.WriteLine(acc.GetValue());
}
}
However, there is still a way to access the private member. It's called Reflection. However, note that private variables are considered an implementation detail and might change at any time, so you can't rely on it. E.g. someone might change the name from Abc to def and your Reflection-based approach fails.
You can either change private to internal or public in this case.
Another way is declaring the variables in the class as private and using C# Properties in the class to set and get the values of variables. this is called encapsulation which is a protective shield that prevents the data from being accessed by the code outside this shield).
public class AccessModifiers
{
private int _abc { get; set; }
private int _bcd { get; set; }
public int Abc
{
get
{
return _abc;
}
set
{
_abc = value;
}
}
public int Bcd
{
get
{
return _bcd;
}
set
{
_bcd = value;
}
}
}
Related
I understand how Auto-Implemented Properties work and how they are supposed to help. I was wondering if I could still use it somehow in a more advanced way.
Imagine I have this:
public int SomeProperty { get; set; }
Which is basically another way of writing the code below (but using Automatic Properties).
private int _someField;
public int SomeProperty
{
get { return _someField;}
set { _someField = value;}
}
What I want to do is write:
private int _someField;
public int SomeProperty
{
get { return _someField;}
set { FunctionA(); _someField = value;}
}
But using the advantages of the Auto-Implemented Properties. Is that possible?
I tried something like this:
public int SomeProperty { get; set{FunctionA();} }
But it doesn't work. Thank you everybody for the help, I know it's silly but I am curious about it.
No, it is not allowed. See the language spec:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/classes#properties
An automatically implemented property (or auto-property for short), is a non-abstract non-extern property with semicolon-only accessor bodies.
I didn't find free tool, but PostSharp handles this. It has trial period and some free-to-use options. Anyway take a look at method decoration and AOP frameworks.
using System;
using PostSharp.Aspects;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var someClass = new SomeClass();
Console.WriteLine($"{nameof(someClass.Value)} = {someClass.Value}");
someClass.Value = 42;
Console.WriteLine($"{nameof(someClass.Value)} = {someClass.Value}");
}
}
class SomeClass
{
public int Value { get; [Decorate] set; }
private void SomeFunction()
{
Console.WriteLine("SomeFunction called");
}
[Serializable, AttributeUsage(AttributeTargets.Method)]
public class DecorateAttribute : MethodInterceptionAspect
{
public override void OnInvoke(MethodInterceptionArgs args)
{
var target = (SomeClass)args.Instance;
target.SomeFunction();
args.Proceed(); // performs the method it applied to
}
}
}
}
Output:
Value = 0
SomeFunction called
Value = 42
Is it possible to to define a string from a variable where the string does NOT have quotations. Example:
public class aclass
{
public string athing;
}
public void example(string thing)
{
aclass thing = new aclass();
}
The string thing can't be put into aclass thing = new aclass(); normaly.
Is there anyway to do it?
You need a constructor
void Main()
{
CreateExampleObject("testing");
}
public class Example
{
// This is a constructor that requires a string as an argument
public Example(string text)
{
this.Text = text;
}
public string Text { get; set; }
}
public void CreateExampleObject(string text)
{
Example example = new Example(text);
Console.WriteLine(example.Text);
}
You can do it this using many way but generally standard way is using constructor
please refer this link for better understanding.
C# : assign data to properties via constructor vs. instantiating
You have to ways of setting fields/property value of an object.
First is to do it through the constructor, as mentioned in other answer.
Second can be implmeneted in various ways:
Expose public property making field privte:
public class aclass
{
private string _athing;
public string Athing
{
get { return _athing; }
set { _athing = value; }
}
}
public void example(string thing)
{
aclass aclass = new aclass();
aclass.Athing = thing;
}
Or even shorter, you could use property:
public class aclass
{
public string Athing {get; set; }
}
Using your implementation, you make your field public, so you can set it easily:
public void example(string thing)
{
aclass aclass = new aclass();
aclass.athing = thing;
}
But it doesn't comply with OOP encapsulation principle.
I've got some methods in one class that return values of to,from,message and im trying to use these in the other class that has the default display message.
I can't seem to use the string values that I get from the methods in class 1 in class 2.
I have tried declaring the string values public but got overloaded with errors none of which really said why the error was happening.
public class ChristmasCard
{
public static void Main()
{
string toWhom = To();
string fromWhom = From();
double decorate = ChooseDecoration();
string message = AddMessage();
DoPromt(message);
DisplayMessage(decorate);
Console.ReadLine();
}
public class ChristmasCardTesting : ChristmasCard
{
public static void SantaA()
{
Console.WriteLine(ChristmasCard.toWhom);
Console.WriteLine(ChristmasCard.Message1);
Console.WriteLine(ChristmasCard.Message2);
Console.WriteLine(ChristmasCard.Message3);
Console.WriteLine(ChristmasCard.fromWhom);
Console.ReadLine();
I guess inheritance is not what you want here. You want to use an instance of your object instead.
First you want to create an instance of your ChristmasCard.
That instance should hold Properties/Fields of the values you like to hold in the RAM.
Then you want to create an instance of your ChristmasCardTesting and call the "testmethod" while giving the christmas card as parameter.
And that code could be executed in your Program.
I guess what you want to achieve should more like the following:
public class Program {
public static void Main(string[] args) {
ChristmasCard card = new ChristmasCard();
ChristmasCardController controller = new ChristmasCardController();
controller.SomeMethod(card);
WriteToConsole(card);
}
public static void WriteToConsole(ChristmasCard card) {
Console.WriteLine(card.ToWhom);
Console.WriteLine(card.Message);
Console.WriteLine(card.FromWhom);
Console.ReadLine();
}
}
/// <summary>
/// Pure data class does not needs methods!
/// </summary>
public class ChristmasCard {
public string ToWhom { get; set; }
public string FromWhom { get; set; }
public double Decorate { get; set; }
public string Message { get; set; }
}
/// <summary>
/// Controller for the dataClass
/// </summary>
public class ChristmasCardController {
public void SomeMethod(ChristmasCard card) {
card.ToWhom = To();
card.FromWhom = From();
card.Decorate = ChooseDecoration();
card.Message = AddMessage();
DoPromt(card);
DisplayMessage(card.Decorate);
}
private void DisplayMessage(double cardDecorate) {
//Write your message to the console
}
private void DoPromt(ChristmasCard card) {
//Do some ConsoleRead in here
}
private string AddMessage() {
throw new NotImplementedException();
}
private double ChooseDecoration() {
throw new NotImplementedException();
}
private string From() {
throw new NotImplementedException();
}
private string To() {
throw new NotImplementedException();
}
}
Edit:
If that is totally not what you want, please explain what you are trying to achieve and i'm 100% sure i'll find the answer.
Edit2
All methods from that Controller class could also be in the Program class.
Note these would have to be static if you move these methods. I hope this snipped makes it clear how to solve the problem.
a simple question :
public class class1
{
public string string1;
public class class2
{
public string string2
{
get{ string tmp = class1.string1; }
}
}
}
I want to be able to reach class1.string1 from class2.string2.get, but I cant. What would you recommend me to change, so that I can do that?
Thanx
Passing class1 reference to class2 in constructor:
public class class1 {
public string string1;
public class class2 {
private class1 _Reference;
public class2(class1 reference) {
if (reference == null) {
throw new ArgumentNullException("reference");
}
_Reference = reference;
}
public string string2 {
get { return _Reference.string1; }
}
}
}
Passing class1 reference to class2 after both classes have been created:
public class class1 {
public string string1;
public class class2 {
private class1 _Reference;
public class1 Reference {
set { _Reference = value; }
}
public string string2 {
get { return _Reference.string1; }
}
}
}
static void usage() {
var foo = new class1();
var bar = new class1.class2();
bar.Reference = foo;
string value = bar.string2;
}
There is no means of accessing a class from within a nested class that I know of. Class nesting doesn't lead to automatic instantiation of the surrounding class, it's just a (usually rather smelly) means of structuring your code.
You would either need a reference to an actual instance of Class1 inside Class2 or you'd need a static method on Class1.
Another way to accomplish this would be to use inheritance, but that's a whole different beast to tame:
public class Class1 {
protected String String1 { get; set; }
}
public class Class2 : Class1 {
public String String2 {
get {
String PropertyFromClass1 = base.String1;
// ...
}
}
}
That said: Your code wouldn't compile, string2's getter doesn't return anything. And please make yourself familiar with C#'s naming conventions.
Thanx for the suggestions. Due to the specific nature of the code, I had to solve this situation with a global public static class in another namespace.
Coming from Java I faced this "problem" when I started developing in C#.
As clearly explained by Dennis Traub and in this article in C# you can't access outer class members or methods. So you have to implement what in Java happens automatically:
class OuterClass {
string s;
// ...
class InnerClass {
OuterClass o_;
public InnerClass(OuterClass o) { o_ = o; }
public string GetOuterString() { return o_.s; }
}
void SomeFunction() {
InnerClass i = new InnerClass(this);
i.GetOuterString();
}
}
I am trying to make a class so when I do the following inside a file:
Functions LoginFunctions = new Functions();
LoginFunctions.loadFunctions();
It will create my object which I need, and make it public so every form which calls the class will be able to use it. The class file is below.
namespace App
{
public class Functions
{
public void loadFunctions()
{
TaskbarItemInfo taskbarItemInfo = new TaskbarItemInfo();
}
}
}
It doesn't seem to be making the taskbarItemInfo object public, and it is not letting me use it anywhere else other then inside the class. How do I make it public so every file that calls the class can use the object?
As the others have mentioned, make it a property, for example like so:
public class Functions
{
public TaskbarItemInfo TaskbarItemInfo { get; private set; }
public void loadFunctions()
{
this.TaskbarItemInfo = new TaskbarItemInfo();
}
}
Your taskbaritem class is in the scope of the method and therefore you wont be able to access it outsite of the class.
Create a public property or return it in the method.
namespace App
{
public class Functions
{
private TaskbarItemInfo _taskbarItemInfo;
public TaskbarItemInfo taskbarItemInfo
{
get
{
return _taskbarItemInfo;
}
}
public void loadFunctions()
{
_taskbarItemInfo = new TaskbarItemInfo();
}
}
}
I would also go and change the loadFunctions method to a constructor which creates all the objects you need.
public Functions()
{
_taskbarItemInfo = new TaskbarItemInfo();
}
In the example you provide, taskbarItemInfo is declared within the local scope of the loadFunctions() method. If you want it to be public for some class, you must make it a class member before you can make it public.
You need to make the variable public.
namespace App
{
public class Functions
{
public TaskbarItemInfo TaskbarItemInfo { get; private set; }
public void loadFunctions()
{
TaskbarItemInfo = new TaskbarItemInfo();
}
}
}
EDIT: You could also do the initialization of the items in the constructor.
namespace App
{
public class Functions
{
public TaskbarItemInfo TaskbarItemInfo { get; private set; }
public Functions()
{
loadFunctions();
}
private void loadFunctions()
{
TaskbarItemInfo = new TaskbarItemInfo();
}
}
}
Then you don't need the LoginFunctions.loadFunctions(); line of code after you initialize your LoginFunctions object.
You probably want to access it as a property which generates a private static member when needed.
namespace App
{
public class Functions
{
private static TaskbarItemInfo _taskbarItemInfo;
public static TaskbarItemInfo TaskBarItemInfoProperty
{
get{
if (_taskbarItemInfo == null)
{
_taskbarItemInfo = new TaskbarItemInfo();
}
return _taskbarItemInfo;
}
}
}
public class Test
{
public void testFunction()
{
Functions.TaskBarItemInfoProperty.doSomething();
}
}
}