|
Just use this.GetType , which will give you the Type for the current instance. Compile and try the following example:
using System;
public class A
{
static void Main()
{
A a = new A();
Console.WriteLine(a.GetType());
a = new B();
Console.WriteLine(a.GetType());
}
}
public class B : A
{
}
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Ok,
To explain again, because I've obviously not been properly clear.
1. I am not *instansiating* Class B from A. I am trying to find the Type of object while calling a method from A that has not been overridden by derived class B.
e.g. If I call A.PrintType() I should get A, if I call B.PrintType(), without implementing PrintType() in the derived class B - i.e. I'm falling back to A.PrintType(), I want to get "B" as the output.
2. The solution must allow for use with statics... "this.GetType()" is right out.
I've been fiddling with all sorts of System.Reflection bits without success
Any other ideas?
Cheers.
Joe.
|
|
|
|
|
And as I've just realised, this isn't possible at all with static members.
The compiler only emits the static member of the base class.
So therefore
B.PrintType()
is actually mapped directly to
A.PrintType()
in my example. Therefore PrintType can never know that it is being called from derived class B.
DOH. Back to the drawingboard.
Thanks anyway.
joe.
|
|
|
|
|
You should read a little more about polymorphism. Even if B doesn't override PrintType , this is still an instance of B .
Just modify the sample and you'll see:
using System;
public class A
{
static void Main()
{
A a = new A();
a.PrintType();
a = new B();
a.PrintType();
}
protected void PrintType()
{
Console.WriteLine(this.GetType());
}
}
public class B : A
{
} For statics, there is no this reference so you can't do it this way. It's this (which is implicit, so you don't need to type this ) that is the correct Type.
Statics are defined on a particular Type and even if you used StackTrace you would see the static member called on the defining Type. Reflection won't help.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
You should read a little more about polymorphism. Even if B doesn't override PrintType, this is still an instance of B.
Sorry, I'm well aware of how polymorphism works thank you.
I was aware that your sample would work. My point was, it didn't answer my immediate question.
As I pointed out (and you subsequently re-iterated), of course what I was looking to do would not work in the context of statics since they are emitted once in the context of the declaring class.
I will instead approach the problem from a different angle.
Thanks
joev.
|
|
|
|
|
Hey there!
I have a problem with a Dialog in a textBox
I want to mark special lines in a different color. Is that possible or do i have to take a listBox? And if, then how is the code for changing single lines color!?
thanks
|
|
|
|
|
For a listbox, the color is for all text within the context. So that won't accomplish what you want to do. The textBox is the same. I would think that what you want to work with is a RichTextBox as your vehicle for text display. Then to set color your would do the following:
myRTB.Text = "some text\n\nsome more text\n"
myRTB.SelectedColor = Color.Red;
myRTB.SelectedText+= "colored text";
myRTB.Text += "end of message.\n"
I am just doing this off by memory, so you may need to do a bit of peeking at the Intellisense to get the correct selected field names.
|
|
|
|
|
thanks a lot
|
|
|
|
|
I want to make a DLL (interface) of which many implementations are possible. Now after I create the class file do I have to compile,
and do I have give the using namespace in the beginning
and can I have the interface & it's implementation in the same project, or should it be different projects.
Thanx.
MaXx
|
|
|
|
|
Anything you create you should compile to build the assembly.
You can have the interface and implementation in the same project.
Using key word is used if you want to referenced any dlls and there onwards you want to access any classes.
Hope you understood.....
|
|
|
|
|
If you have the interface and the class in the same project then you defeat the purpose of the interface ... which is to be a portable representation of that implementation.
When you create your interface you would compile it.
In the classes that implement it, you would include reference to the DLL. I don't remember if I added the using statement, but a quick compile will answer that question for you. (If the compile says you are missing a using then add the using) ... my VS build is temporarily broken to get a specific answer, but creating reference, adding the interface implementation and then doing the compile is how you discover your own answers.
The major advantage to the use of interfaces is that I don't have to know what implementation I'm working with. So if I have an IAmFunky interface and there are 20 implementations. My system just has to refer to the implemenations via the interface IAmFunky myObject = new IAmFunky();
______________________________
The Tao gave birth to machine language.
Machine language gave birth to the assembler.
The assembler gave birth to ten thousand languages.
Each language has its purpose, however humble.
Each language expresses the Yin and Yang of software.
Each language has its place within the Tao.
Beauty exists because we give a name to C#.
Bad exists because we give a name to COBOL.
|
|
|
|
|
ok thanx bro..
but a little more..
here I go
Now I have made an interface called Inter Then I made its dll from the command line saying ...>csc /t: library Int.cs ten I made a class call Impl and implemented a function called fun (it just returns an integer) then when I run it through VS it ran but after I used using... and made an object of the interface.
fine..
Now I want to know if I can have more than one interface in a namespace, and is a namespace something like a package in Java.
And if I have classes divided in different namespaces, can I use them in one main class with the one main function? (ofcource I may have to use the using..)
.
Believe! Everything has a purpose
|
|
|
|
|
saud_a_k wrote:
can have more than one interface in a namespace
You can have as many interfaces inside a namespace as you want. There exists no restriction.
Namespace are used to group classes, interfaces, enumeration,... into different scopes which helps to logically organize your code. The Framework Class Library does the same, as it includes many namespaces which contain implementations for a specific area.
saud_a_k wrote:
And if I have classes divided in different namespaces, can I use them in one main class
Whether you can use the types created inside your namespace depends on their access modifier and nothing else. You dont't even have to insert a using namespace , cause this is mainly an easement so you can use your types without having to specify the namespace.
Take a look at the documentation for the using directive[^] and the namespace keyword[^].
www.troschuetz.de
|
|
|
|
|
so you mean namespaces are like packages in Java.. right?
MaXx
|
|
|
|
|
Yes, I would say it's mainly the same.
But I'm only a bloody beginner in Java, so I don't take this for granted
www.troschuetz.de
|
|
|
|
|
I seem to vaguely recall from my UML class that a java package is really comparable to a component. In .NET a component is a number of classes grouped together to form common functionality. A namespace represents a broader aspect which may have no true Java equivalency.
A standard for namespace standardization is:
companyName.{application}
in my company I established the standard that our namespace standard is:
companyName.groupName.application
The application namespace represents all elements, classes, methods, UI that represent that application. Within that application I may have a component (just a really large class) which is made up of several class libraries. An example is an SCCProviderClass. This provides a common interface to our source management providers with Source Safe, PVCS, and CVS supported in the backside. An interface (ISourcePackage) is the interface between my API and the individual API's.
For interface naming conventions you should also try to identify what you are interfacing (ComponentLibrary for example which is a library of common components) and then append I to that. (IComponentLibrary) which I believe is a common naming convention in Java as well.
______________________________
The Tao gave birth to machine language.
Machine language gave birth to the assembler.
The assembler gave birth to ten thousand languages.
Each language has its purpose, however humble.
Each language expresses the Yin and Yang of software.
Each language has its place within the Tao.
Beauty exists because we give a name to C#.
Bad exists because we give a name to COBOL.
|
|
|
|
|
Is there anyone who can help me out to add datepicker control in a datagrid in window forms. Please tell me the code or send me the component file
|
|
|
|
|
|
hello...
I want to print some text, I have it in a RichTextBox!
I dont know how to do it...
However i tried this...
PrintPreviewDialog Print = new PrintPreviewDialog();
Print.Document = richTextBox1.Text;
Print.ShowDialog();
But it can't convert string to PringDocument!
I also tried....
PrintDocument preview = new PrintDocument();
PrintPreviewDialog Print = new PrintPreviewDialog();
Print.Document = preview ;
Print.ShowDialog();
then it works, but the paper is blank!
I don't know what else to do!
so please help me!
|
|
|
|
|
If you are using visual studio use the designer to add a PrintDocument object to your form.
In your function that calls the print event put the following
try<br />
{<br />
PrintPreviewDialog preview = new PrintPreviewDialog();<br />
preview.Document = printDocument1;<br />
preview.ShowDialog();<br />
if (preview.DialogResult == DialogResult.OK)<br />
printDocument1.Print();<br />
}
In the printDocument1.Print event (this can be created by ouble clicking the printDocument1 object in the designer) write the following code
e.Graphics.DrawString(richTextBox1.text, myFont, Brushes.Black,<br />
LeftMargin, YPosition, new StringFormat());
You will have to provide the values for myFont, LeftMargin and YPosition .
Hope this helps
KP
|
|
|
|
|
thanks it worked....
But do you know what the best LeftMargin and Yposition will be so it will print all the text?
My printer is currently broke so i can't test what would be best!
if U have time please write back else thanks anyway!
|
|
|
|
|
I've not reall y had much experience printing although my current project is going to require me to do quite a bit as soon as i overcome my current problem but i,ll try to help.
If you set LeftMargin equal to e.MarginBounds.Left
and Yposition equal to e.MarginBounds.Top
The printing will start in the top left corner of the page, taking into account any changes you make to the margins in the printPreviewDialog when it is displayed.
You may also have to write code to work out the number of lines on the page compared to how many lines your printing. If you get to the end of the page just set the e.HasMorePages = true and finish the printDocument.Print event, it will then start a new page (i think).
Sorry i can't give you any definate answers but like i said, i'm just starting to look into printing myself.
Hope you sort your problem.
KP
|
|
|
|
|
okay ill try it.... or else i wait to my printer works again!
Thanks!
|
|
|
|
|
With a bit of help from Heath Stewart i have a programme up and running with a datagrid, the first column of which is a check box.
What i now need is to be able to lock the grid down except for the check box column, I could just set each column to read only but this still allows the user to create new rows. New rows are added programatically and i need to restrict the user from doing this.
Any help you guys can give me would be appreciated.
Cheers
KP
|
|
|
|
|
I can't remember the exact details but you can do it with the dataview.allowNew method
|
|
|
|