I am trying to use setter and getter. When i debug, the value gets set but when i try to retrieve, it gets null value.
Class1.cs
private string setMAX;
public string SETMax
{
get
{
return setMAX;
}
set
{
setMAX = value;
}
}
private string value1;
public string MaxValue
{
get
{
return value1;
}
set
{
value1= value;
}
}
Class2.cs
Class1.SETMax = Class1.value1; //This gets set
Class3.cs
//When i debug, first Class1.cs and Class2.cs completes, then it comes in Class3.cs
string max = Class1.SETMax; //I GET NULL here.
I dont know where I am wrong here.Can anyone please explain me ?
You are referencing File1 as an instance. You are probably referencing different instances. You probably want static properties.
private static string setMAX;
public static string SETMax
{
get
{
return setMAX;
}
set
{
setMAX = value;
}
}
I think you have a few things mixed up so lets start from the beginning
Class1.SETMax = Class1.value1;
// for a start you are assigning a
// private variable to a public one
// via the Class definition I'm not even sure how that compiles.
Have a look here see if this makes sense to you
// This is a Class definition
public class Class1 {
public string SETMax {get; set;}
public int MaxValue {get; set;}
}
// This is your application
public class MyApp{
// this is a private field where you will assign an instance of Class1
private Class1 class1Instance ;
public MyApp(){
//assign the instance in the constructor
class1Instance = new Class1();
}
public void Run {
// now for some fun
class1Instance.SETMax = "Hello";
Console.WriteLine(class1Instance.SETMax); // outputs "Hello"
var localInstance = new Class1();
localInstance.SETMax = class1Instance.SETMax;
Console.WriteLine(localInstance.SETMax); // outputs "Hello"
}
}
Related
I'm trying to get and set a property using the following code.
But the when trying to print the property using Console,it returns an empty string.Why is the property not getting set?
using System;
public class Program
{
public static void Main()
{
myclass x=new myclass();
x.myproperty="test";
Console.WriteLine(x.myproperty);
}
class myclass{
string sample;
public string myproperty
{
get { return sample;}
set {sample=myproperty;}
}
}
}
In setter you should use value to assign new value to underlying field
use this instead
public string myproperty
{
get { return sample; }
set { sample = value; }
}
or in C#7
public string myproperty
{
get => sample;
set => sample = value;
}
Edit
As #bradbury9 mentioned, you can also use auto-implemented properties, of course this is the case if you don't want any other logic in getter and setter than just getting and setting the field, if this is the case you can use below snippet
public string myproperty { get; set; }
value keyword is important for setting the value. In Visual Studio you can use propfull + double tab to avoid such common mistakes. It will create full property through shortcuts.
Here is the solution
public static void Main()
{
myclass x = new myclass();
x.myproperty = "test";
Console.WriteLine(x.myproperty);
}
class myclass
{
string sample;
public string myproperty
{
get { return sample; }
set { sample = value; }
}
}
If you just want to return null instead of empty string. This works even when you deserialize your Json:
class myclass
{
string sample;
[JsonProperty("my_property")]
public string My_property
{
get { return sample; }
set { sample = string.IsNullOrEmpty(value) ? null : value; }
}
}
I would like to pass values from one class file to another class.
E.g:
Step1:
Class1.cs
public class Class1
{
public string LogedInPerson { get; set; }
public string Name
{
get { return this.LogedInPerson; }
set { this.LogedInPerson = value; }
}
}
Step2:
Value has been assigned in below method:
test.xaml.cs
public void assignValue()
{
Class1 obj = new Class1();
obj.LogedInPerson = "test123";
}
Step3:
I would like to get "test123" values from Class2.cs.
E.g:
public void test()
{
string selected_dept = ?? //How to get "test123" from here.
}
You can have variables class that includes public variables. Define instance of class1 in variables class .
public static class1 myclass=new class1();
in test.xml.cs set value
public void assignValue()
{
myclass.LogedInPerson = "test123";
}
in class2.cs
public void test()
{
string selected_dept = myclass.LogedInPerson;
}
Initialize Class1 outside assignValue() methos
Class1 obj = new Class1();
public void assignValue()
{
obj.LogedInPerson = "test123";
}
public string returnValue()
{
return obj.LogedInPerson;
}
if your second class name test.xaml then call it like this, but I don't think you can use class name test.xaml so use a nice name instead there eg: Class2
public void test()
{
test.xaml test = new test.xaml();
test.assignValue();
string selected_dept = test.returnValue(); //How to get "test123" from here.
}
I believe this question is more on the topic of basic Object Oriented Programming principles, not so much about WPF specific features. Therefore, I will provide you a non-WPF specific answer, as it will allow me to address your question in the most direct way.
In OOP, a method can return a result to the caller. So, for instance,
public string GetReturnObject(){
return "This is a return object";
}
You can create a new object and pass it back to the caller,
public void Test(){
string data = GetReturnObject();
}
And now data will be assigned the object that was returned from the method that Test() called. So, if you modify your AssignValue method by adding a return type and passing the instantiated Class1 object back to the caller, you will have the answer you need
public Class1 assignValue()
{
Class1 obj = new Class1();
obj.LogedInPerson = "test123";
return obj;
}
Hope that helps.
I have the following class but I can't seem to get the desired results with C#.
public class AppOsType {
public static class IOS {
public static int IOS()
{
return 100;
}
public static string ToString()
{
return "iOS";
}
}
... // additional values
}
I want to get the following results:
AppOsType.IOS // returns 100
AppOsType.IOS.ToString() // returns "iOS"
But I'm getting an error saying AppOsType.IOS is a type when i do the following:
Assert.AreEqual(100, AppOsType.IOS);
What am I missing?
Edit: left out static.
Not sure why all the static and inner class stuff is needed for, why don't you keep it simple and define an enum:
public enum AppOsType
{
IOS = 100
}
Then use
var ios = AppOsType.IOS;
var number = (int)ios;
var name = ios.ToString();
If you need to return a translated string based on enum, you could add a dictionary:
var translations = new Dictionary<AppOsType, string>()
{
{ AppOsType.IOS, "iOs" }
}
and then
var ios = AppOsType.IOS;
var number = (int)ios;
var name = translations[ios];
If you really need this nested static class inside the AppOsType class then you need to change something because a method cannot have the same name of the class and a constructor cannot return values. (Think to the fact as if the return value of the constructor is already defined to be the instance of the class)
void Main()
{
Console.WriteLine(AppOsType.IOS.Version);
Console.WriteLine(AppOsType.IOS.ToString());
}
public class AppOsType
{
// .... other members here ?? ...
public static class IOS
{
public static readonly int Version;
static IOS()
{
// In the static constructor you could set the readonly
// static property
Version = 100;
}
public static string ToString()
{
return "iOS";
}
}
}
Its becaus IOS is a class. AppsOsType.IOS points to the static class. If your Method public int IOS() would be static, you can access it using AppOsType.IOS.IOS()
If you don't want an enum for some reason, to make it work with desired syntax, you need a public property (not a ctor or method) :
public class AppOsType {
public static class IOS {
public static int IOS
{
get { return 100; }
}
public static string ToString()
{
return "iOS";
}
}
}
You are using the same name IOS so change this
AppOsType.IOS // returns 100 [error]
to
AppOsType.IOS.IOS // might returns 100
public class Foo
{
public Foo(){ }
//One of many properties
//set up in the same way
private String _name;
public String Name
{
get { return _name; }
set {
_name = value;
//code that is important to run
//only after the objects initial creation
}
}
private int _id;
public int ID
{
get { return _id; }
set {
_id = value;
//code that is important to run
//only after the objects initial creation
}
}
public void Win()
{
//clean up method that wouldn't be needed
//if I used optional parameters because
//i would be able to set _name (and all the
//other private properties directly without
//using the public Set
}
}
How do I call a method automatically after this kind of object creation in c#
Foo ko = new Foo() {
ID = 4,
Name = "Chair"
};
ko.Win(); // <-- Want this to be called automatically inside the class
There is no method that automatically called after some random set of properties is set (Which is what initialization is translated to...)
var foo = new Foo { Name = "bar" };
Is actually shortcut to:
var foo = new Foo();
foo.Name = "bar";
When written in second form one would not expect any magical method to be called after foo.Name assignment.
You options:
if you have some information that need to be set on property change - just make it a property and write code in set part of it.
if you must have particular set of properties configured before object is considered "created" constructor arguments is one reasonable way to enforce it.
you can also implement builder pattern that allow you to delay final construction (or use some other factory method that forces setting parameters before final object creation.
Sample of code with builder pattern:
var foo = new FooBuilder { Name = "bar" }
.Build();
add the Win() to the constructor. Call/put inside the constructor.
public Foo(string value, string value2) {
Value = value;
Value2 = valu2e;
Win();
}
This is the constructor. Set it manually.
Well if you are always setting ID and the Name how about this?
private string _Name;
public string Name
{
get { return _Name; }
set {
_Name = value;
this.Win();
}
}
Win function will always called after you set a value on the name or you can do this for ID that's your choice!
Not the most scalable solution but why not try this:
public class Foo
{
public int ID { get; set; }
public String Name { get; set; }
public Foo()
{
}
public Foo( int id )
{
// Win()
ID = id;
// Win()
}
Public Foo( string name )
{
// Win()
Name = name;
// Win()
}
public Foo( int id, string name )
{
// Win()
ID = id;
Name = name;
// Win()
}
public void Win()
{
//Do Stuff that would go into the constructor
//if I wanted to use optional parameters
//but I don't
}
}
You can call Win before or after setting the properties.
I have a class that requests that when called a string is sent when requesting / initializing it.
class Checks
{
public Checks(string hostname2)
{
// logic here when class loads
}
public void Testing()
{
MessageBox.Show(hostname2);
}
}
How would it be possible to take the string "hostname2") in the class constructor and allow this string to be called anywhere in the "Checks" class?
E.g. I call Checks(hostname2) from the Form1 class, now when the Checks class is initialized I can then use the hostname2 string in my Checks class as well
Declare a member inside the class and assign the value you passed to the member inside the constructor:
class Checks
{
private string hostname2;
public Checks(string hostname2)
{
this.hostname2 = hostname2; // assign to member
}
public void Testing()
{
MessageBox.Show(hostname2);
}
}
If you also need to have outside access, make it a property:
class Checks
{
public string Hostname2 { get; set; }
public Checks(string hostname2)
{
this.Hostname2 = hostname2; // assign to property
}
public void Testing()
{
MessageBox.Show(Hostname2);
}
}
Properties start with a capital letter by convention. Now you can access it like this:
Checks c = new Checks("hello");
string h = c.Hostname2; // h = "hello"
Thanks to Andy for pointing this out: if you want the property to be read-only, make the setter private:
public string Hostname2 { get; private set; }
You need to copy the constructor argument in a class variable:
class Checks {
// this string, declared in the class body but outside
// methods, is a class variable, and can be accessed by
// any class method.
string _hostname2;
public Checks(string hostname2) {
_hostname2 = hostname2;
}
public void Testing() {
MessageBox.Show(_hostname2);
}
}
You can expose a public property to retun the hostname2 value which is the standard for exposing your private varibles
class Checks
{
private string _hostname;
public Checks(string hostname2)
{
_hostname = hostname2;
}
public string Hostname
{
get { return _hostname; }
}
}