Hey, programming.
Lets say I have this code:
[cpp]
namespace Test {
class Class1 {
}
class Class2 : Class1 {
String name = "Bobby";
}
class Class3 {
public Class3() {
Class2 c2 = new Class2();
doSomething(c2);
}
private doSomething(Class1 class) {
// FIND THAT CLASS IS CLASS2
MessageBox.Show(class.Name);
}
}
}
[/cpp]
I need to know 100% for sure that the class variable in doSomething is a Class2. Then I want to make a new variable of Class2, as long as class == Class2.
if ( class is Class2 )
[QUOTE=foszor;25713391]if ( class is Class2 )[/QUOTE]
Thats a start, but lets say I don't know what class I want.
For example, if I have a list of dynamically generated buttons, which have an instance of the class I want, that need to clone that class.
[editline]28th October 2010[/editline]
Note: not Windows.Forms buttons.
[QUOTE=i300;25713462]Thats a start, but lets say I don't know what class I want.
For example, if I have a list of dynamically generated buttons, which have an instance of the class I want, that need to clone that class.
[editline]28th October 2010[/editline]
Note: not Windows.Forms buttons.[/QUOTE]
After reading it a few times: You have a list of classes ('button'), and I'm assuming one of them is actually a class that derives from button, and you want to clone that class?
[cpp]ButtonDerivative result = listofbuttons.OfType<ButtonDerivative>().FirstOrDefault();
listofbuttons.Add(result .CloneMe());[/cpp]
Fixed it.
[editline]28th October 2010[/editline]
Now lets say I have a variable. I want to COPY it (not reference it) to another variable.
[QUOTE=i300;25715649]Fixed it.
[editline]28th October 2010[/editline]
Now lets say I have a variable. I want to COPY it (not reference it) to another variable.[/QUOTE]
What type is the variable?
[QUOTE=ShaRose;25716797]What type is the variable?[/QUOTE]
SilkApplication, A custom class.
It needs to implement ICloneable
[editline]29th October 2010[/editline]
Then you just pull a:
[csharp]
var a = new MyClass();
var b = (MyClass)a.Clone();
[/csharp]
[editline]29th October 2010[/editline]
a and b are now different objects.
[editline]29th October 2010[/editline]
Keep in mind, there's a shallow copy and a deep copy. Shallow copies just clone the object, but all references held by the object are the same. Deep copy copies everything.
It's quite a complex problem, actually.
Thats the only thing I dislike about C#. Even in Objective-C, var a = bluh; var b = [a copy]; or [a deepCopy];
C# makes it so complex.
[QUOTE=i300;25718283]Thats the only thing I dislike about C#. Even in Objective-C, var a = bluh; var b = [a copy]; or [a deepCopy];
C# makes it so complex.[/QUOTE]
Keeping a reference by default is actually useful in a lot of areas. For one, it speeds things up and keeps memory usage down.
[QUOTE=ShaRose;25718722]Keeping a reference by default is actually useful in a lot of areas. For one, it speeds things up and keeps memory usage down.[/QUOTE]
It just sort of makes no sense if you've gotten used to C-like languages. If I started programming in C#, I would've expected that default functionality to be shallow copying.
Then again, for example in Lua all userdata and table variables are always references and copying has to be implemented by oneself (Which kinda makes sense for userdata, though)
[QUOTE=esalaka;25721743]I would've expected that default functionality to be shallow copying.[/QUOTE]
You're supposed to use structs for that.
[QUOTE=jA_cOp;25722131]You're supposed to use structs for that.[/QUOTE]
For copying objects?
[QUOTE=esalaka;25723727]For copying objects?[/QUOTE]
The default functionality for structs in C# is shallow copy when passed around or assigned.
[QUOTE=jA_cOp;25723816]The default functionality for structs in C# is shallow copy when passed around or assigned.[/QUOTE]
But... I meant I would expect the default functionality to be copy for any and every object!
[QUOTE=esalaka;25725459]But... I meant I would expect the default functionality to be copy for any and every object![/QUOTE]
That's not allowed mainly because of the slicing problem. Copy semantics and inheritance don't go well together in a memory-safe language.
Classes cover most use cases for objects in C#, structs cover those where copy semantics are useful.
[QUOTE=ShaRose;25718722]Keeping a reference by default is actually useful in a lot of areas. For one, it speeds things up and keeps memory usage down.[/QUOTE]
Yeah, its nice to have a reference, but in this case I want to be opening a new instance of this class.
[editline]29th October 2010[/editline]
My original question hasn't been answered.
I want to take the SilkApplication class and find which subclass of SilkApplication it is (without knowing the subclasses) and make a new instance of that subclass.
[QUOTE=i300;25732124]Yeah, its nice to have a reference, but in this case I want to be opening a new instance of this class.
[editline]29th October 2010[/editline]
My original question hasn't been answered.
I want to take the SilkApplication class and find which subclass of SilkApplication it is (without knowing the subclasses) and make a new instance of that subclass.[/QUOTE]
That's going into reflection. At least if I understand you correctly.
[cpp] static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
AClass myclassref = new CClass(); // CClass is derived from BClass which is derived from AClass
dynamic mynewclassref = Activator.CreateInstance(myclassref.GetType()); // Bam! A new CClass. Note that the dynamic keyword is .net 4 or higher only.
Console.WriteLine(mynewclassref.CString); // Will print 'CClass'
}
public class AClass
{
public string AString = "AClass";
}
public class BClass : AClass
{
public string BString = "BClass";
}
public class CClass : BClass
{
public string CString = "CClass";
}
}[/cpp]
It's probably handy to add that all classes override GetType(), so if you have an object of type SubClass in a variable of type BaseClass, GetType() will still return typeof(SubClass)
[QUOTE=Siemens;25734624]It's probably handy to add that all classes override GetType(), so if you have an object of type SubClass in a variable of type BaseClass, GetType() will still return typeof(SubClass)[/QUOTE]
Actually, they don't. GetType is a call into the internal runtime. Just open it up in reflector and you will see the following:
[code][MethodImpl(MethodImplOptions.InternalCall)]
public extern Type GetType();[/code]
[QUOTE=ShaRose;25738724]Actually, they don't. GetType is a call into the internal runtime. Just open it up in reflector and you will see the following:
[code][MethodImpl(MethodImplOptions.InternalCall)]
public extern Type GetType();[/code][/QUOTE]
Oh cool.
I didn't know the specifics of it, but I assumed GetType was implemented as a special override because it returns a different value for different types.
Sorry, you need to Log In to post a reply to this thread.