Variable not saving data in C# - c#

i've been wondering why my variable is not saving data. this is my code
class MainProg
{
public string name;
static void Main()
{
MainProg m = new MainProg();
m.Start();
}
public void Start()
{
Register rs = new Register();
Register r = (Register)rs;
r.run();
Console.WriteLine(name);
}
}
class Register : MainProg
{
public void run()
{
name = "a";
}
}
Did I forget anything?everytime I try to show the output it shows nothing.Thanks by the way for taking your time.

This will work.
class MainProg
{
static void Main()
{
Register rs = new Register();
Register r = (Register)rs;
r.run();
Console.WriteLine(r.name);
}
}
class Register : MainProg
{
public string name;
public void run()
{
name = "a";
}
}
The reason your code fails is that you are creating an instance of Register, which is separate from MainProg even though it inherits from it. You set the variable in the new instance, and then read it from the old.

name is an instance field so you need reference of an object.
Try,
Console.WriteLine(r.name);

Because you are changing the name inside the instance of Register called r then you print the var name inside the instance of MainProg.
To prove it try:
Console.WriteLine(r.name);

Since you are trying to access a member variable inside a static function it will throw an compilation error.
Try to access the variable using object like
Console.WriteLine(r.name);

The problem is that you are trying to write the name from this object. The variable you changed is located in the other object 'r'.

Related

Statics varibales dont show in Unity

