what I want to do is:
Some script accessing:
//...
string Str = "aab";
void Update(){
GUI3D.Label(Str);
GUI3D.TextField(ref Str);
}
//...
in GUI3D script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public static class GUI3D {
static void Label(string Text){
// make text apear
}
static int CursorField;
static void TextField(ref string Text){
// changing cursor field depending on the inputs
// change text and make it apear
}
}
I hope I didn't strip down too much.
but what I want to do is that Only TextField can access the CursorField and noone else.
so that if I'd want to do:
static void Label(string Text){
// make text apear
CursorField = 0; // that I'd get error due to protection level.
}
how would I be doing that?
You have an architecture issue. You can't hide a class member from itself. You are telling your static GUI3D class to Label() something and you are also trying to hide data from itself? Why do you need to hide the data from one member function of your GUI3D class?
If you don't mind changing your architecture a bit, you could try a class like this ( forgive syntax errors, I don't have a compiler handy :D ):
public class MyLabel {
private string _text;
public string Text {
get { return _text; }
set { _text = value; CursorField = _text.Length; }
}
public int CursorField { get; private set; }
}
As baoghal said, you can't hide a class member from itself - so wrap that functionality in an auxiliary class & only let the class itself modify CursorField.
Related
First of all I want to make clear the fact that I'm not using Unity or anything related to this .
I want to get values from a variable from a script to another , but inside the same .cs file.
My variable is named spellpos
Im setting it's values in this script
public class MyMissile: ISpellScript
etc etc
public void CastKeg(ISpellMissile missile)
{ etc
var spellpos = new Vector2(daspell.CastInfo.TargetPositionEnd.X, daspell.CastInfo.TargetPositionEnd.Z);
etc
} etc etc
Then I want to access that spellpos value in another script
public class GragasQToggle : ISpellScript
etc etc
public void OnSpellCast(ISpell spell)
{
var position = spellpos;
}
How can I achieve this ?
OR
You can make the first method return the value and use in the second method like below:
public Vector2 CastKeg(ISpellMissile missile)
{
var spellpos = new Vector2(daspell.CastInfo.TargetPositionEnd.X, daspell.CastInfo.TargetPositionEnd.Z);
return spellpos;
}
And then you call in the second method like below:
public void OnSpellCast(ISpell spell)
{
var position = CastKeg(spell);
}
Try making your variable public:
public class MyMissile: ISpellScript
{
public static Vector2 spellpos;
public void CastKeg(ISpellMissile missile)
{
spellpos = new Vector2(daspell.CastInfo.TargetPositionEnd.X, daspell.CastInfo.TargetPositionEnd.Z);
}
}
And now you can access the variable from the other class:
public class GragasQToggle : ISpellScript
{
public void OnSpellCast(ISpell spell)
{
var position = MyMissile.spellpos;
}
}
Another option is to make get and set functions for each class
and leave them as private.
If those are separated programs or even the same program a creative solution is to use TCP sockets.
I'm writing a code for a small game so naturally, I have a lot of variables that all do different things whenever they change. My question is, is it possible to tell my code to run certain lines of code each time the variables change, so as to avoid repeatedly typing in the same lines, over and over again?
Example:
Hero1.Health = 10;
Hero1.MaxHealth = 12;
ProgressBarHeroHealth1.Max = Hero1.MaxHealth;
ProgressBarHeroHealth1.Value = Hero1.Health;
If I change the hero's health, I want the Progress Bars value to automatically change with it, without having to write it down every time that it changes.
I don't know if it's at all possible, or if I'm just hoping for too much, so I'm coming to you for answers. Any info would be much appreciated.
Beginner
You could write an extension method for the control, e.g.
static class ExtensionMethods
{
static public void SetHealth(this ProgressBar control, Hero hero)
{
control.Max = hero.MaxHealth;
control.Value = hero.Health;
}
}
And call it like this:
ProgressBarHeroHealth1.SetHealth(Hero1);
Intermediate
Now extension methods are cool and all, but this is like the dumbest way to use them. If the control class isn't sealed, a better design is to write a proper method:
public class HealthBar : ProgressBar
{
public void SetHealth(Hero hero)
{
this.Max = hero.MaxHealth;
this.Value = hero.Health;
}
}
And call it the same way:
HealthBarHeroHealth1.SetHealth(Hero1);
(In order for this to work, you have to use HealthBar instead of ProgressBar in whatever UI platform you're using).
Advanced
But you know what would be really cool? A progress bar that updates itself. Hmmm... maybe if it listened for some kind of event....
class HeroHealthBar : ProgressBar
{
protected readonly Hero _hero;
public HeroHealthBar(Hero hero) : base()
{
_hero = hero;
hero.HealthChanged += this.Hero_HealthChanged;
}
public void Hero_HealthChanged(object sender, EventArgs e)
{
this.Max = _hero.MaxHealth;
this.Value = _hero.Health;
}
}
Of course you'd need a Hero class that raised the event....
class Hero
{
public event EventHandler HealthChanged;
public void SetHealth(int current, int max)
{
_health = current;
_maxHealth = max;
OnHealthChanged();
}
protected void OnHealthChanged()
{
if (HealthChanged != null) HealthChanged(this, EventArgs.Empty);
}
}
Once all this setup is done, when you want to set the health, all you have to do is write
_hero.SetHealth(current,max);
...which will set it on the hero and automatically notify the progress bar to update itself as well.
It is possible. Suppose You have a class called Hero and you want to change progress bar colour when healt is increase or decrease then
Class Hero
{
Public String Name{get; set;}
Public Double health;
Public Double Health
{
get
{
return health;
}
set
{
//Here you can place a function which can easily check the value of health and work accordingly.
ProgressBarColorChange(value+health);
health = value;
}
}
Public void ProgressBarColorChange(double health)
{
//Color Change Fuction Implementation
}
}
I would ask you what you think about the method I use to store a lot of string constant that I use in my program.
The goal is not to block memory resources and have some text string used for, an example, a debugger, but not only, basically its a text library used in all my program with some default text string.
I first thought about a singleton class but I don't know if it would be more efficient in this case, while singleton is in the heap and not cool for memory issues while the struct is normally in the stack and seeing microsoft advice would be more efficient.
here is the code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MessageBox.Show(Library.Apps_txt.resultvalue);
//I want to be able to call that in any form or class using this namespace
}
}
public class Library
{
public struct Apps_txt
{
public static String resultvalue { get { return "result value IS: "; } }
public static String Pbexcept { get { return "Exception has raised."; } }
//thousand of string.
//...
public static String mystring1000000 { get { return "my string 1000000"; } }
}
}
Thanks a lot.
hey guys, i have 3 winForms named carForm,parForm and updateForm, so there's updateForm.show() method in both carForm n parForm, while m in updateForm i want to know which class/form has called updateForm, so that i can update the respected class db. Currently i'm setting up a public global variable to verify that which form is calling updateForm..but i was thinkin' is there's another way to do this, i guess Reflection can solve this issue, but i'm not able to solve it, here's my code
///carForm
public class carForm:Form
{
Program.globalvariable="CAR"; //global variable
UpdateFrom updateForm=new UpdateForm();
updateForm.Show();
}
///parForm
public class parForm:Form
{
Program.globalvariable="PAR";
UpdateFrom updateForm=new UpdateForm
updateForm.Show();
}
///updateForm
public class updateForm:Form
{
if(Program.globalvariable=="CAR")
///code for update CAR db table
else if(Program.globalvariable=="PAR")
///code for update PAR db table
Type obj = GetType(); //This is what i was tryin' using Reflection but giving error
}
so if i get the calling Class/Objects info, i can update respected DB table,
can ne1 know hw to do this with Reflection,
Put the argument in a constructor of updateForm
///carForm
public class carForm:Form
{
UpdateFrom updateForm=new UpdateForm("CAR");
updateForm.Show();
}
///parForm
public class parForm:Form
{
UpdateFrom updateForm=new UpdateForm("PAR");
updateForm.Show();
}
///updateForm
public class updateForm:Form
{
private readonly string _key;
public updateForm(string key)
{
_key = key;
}
public void SomeMethod()
{
// check for _key here.
}
}
Edit:
If you want to have the actual type you can pass it directly, no need for reflection.
///carForm
public class carForm:Form
{
UpdateFrom updateForm=new UpdateForm(this.GetType());
updateForm.Show();
}
///parForm
public class parForm:Form
{
UpdateFrom updateForm=new UpdateForm(this.GetType());
updateForm.Show();
}
///updateForm
public class updateForm:Form
{
private readonly Type _type;
public updateForm(Type type)
{
_type = type;
}
public void SomeMethod()
{
// check for _type here.
}
}
Edit 2:
But in general, passing the type like this smells like bad code. Your control flow will probably end up like a bowl of spaghetti.
If you want the updateForm to update some values on the other forms you should
Send all relevant information about what questions/titles/etc to show in the updateForm in the constructor of the updateForm.
In the updateForm, save relevant "answers" to public properties of updateForm
Set DialogResult in updateForm to OK or Cancel depending on how you exit updatForm
Call updateForm like this: if (updateForm.ShowDialog == DialogResult.OK) {// read all properties from updateForm}
You'd probably have to look at the StackTrace class if you wanted to automatically get that kind of information. Not sure about the performance of using it though...
friends i have problem with using get or set in class in c#
when i use get or set in gives error(invalid token { in class)
pls, see below code,i have this problem in it
static int abcd
{
get
{
return _abcd;
}
}
thanx
this is the complete code,i dont have this problem with any of your codes but just this:
namespace ConsoleApplication2
{
class Program
{
class Car
{
private int _speed;
public int Speed;
{
get
{
return _speed
}
}
}
}
}
The snippet you have posted is fine as it is, also in regards to the error, as it has the correct number of { to } and in the right order.
Look at where you have placed it (possibly outside of a class), or look for extra } in the file.
Update: (following edit in question)
Your issue is here:
public int Speed; // <-- ; should not be here
And:
return _speed // <-- missing the ;
The property should be implemented like this:
public int Speed
{
get
{
return _speed;
}
}
There are two errors in your code.
You have a semicolon where there shouldn't be one (spotted by Oded).
You are missing a semicolon where there should be one.
Try this instead:
namespace ConsoleApplication2
{
class Program
{
class Car
{
private int _speed;
public int Speed // <-- no semicolon here.
{
get
{
return _speed; // <-- here
}
}
}
}
}
I noticed that the code you originally posted was formatted badly. I would suggest that you format your document automatically in Visual Studio to make the braces line up. This should make the error more obvious. When the formatting of the code looks wrong you know that there is an error nearby. You can find this option in the menu: Edit -> Advanced -> Format Document or use the keyboard shortcut (Ctrl-E D for me, but might be different for you, depending on your settings).
I would also suggest that you consider using auto-implemented properties instead of writing the getter out in full:
namespace ConsoleApplication2
{
class Program
{
class Car
{
public int Speed { get; private set; }
}
}
}
This should work:
class Foo
{
static int _abcd;
static int Abcd
{
get { return _abcd; }
set { _abcd = value; }
}
}