|
Console.WriteLine(111.GetType().ToString()) gives System.Int32
Pretty obvious, I'd say.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
How to make TabPage in TabControl invisible?
TabConltrol1.TabPages(0).Visible=False ;
|
|
|
|
|
The tabs in a TabControl are part of the TabControl, but not parts of the individual TabPage controls. Members of the TabPage class, such as the ForeColor property, affect only the client rectangle of the tab page, but not the tabs. Additionally, the Hide method of the TabPage will not hide the tab. To hide the tab, you must remove the TabPage control from the TabControl.TabPages collection.
For reference, have a look here.
My signature "sucks" today
|
|
|
|
|
Hello
Can one access methods of a derived class?
e.g.:
public class GameObject { ... }
public class Chest : GameObject
{
public void OpenChest();
public void CloseChest();
}
GameObject chest = new Chest();
chest.OpenChest();
It seems the only GameObject members that appear are those from the base class GameObject.
The methods from Chest are apparently not accessible by polymorphism context.
You can only access Chest's methods by explicitly casting my GameObject type (chest)
to a Chest type, i.e.:
(Chest)chest; chest.OpenChest();
Is there any other way to do this other than casting it explicitly to a Chest type?
I would like to know the reasons why the compiler works this way.
Thank you.
|
|
|
|
|
Because you have declared chest as a GameObject, it can hold a GameObject or any item derived from GameObject.
But because you declared it as a GameObject, the compiler assumes it is the most basic of the control family, and will only allow you to access the GameObject methods, properties etc.
If you had declared it as a Chest, you could access all the chest methods.
Think about it:
public class GameObject { ... }
public class Chest : GameObject {...}
public class TresureChest: GameObject {...}
...
public void PlayWithObject(GameObject go)
{
...
} You can provide a GameObject, Chest, or TreasureChest as a parameter to PlayWithObject, but the compiler does not know what type go will end up being. If it allowed you you use methods from Chest, and you handed it a TreasureChest, what would happen?
Instead, you have to provide runtime checking:
Chest ch = go as Chest;
if (ch != null)
{
ch.OpenChest();
}
Did you know:
That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
|
|
|
|
|
You might want to look at the new modifier.
My signature "sucks" today
modified on Tuesday, May 25, 2010 12:25 PM
|
|
|
|
|
CsTreval wrote: Is there any other way to do this other than casting it explicitly to a Chest type?
Yes, IF those methods also exist as part of the base class. For example:
public class ABC
{
public virtual string DoIt()
{
return "ABC";
}
}
public class XYZ : ABC
{
public override string DoIt()
{
return "XYZ";
}
}
ABC a = new XYZ();
string str = a.DoIt();
Also, you can technically use reflection to access the methods of any class without even knowing the name of the method. However, that is an advanced topic and not likely what you are looking for.
|
|
|
|
|
Thanks, I already know that topic. I do have some years of programming experience and theory, though. OO (2), Java(3), VB .NET(2), C#(2). Well, I've heard in the past that reflection is not recommended to use since it would not be ideal OO. I think I will do just fine with Chest ch = new Chest(). I didn't realize before that Chest can have GameObject's properties etc. However, why can't you do
Chest ch = new GameObject ? I know the reason but I don't know precisely why.
|
|
|
|
|
Here's some invalid C# for you:
Duck d = new Animal();
d.Quack();
d = new Cat();
d.Quack();
Does that clear things up?
|
|
|
|
|
Because Chest is derived from GameObject, it contains everything that GameObject does. However, the converse is not true: Chest contains stuff that GameObject does not - or there would not be any point in creating it. The framework cannot "invent" that stuff - it does not know what values to use! Hence, you cannot cast a class to a class derived from it.
It would be like having a blank book, and saying that "A Brief History of Time" by Stephen Hawking can be created from it just by adding characters. Yes, in theory it could, but the chances of getting is right are incredibly small!
Did you know:
That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
|
|
|
|
|
You could try a different design. Try e.g.
public class GameObject
{
public virtual void OpenGame() {...}
public virtual void CloseGame() {...}
}
Then overwrite these functions in your derived class.
|
|
|
|
|
The problem is, it's not correct OO to write non-related methods in a baseclass, since the base class is only to contain the basic methods and the subclasses contain their own methods, which are used only for them. Chest.Open() belongs to Chest. You can't open a Coin for example. Maybe I should reconsider my design, I don't know. Currently, a GameObject is something laying on the game world floor. However, I also use GameObject to simply put non-world-interactive sprites on the screen. Perhaps I could make an IPickupable and so on..
|
|
|
|
|
Then you could use an enum defining all possible actions for any game object. The base class has an abstract property for the allowed actions, and every derived class would return the actions valid for that class. Then you could call a function public void PerformAction(enum myAction) on your (derived) game objects (that function is abstract in the base class).
|
|
|
|
|
Hi all,
I just recently stepped into a strange problem.
I am making async calls to a method that scans files for "strange stuff"
public delegate HandleCheckResult(long id);
private void GetResult(long id)
{
System.Threading.AutoResetEvent ev = new System.Threading.AutoResetEvent(true);
HandleVirusCheckResult proc = Scan;
IAsyncResult async = proc.BeginInvoke(id, HandleResult, ev);
ev.WaitOne();
}
private void HandleResult(IAsyncResult async)
{
HandleCheckResult proc = (HandleCheckResult)((System.Runtime.Remoting.Messaging.AsyncResult)async).AsyncDelegate;
retVal = proc.EndInvoke(async);
((System.Threading.AutoResetEvent)async.AsyncState).Set();
}
It looks somehow like the code fragment above.
Everything works fine in my win-form application, but now I ported it to a Windows Service and get ArgumentOutOfRangeException on the EndInvoke method...
(index out of range, should not be negative and smaller than the collection)
If anybody could be so kind and point me into a direction... I am somehow lost right now...
Thanks in advance,
cheers
Hoernchenmeister
|
|
|
|
|
Hi,
When an exception is thrown on the worker thread started with BeginInvoke it is trapped and rethrown when EndInvoke is called. The exception will be happening somewhere in your Scan method and the first thing to do is find out where. Wrap the EndInvoke in a try..catch block and dump the full text of the exception out to a file, e.g. System.IO.File.WriteAllText("log.txt", ex.ToString()).
The stack trace will contain the line number where the exception was thrown and get you started on finding out what went wrong.
Alan.
modified on Tuesday, May 25, 2010 1:02 PM
|
|
|
|
|
Thanks a ton Alan,
that was exactly what I was looking for
best regards
Andy
|
|
|
|
|
Hi
I have a code as below
public class classname{
// first method
public bool methodtest()
{
string inputcallmethod = "methodtest2";
bool blnBurstsuccess = true;
Type testcall = typeof(classname);
object obj = System.Activator.CreateInstance(testcall);
string[] strnparam = new string[3]{this.par1, this.par2, this.par3 };
try
{
blnBurstsuccess = (bool)testcall.InvokeMember(inputcallmethod, BindingFlags.Public |BindingFlags.Instance | BindingFlags.InvokeMethod, null, obj, strnparam);
}
catch (Exception e)
{
blnBurstsuccess = false;
}
return blnBurstsuccess;
}
//second method
public bool methodtest2(string[] strparam)
{
--- some function
return bool varibale
}
}
This code returns an error as the "method methodtest2 not found".
I am not able to figure out the reason.While debugging, the try part is not executed.
Thanks,
|
|
|
|
|
Hi,
There is one small error which is not obvious.
Instead of
string[] strnparam = new string[]{this.par1, this.par2, this.par3 };
do this
string[] strnparam = new string[]{this.par1, this.par2, this.par3 );
object[] args = new object[] { strnparam };
and pass the args array to Type.InvokeMember. This will make it look for a method with one array parameter. Passing strnparam directly was making it look for a method with three parameters.
Alan.
|
|
|
|
|
what does object initializer do?? I mean, when we use it using new key word, then constructor should be called, but it is written on the msdn site that constructor is not called. If constructor is not called then how does the values are assigned?? What exactly the story is??
|
|
|
|
|
Muzammil Saeed wrote: but it is written on the msdn site that constructor is not called.
Where did you see that? If you look here[^] it states explicitly that the constructor is called.
It's time for a new signature.
|
|
|
|
|
Richard MacCutchan wrote: Where did you see that? If you look here[^] it states explicitly that the constructor is called.
If you look here[^] it says:
Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to explicitly invoke a constructor.
I think this is what is confusing the OP.
|
|
|
|
|
David Skelly wrote: I think this is what is confusing the OP.
And yours truly.
It's time for a new signature.
|
|
|
|
|
Keyword "explicitly"; I guess it would call the default constructor and populate the properties right after that.
I are Troll
|
|
|
|
|
Exactly right, it takes a couple of readings to recognise this.
It's time for a new signature.
|
|
|
|
|
Can you post a link to where you found this?
My signature "sucks" today
|
|
|
|