473,473 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Fetching the name of a class

There's got to be a better way to do this.

In this sample code, I've created a class in a namespace, and a class
inside of a class. On calling .ToString() for those two classes, I
see different output:

name of Class1 is: ConsoleApplication1.Class1
name of Class2 is: ConsoleApplication1.CMain+Class2

I'm not interested in the hierarchical name of the class. All I
really want from it is "Class1" and "Class2". I could do some string
manipulation, for instance scanning from right to left for non-alpha
and non-numeric characters, but I'd like to think there's a more
intelligent way to derive only what I want without resorting to stupid
string tricks.

Can someone point me in the right direction?

using System;

namespace ConsoleApplication1
{
class Class1{}

class CMain
{
class Class2{}

static private string GetObjectName(object obj)
{
return obj.ToString(); //ugly. fix this.
}

[STAThread]
static void Main(string[] args)
{
Console.WriteLine("name of Class1 is: {0}", GetObjectName(new
Class1()));
Console.WriteLine("name of Class2 is: {0}", GetObjectName(new
Class2()));

Console.WriteLine();
Console.ReadLine();
}
}
}
Nov 13 '05 #1
1 3363
vector,

The ToString method is not guaranteed to give you the class name when
you call it, so you should not be using it.

You should use in your GetObjectName method "obj.GetType().Name".

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"vector" <is****@yahoo.com> wrote in message
news:79**************************@posting.google.c om...
There's got to be a better way to do this.

In this sample code, I've created a class in a namespace, and a class
inside of a class. On calling .ToString() for those two classes, I
see different output:

name of Class1 is: ConsoleApplication1.Class1
name of Class2 is: ConsoleApplication1.CMain+Class2

I'm not interested in the hierarchical name of the class. All I
really want from it is "Class1" and "Class2". I could do some string
manipulation, for instance scanning from right to left for non-alpha
and non-numeric characters, but I'd like to think there's a more
intelligent way to derive only what I want without resorting to stupid
string tricks.

Can someone point me in the right direction?

using System;

namespace ConsoleApplication1
{
class Class1{}

class CMain
{
class Class2{}

static private string GetObjectName(object obj)
{
return obj.ToString(); //ugly. fix this.
}

[STAThread]
static void Main(string[] args)
{
Console.WriteLine("name of Class1 is: {0}", GetObjectName(new
Class1()));
Console.WriteLine("name of Class2 is: {0}", GetObjectName(new
Class2()));

Console.WriteLine();
Console.ReadLine();
}
}
}

Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Philippe C. Martin | last post by:
Hi, I'm getting pretty desperate here: The code below crashes on the last line (but works from a shell). The class 'BC' exists and the loop on self.__BC_EXEC_LIST passes fine. It's got to...
5
by: Ram | last post by:
Hi Friends I want to develope a custom control in .net which can be used with any project. I am writing a function in that class which I want to take any object as parameter. For that I have...
0
by: Shujun Huang | last post by:
Hi, I am working on converting Informix database to Postgre. I have one question for fetching records using PostgreSQL. The record I am fetching is a variable size text string. Before fetching...
2
by: Florian Albrecht | last post by:
Hi there, as I am trying to write a recordset class, I am interested in fetching field information from an oracle db. Several information on the fields are very simple to request. So I already...
1
by: nasirmajor | last post by:
Dear All, im fetching record from database using datareader, and is displaying them in the textboxes. when user change the text fields and then presses the update button the record should be...
22
by: Sandman | last post by:
So, I have this content management system I've developed myself. The system has a solid community part where members can register and then participate in forums, write weblogs and a ton of other...
5
by: Bhavesh | last post by:
Hello genious people, I m trying to insert a LARGE text from Multiline Textbox into my table of sqlserver2000. I m using vs-2005. Please note that I dont want to store blob data From FILE...
0
parshupooja
by: parshupooja | last post by:
Contact Reply 1 point Member propoo Joined on 08-31-2007, 10:32 PM Posts 3 Hey all ,
4
by: tokcy | last post by:
HI every one, i am using tooltip on click of link and i want like when that tooltip open then background window would be blure(). can anyone help me...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.