How to refer to strings in an external class (C#) - c#

How would it be possible to reference an external string in an external class.
e.g.
Class1.cs:
MessageBox.Show(mystring);
Class2.cs:
public static void myMethod()
{
string mystring = "foobar";
// some logic here
}

If I right understood your question, you can do something like this:
public class2
{
public static string MyString
{
get {return "foobar"; }
}
}
public class1
{
public void DoSomething()
{
MessageBox.Show(class2.MyString );
}
}

Something like this?
public static class Foo
{
public const string FOO_CONST = "value";
}
public class Bar
{
public void Bar()
{
Console.WriteLine(Foo.FOO_CONST);
}
}

If you create a new instance of Class2 you can make MyString public or pull it out into a get method:
//In Class1
Class2 class2 = new Class2();
MessageBox.Show(class2.Mystring());
//In Class2
public string Mystring{ get; set; }
Or you could return the string from the method
public static string myMethod()
{
string myString = "foobar";
//logic goes here
return myString;
}
//In Class1
Class2 class2 = new Class2();
MessageBox.Show(class2.MyMethod());

Based on your clarification to your question:
I am trying to check a boolean value in a method in class2. E.g. if
the method run in class2 changes the boolean value in that method, the
method in class1 can check this and do some logic
You could do something like this:
class Class1 {
Class2 myClass = new Class2();
public void ActivityMethod() {
myClass.MethodThatMayChangeBoolean();
if(myClass.myBoolean) {
// Response to a truth change.
} else {
// Respond to a false change.
}
}
}
class Class2 {
public boolean myBoolean { get; }
public void MethodThatMayChangeBoolean() {
// Do stuff in here that may change boolean.
}
}

You will need to use Properties
private static string _mystring = "foobar";
public static string mystring
{
get { return _mystring ; }
set { _mystring = value; }
}
Or use auto properties and initialize their values in the static constructor of the class:
public static string mystring { get; set; }
public static MyStaticClass()
{
mystring = "foobar";
}

Related

C# reuse instance method

I have 3 classes that have instance functions that are completely identical. Is there any way to reuse these instance methods or should I have a copy in all 3 classes e.g.
public class myclass1
{
public string METHOD;
public string RATE;
public string QTY;
void parseFunctionA()
{
}
void parseFunctionB()
{
}
void parseFunctionC()
{
}
}
public class MYCLASS2
{
public string PRICE;
public string WEIGHT;
void parseFunctionA()
{
}
void parseFunctionB()
{
}
void parseFunctionC()
{
}
}
For me two most popular ways would be to do it using:
Inheritance:
public class Program
{
public static void Main(string[] args)
{
var one = new myclass1();
var two = new myclass2();
one.parseFunctionA();
two.parseFunctionC()
}
// Common class can be abstract if you do want to
// prevent it to be instantiated by itself (prevents var common = new Common())
public class Common
{
// Methods made public only for example usage in Main
public void parseFunctionA()
{
}
public void parseFunctionB()
{
}
public void parseFunctionC()
{
}
}
public class myclass1 : Common
{
public string METHOD;
public string RATE;
public string QTY;
}
public class myclass2 : Common
{
public string PRICE;
public string WEIGHT;
}
}
Or composition:
public class Program
{
public static void Main(string[] args)
{
var common = new Common();
var one = new myclass1(common);
var two = new myclass2(common);
}
public class Common
{
public void parseFunctionA()
{
}
public void parseFunctionB()
{
}
public void parseFunctionC()
{
}
}
public class myclass1
{
private Common _common;
public myclass1(Common common)
{
_common = common;
}
public string METHOD;
public string RATE;
public string QTY;
// use _common as you see fit, in methods, in properties, etc.
}
public class myclass2
{
private Common _common;
public myclass2(Common common)
{
_common = common;
}
public string PRICE;
public string WEIGHT;
// use _common as you see fit, in methods, in properties, etc.
}
}
There are also other methods mentioned in comments under your original post.
Mind that there are no safety features in this example.

How to get all properties in a method of same base class?

Actually, I want to access properties of a base class in a method and I am not instantiating that object directly. Below is code, I am working on:
public class Test
{
public static void Main()
{
drivedclass obj = new drivedclass();
obj.DoSomething();
}
}
public class drivedclass : baseclass
{
public void DoSomething()
{
LoadSomeThing();
}
}
public class baseclass
{
public string property1
{
get;
set;
}
public string property2
{
get;
set;
}
public void LoadSomeThing()
{
//here I want to access values of all properties
}
}
I would like to know if there is a way, I can access the properties in method of same class and that class is base class.
You can just use property1 and property2 as they are.
However, note that in LoadSomeThing() you will not be able to access any properties of drivedlcass, because base classes cannot see properties of their derived classes by definition.
You can access them with reflection, but this is not the 'normal' way.
foreach(PropertyInfo prop in this.GetType().GetProperties())
{
prop.SetValue(this, newValue);
}
If you want to make it 'cleaner', you should make the properties virtual.
Use the following method to enumerate all property values:
public void EnumerateProperties()
{
var propertiesInfo = this.GetType().GetProperties();
foreach (var propertyInfo in propertiesInfo)
{
var val = propertyInfo.GetValue(this, null);
}
}
Question is quiet unclear but if you wish to access your properties, they are well present in both the Base class and the derived class. thus, if you do s = obj.property2 in your main class Test, that should be available.
public class Test {
public static void Main( ) {
drivedclass obj = new drivedclass( );
obj.DoSomething( );
string s = obj.property2 ;
}
}
You could always make it explicit:
public class DerivedClass : BaseClass
{
public string Property3
{ get; set; }
public void DoSomething ()
{
LoadSomeThing();
}
public override void LoadSomeThing ()
{
base.LoadSomeThing();
Console.WriteLine(Property3);
}
}
public class BaseClass {
public string Property1
{ get; set; }
public string Property2
{ get; set; }
public virtual void LoadSomeThing()
{
Console.WriteLine(Property1);
Console.WriteLine(Property2);
}
}
You can simply try: this.property1

Constructors GetInfo

I am new to C# and am working on classes and understanding them. My problem is I am not understanding how to create a Get to retrieve the private variable _yourname and Set to set the private variable _yourname.
namespace WindowsFormsApplication1
{
class InputClass
{
private string _yourName;
public string _banner;
public virtual void GetInfo();
public InputClass(String _banner)
{
_banner = "Enter your name";
}
}
}
Maybe I am using the wrong function to GetInfo. But I am also wondering when I have the GetInfo if in the () I should write _yourname in it.
In C# there are properties, which have the function of public getter and setter methods in other languages:
class InputClass
{
private string _yourName;
public string _banner;
public InputClass(String _banner)
{
this._banner = _banner;
}
public string YourName
{
get { return _yourName; }
set { _yourName = value; }
}
}
But you can use auto properties, if you want:
class InputClass
{
public InputClass(String _banner)
{
Banner = _banner;
}
public string YourName
{
get; set;
}
public string Banner
{
get; set;
}
}
It sounds like you are trying to provide access to the _yourName field. If so then just use a property
class InputClass {
public string YourName {
get { return _yourName; }
set { _yourName = value; }
}
...
}
Now consumers of InputClass can access it as if it were a read only field.
InputClass ic = ...;
string yourName = ic.YourName;
ic.YourName = "hello";
Note: C# provides a special syntax for simple properties like this which are just meant to be wrappers over private fields. It's named auto-implemented properties
class InputClass {
public string YourName { get; set; }
}
You can override getters and settings using the get and set keywords. For example:
class InputClass
{
private string _yourName;
private string _banner;
public YourName
{
get { return _yourName; }
set { _yourName = value; }
}
public Banner
{
get { return _banner; }
set { _banner = value; }
}
public InputClass(String banner)
{
_banner = banner;
}
}
1.) Use properties instead of members, you get a free accessor (get) and mutator (set).
public string YourName { get; set; }
public string Banner { get; set; }
2.) You can take advantage of the default constructor, and declare it on the fly.
//the old way:
InputClass myClass = new InputClass();
myClass.YourName = "Bob";
myClass.Banner = "Test Banner";
//on the fly:
InputClass myClass = new InputClass()
{
YourName = "Bob",
Banner = "Test Banner"
}