Ok, first, excuse my english. Well, I stay making a SAVEFILE with [Serializable()], there's not the problem, the problem is, when I try to make a change of a this static string with the Username, I can't find the reference to this variable in the other script with de InputField. Nothing strange, the SAVEFILE script just say:
and the script save:
public static UIsettings uisettings;
//Data to save.
public string AirlineName = "Unknow";
public string UserName = "Unknow";
//Autoload.
public bool StartupLoad;
void Awake()
{
if(uisettings == null)
{
uisettings = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
void Start()
{
if (StartupLoad)
{
Load();
}
}
//Guardar
public void Save() //Start the SAVEGAME code
later, I change the value of Airline Name and Username from other two scripts in InputFields, the two methods look like these:
public void SubmitName(string newAirlineName)
{
if(newAirlineName == "")
{
print("Write a valid name");
}
else
{
print(newAirlineName + " es valido.");
profileData.AirlineName = newAirlineName;
profileData.Save();
Debug.Log(UIsettings.uisettings.AirlineName);
}
}
If I call it like this, work, but is useless bacause when I change of scene these values go again to Default, so I use static in the values like:
{
public static string AirlineName = "Unknow";
}
without keys, obiously. But, when I make this the metod of the second script can't show the assigned variable, say: error CS0176: Member 'UIsettings.AirlineName' cannot be accessed with an instance reference; qualify it with a type name instead...
I have a few days trying to solve this by myself but, I dunno what to do.
Is to know that the SAVEGAME script is a DontDestoyOnLoad file, like can be see in the first script, but I this that this is't 'cause the script works fine without the static.
When you have a static variable (public static string AirlineName) you can only reference it using the class name -- not an instance of the class - because only one AirlineName exists, and it is not related to any instance of your class.
Here's an example of why you're getting your error:
// This example shows why are you getting the error. Skip to the second example
// to see the solution that you should use
public class UiSettings : MonoBehaviour
{
public static string AirlineName = "Unknow";
}
// ...
public void SubmitName(string newAirlineName)
{
// Just an example of getting the current instance of UiSettings
UiSettings profileData = (UiSettings)GameObject.FindObjectOfType(typeof(UiSettings));
// You can't do this because AirlineName is a static property, so you can't use an
// instance variable (profileData) to access it because AirlineName doesn't belong
// to an instance
profileData.AirlineName = newAirlineName;
// You would have to access it like this
UiSettings.AirlineName = newAirlineName;
}
But since you're making UiSettings a singleton class by using DontDestroyOnLoad, you would do this
public class UiSettings : MonoBehaviour
{
public static UIsettings uisettings;
// Do not make these static
public string AirlineName = "Unknow";
public string UserName = "Unknow";
void Awake()
{
if(uisettings == null)
{
uisettings = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
}
// ...
public void SubmitName(string newAirlineName)
{
// uisettings contains an instance of UiSettings, so you are allowed to say
// UiSettings.uisettings.AirlineName = ...
UiSettings.uisettings.AirlineName = newAirlineName;
}
I would also recommend changing the name uisettings to Instance to make it clear that the property references the singleton instance of the class. Like this:
public class UiSettings : MonoBehaviour
{
// Instead of public static UiSettings uisettings
public static UiSettings Instance { get; private set; }
}

How to make a string different for each instance of a class

So, I have a public partial class called Condensed. Each instance of condensed is supposed to have its own file path which it will load data from, which I tried to create by having a private static string called Path I then create 6 instances of Condensed, but I found out that when I change one of the values of Path that all of the instances of Condensed set their value of Path to the most recent one
public partial class Condensed : UserControl
{
private static string Path;
public Sender Export()
{
//this uses Path to Load data then return it to the main class
}
public void Load(string path)
{
Path = path
}
}
And then inside my main class I do as follows:
public class Main
{
public void Load_Condensed()
{
condensed1.Load(Paths[0]);
condensed2.Load(Paths[1]);
condensed3.Load(Paths[2]);
condensed4.Load(Paths[3]);
condensed5.Load(Paths[4]);
condensed6.Load(Paths[5]);
}
private void exportToolStripMenuItem_Click(object sender, EventArgs e)
{
List<Condensed> Pokemon = new List<Condensed>
{
condensed1.Export(),
condensed2.Export(),
condensed3.Export(),
condensed4.Export(), //Loads all of the data from calling Export()
condensed5.Export(),
condensed6.Export()
};
Export(Condensed); //Sends the data
}
}
Paths[] is just an array of file paths stored as a public array of strings.
Basically I want each instance of Condensed to have its own unique string Path which can be used inside of the instance it is created for, how do i do this?
Your Path-field is static, which means it gets shared between all instances.
Modify it to an instance field to have it different for each instance:
public partial class Condensed : UserControl
{
private string Path; // No static here
public Sender Export()
{
this uses Path to Load data then return it to the main class
}
public void Load(string path)
{
Path = path
}
}
The fact that it is a partial class, doesn't really have anything to do with it. Partial classes just mean that you can define the class in separate files (in this case because it's a usercontrol that needs to set up some code for the UI)
The static is causing your problem because it uses shared memory between all instances of the Condensed class, just remove the static keyword from your property definition.
using System;
namespace ConsoleApp
{
class Program
{
static void Main()
{
var condensed1 = new Condensed() { FilePath = "/filepath1/" };
var condensed2 = new Condensed() { FilePath = "/filepath2/blah/" };
var condensed3 = new Condensed() { FilePath = "/filepath3/blah/blah/" };
Console.WriteLine(condensed1.FilePath);
Console.WriteLine(condensed2.FilePath);
Console.WriteLine(condensed3.FilePath);
Console.ReadKey();
}
}
public partial class Condensed
{
public string FilePath { get; set; }
}
}
Add new constructor like
public Condensed(string path)
{
Path = path
}
And when you create an instance of Condensed class, call it like
Condensed c1 = new Condensed("C/Desktop/blabla");
c1.Export();

How to pass values from one class to another class using wpf?

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.

How do I get the value from a variable which has it's values assigned in another class?

I need to have a class for Variables and Methods for the sake of manageability.
The problem is I am not sure how I can access the value of the variable assigned in the method class.
Here is the variable class, it just holds variables:
namespace Classes
{
class MyVariableClass
{
public string MyVariable { get; set; }
}
}
Here is the method:
I am calling an instance of the variable class in the method class so that I can assign the variable to the values of the method.
namespace Classes
{
class MyMethods
{
MyVariableClass myVarClass = new MyVariableClass();
public void DoSomeStuff(string myString1, string myString2)
{
myVarClass.MyVariable = (!string.IsNullOrEmpty(myString2)
? myString2 : "String 2 has nothing!");
}
}
}
Finally, below is the Main Method:
When I run this code, MyVariable returns null, I am assuming I am accessing the variable before it is assigned it's values?
How do I get the variable with the values assigned in the Method class?
namespace Classes
{
class Program
{
static void Main(string[] args)
{
MyMethods myMethod = new MyMethods();
MyVariableClass myVarClass = new MyVariableClass();
string something = "something";
string nothingHere = null;
myMethod.DoSomeStuff(something, nothingHere);
//I need to call MyVariable here
//Or be able to access it's values assigned in the MyMethod Class.
Console.Write(myVarClass.MyVariable);
Console.ReadKey();
}
}
}
Problem : You are working on two different objects.
you are creating an object with an instance variable myVarClass in MyMethods class.
you are creating one more new object with an instance variable myVarClass in Main() method.
Note : You should always remeber that in object oriented programming objects are independent and maintain their own copy so modifying one object parameters/properties doesnot effect the other object parameters or properties.
Solution : instead of creating two different object create only one object with instance variable myVarClass in Main() method and pass it to the myClass method.
so you should change your myClass method DoSomeStuff() as below to accept the instance variable of `MyVariableClass``
public void DoSomeStuff(MyVariableClass myVarClass, string myString1, string myString2)
{
//do something
}
from Main() method call the above method as below:
MyVariableClass myVarClass = new MyVariableClass();
string something = "something";
string nothingHere = null;
myMethod.DoSomeStuff(myVarClass, something, nothingHere);
Complete Code:
namespace Classes
{
class MyMethods
{
public void DoSomeStuff(MyVariableClass myVarClass, string myString1, string myString2)
{
myVarClass.MyVariable = (!string.IsNullOrEmpty(myString2)
? myString2 : "String 2 has nothing!");
}
}
}
and Main Program should be :
namespace Classes
{
class Program
{
static void Main(string[] args)
{
MyMethods myMethod = new MyMethods();
MyVariableClass myVarClass = new MyVariableClass();
string something = "something";
string nothingHere = null;
myMethod.DoSomeStuff(myVarClass, something, nothingHere);
//I need to call MyVariable here
//Or be able to access it's values assigned in the MyMethod Class.
Console.Write(myVarClass.MyVariable);
Console.ReadKey();
}
}
}
The myVarClass member of MyMethods is private by default, so if you want to be able to call it from outside the class itself, then you need to make it public.
Once public, you'll able to do:
static void Main(string[] args)
{
MyMethods myMethod = new MyMethods();
MyVariableClass myVarClass = new MyVariableClass();
string something = "something";
string nothingHere = null;
myMethod.DoSomeStuff(something, nothingHere);
//I need to call MyVariable here
//Or be able to access it's values assigned in the MyMethod Class.
Console.Write(myMethod.myVarClass.MyVariable);
Console.ReadKey();
}
Also, be careful with the fact that the myVarClass defined in main is a completely different object.
Cheers
Define you class as:
namespace Classes
{
class MyMethods
{
public MyVariableClass MyVarClass {get; private set;}
public void DoSomeStuff(string myString1, string myString2)
{
if(MyVarClass == null)
{
MyVarClass = new MyVariableClass();
}
MyVarClass.MyVariable = (!string.IsNullOrEmpty(myString2)
? myString2 : "String 2 has nothing!");
}
}
}
then use as:
namespace Classes
{
class Program
{
static void Main(string[] args)
{
MyMethods myMethod = new MyMethods();
string something = "something";
string nothingHere = null;
myMethod.DoSomeStuff(something, nothingHere);
//I need to call MyVariable here
//Or be able to access it's values assigned in the MyMethod Class.
Console.Write(myMethod.MyVarClass.MyVariable);
Console.ReadKey();
}
}
}

How to pass in the "modifier" string returned by the function?

class test
{
public String get() {return s;}
private String s="World";
}
class modifier
{
public static void modify(ref String v)
{
v+="_test";
}
}
String s1="Earth";
modifier.modify(ref s1); <-------- OK
test c=new test();
modifier.modify(ref c.get()); <------- Error
How to pass in the "modifier" string returned by the function?
Assignment through another String object is unacceptable. So how will the copy is created.
You're trying to write C++ code in C#. You'll need to approach this the C# way.
Add a property for test.s, instead of a method called "get()".
We'll call this new property "MyString" in the code below:
class test
{
public String MyString
{
get
{
return s;
}
set
{
s = value;
}
}
private String s = "World";
}
class modifier
{
public static string modify(String v)
{
return v + "_test";
}
}
test c = new test();
c.MyString = modifier.modify(c.MyString);
If you dont like Matthew Watson's answer and you want to stay to your approach, the way to go is by using StringBuilder.That is a String that can change its value without creating a new one!
What i mean:
Normal String's value cant be changed, what is happening is that a new String is being created....and the pointer(that points to the String you want to change its value) points from now on to this new String. All other pointers....that "were pointing" to the old String .....are still pointing...to the old String!(the String's value didnt change!)
I am not sure if thats clear enough but you have to understand this if you want to play with Strings.This is exactly why s1 cant change its value.
The workaround is with StringBuilder:
class test
{
public StringBuilder get() { return s; }
private StringBuilder s = new StringBuilder("World");
}
class modifier
{
public static void modify(StringBuilder v)
{
v.Append("_test");
}
}
And some test code : (of course all this comes with processing cost...but i dont think that will be an issue for now)
StringBuilder s1 = new StringBuilder("Earth");
System.Diagnostics.Debug.WriteLine("earth is {0}", s1);
modifier.modify(s1); //<-------- OK
System.Diagnostics.Debug.WriteLine("earth is {0}",s1);
test c=new test();
StringBuilder aa=c.get();
System.Diagnostics.Debug.WriteLine("earth is {0}", aa);
modifier.modify(aa); //<------- Error(not anymore)
System.Diagnostics.Debug.WriteLine("earth is {0}", c.get());
Before you use the code try to understand how String and StringBuilder works
You have to create a temporary variable:
string tmp = c.get();
modifier.modify(ref tmp);
Because you are passing your parameter by reference.
The class test is designed such that its field s can't be reassigned (changed to point to a new object) from outside the class. That's because s is private, and the get() method only returns s, hence can't reassign it.
Either change the class test to allow the outside world to reassign s somehow, or use reflection to access a private field from the outside.
You can use a property with a get/set-eccessor and then pass to the modify() a test object:
class test
{
public String myString
{
get { return s; }
set { s = value; }
}
private String s="World";
}
class modifier
{
public static void modify(test myTest)
{
myTest.myString += "_test";
}
}
test c = new test();
modifier.modify(c);
Console.WriteLine(c.myString); //World_test

Categories