- Home /
Custom Class Type Casting (to string)
How can I format a typecasting function in my custom class so that when I pass it off as another parameter type, say string, it uses the function to return an instance of the new type? I'm guessing it is something like
static public string...something something?
Edit: The custom class is essentially a string holder, with a name and other flags
public class Datum
{
public string name;
public string value;
public string type;
this has a few unnecessary variables omitted. It would be great if I could use simplified code to do things like the following: Datum datumExample = "null"; // Every variable is initialized to the string "null"
// or
void aFunction(Datum a = "null"){};
aFunction();
// or
void aFunction(string a = "null"){};
aFunction(datumExample); // Where datumExample automatically returns it's
// .value variable
You are talking about two separate things. In terms of initialization, the most obvious way is just initialize all your members in the constructor:
public class Datum
{
public Datum(string init)
{
name = init;
value = init;
}
public string name;
public string value;
}
Datum d = new Datum("initialValue");
I would also avoid using a string called "null" since that seems counter-intuitive (by assigning the string value "null" the string itself is not null). I would simply either set it to actual null, or string.Empty is a safer bet to avoid null reference exceptions.
For the second part when you want your class to have a string representation, just override the ToString() method that is available on all objects and return your value member.
class Datum()
{
public override string ToString()
{
return value;
}
}
Answer by Socapex · Oct 26, 2015 at 09:36 PM
If you do not inherit MonoBehavior, than you can add a simple default constructor, or parameter constructor:
public class Datum
{
public string name;
public string val;
public string type;
public Datum() {
name = "null";
val = "null";
type = "null";
}
}
//or
public class Datum
{
public string name;
public string val;
public string type;
public Datum(string blee) {
name = blee;
val = blee;
type = blee;
}
}
Then you can do Datum blou = new Datum();
or Datum blaa = new Datum("null");
In this case:
void aFunction(string a = "null"){};
aFunction(datumExample); // Where datumExample automatically returns it's
// .value variable
Why not aFunction(datumExample.value);
if it is public, or a simple getter aFunction(datumExample.getValue());
if it is private?
KISS :) Hope this helps.
[edit] I agree with @NewPath that using "null" is a little strange...
Your answer
Follow this Question
Related Questions
Force typcasting of member objects in a List? (Boo) 1 Answer
CustomAttribute on Class or Method ( EDIT: Button to call methods) 2 Answers
Help removing a Custom Class from a List in UnityScript 1 Answer
How can I get functionality similar to OnValidate for a custom class? 4 Answers
Please Help! :) Loading assetBundle Animations 4.0.0 -> 4.0.1 broke my scene 1 Answer