Calling a Variable from another Class

How can I access a variable in one public class from another public class in C#?
I have:
public class Variables
{
static string name = "";
}
I need to call it from:
public class Main
{
}
I am working in a Console App.
That would just be:
Console.WriteLine(Variables.name);
and it needs to be public also:
public class Variables
{
public static string name = "";
}
I would suggest to use a variable instead of a public field:
public class Variables
{
private static string name = "";
public static string Name
{
get { return name; }
set { name = value; }
}
}
From another class, you call your variable like this:
public class Main
{
public void DoSomething()
{
string var = Variables.Name;
}
}
You need to specify an access modifier for your variable. In this case you want it public.
public class Variables
{
public static string name = "";
}
After this you can use the variable like this.
Variables.name
class Program
{
Variable va = new Variable();
static void Main(string[] args)
{
va.name = "Stackoverflow";
}
}

Override only Get accessor

I got an abstract class :
abstract class ClassBase
{
public abstract string Test { get; }
}
I want to derive it and by the way add a set accesor
class ClassDerive : ClassBase
{
string _s;
public override string Test
{
get { return _s; }
set { _s = value; }
}
}
I can't do that because i may not override set
class ClassDerive2 : ClassBase
{
string _s;
public string Test
{
override get { return _s; }
set { _s = value; }
}
}
Syntax error
class ClassDerive3 : ClassBase
{
string _s;
public override string ClassBase.Test
{
get { return _s; }
}
public string Test
{
set { _s = value; }
}
}
Syntax error
Any Idea ???
thx
You cannot do exactly what you want to do but here is a workaround:
abstract class ClassBase
{
public abstract String Test { get; }
}
class ClassDerive : ClassBase
{
string _s;
public override string Test
{
get { return _s; }
}
public void SetTest(String test)
{
this._s = test;
}
}
This will make Test only settable in ClassDerived via the public SetTest method. I know this is not as clean as using the property's setter but it is about as good as it's going to get.
If at first you have defined a read-only property in a type, you can't later change it to a read/write property in a derived class. That's simply how .NET works, and can't be changed.
If, on the other hand, you define an interface with a read-only property, you can later implement that interface in a class with a writable property.
If you'd like to share what you are trying to achieve, perhaps we can come up with a design that works and can compile :)
Another way:
abstract class ClassBase
{
public abstract string Test { get; }
}
class ClassDerive : ClassBase
{
string _s;
protected void setTest(string s)
{
_s = s;
}
public override string Test
{
get { return _s; }
}
}
class ClassDerive2 : ClassDerive
{
public new string Test
{
get { return base.Test; }
set { setTest(value); }
}
}
class Program
{
static void Main(string[] args)
{
var cd2 = new ClassDerive2();
cd2.Test = "asdf";
Console.WriteLine(cd2.Test);
}
}
My first thought was also to implement it as an interface. If this fits in with your design, the following code will work:
public interface TestInterface
{
string TestProperty { get; }
}
public class TestClass : TestInterface
{
public string TestProperty
{
get { return "test"; }
set { string t = value; }
}
}
No you cant, sorry. It is by design, so it's the law.

Categories