473,651 Members | 2,536 Online
Bytes | Software Development & Data Engineering Community
+ 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: ConsoleApplicat ion1.Class1
name of Class2 is: ConsoleApplicat ion1.CMain+Clas s2

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 ConsoleApplicat ion1
{
class Class1{}

class CMain
{
class Class2{}

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

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

Console.WriteLi ne();
Console.ReadLin e();
}
}
}
Nov 13 '05 #1
1 3371
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************* *@exisconsultin g.com

"vector" <is****@yahoo.c om> wrote in message
news:79******** *************** ***@posting.goo gle.com...
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: ConsoleApplicat ion1.Class1
name of Class2 is: ConsoleApplicat ion1.CMain+Clas s2

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 ConsoleApplicat ion1
{
class Class1{}

class CMain
{
class Class2{}

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

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

Console.WriteLi ne();
Console.ReadLin e();
}
}
}

Nov 13 '05 #2

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

Similar topics

8
1526
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 be something really stupid but I've been looking at it too long I guess.
5
7447
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 used Object class as parameter. Now it can take any object as its parameter. But the problem is that I want to access the values of the private or public member variables of the passed object, for which I may have to typecast the Object class...
0
1697
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 the record, a preallocated static buffer size will have to be declared beforehand in the declaration section. Since the record sizes vary a lot, instead of using the static buffer, is there a dynamic way to fetch these records? Informix (Oracle...
2
1397
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 receive the field properties name, max_length, type not_null, has_default and defaul_value. This is achieved by using functions like oci_field_name(). But I am not able to request the information on if the field is primary key or marked as unique...
1
2271
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 updated but what happen is: form is postedback but no change happen to database and also textboxes return to there original state i.e previous record(enableviewstate is false/same result with true). small code is here:...
22
2695
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 things. So, in instances where I list, for example, the latest weblogs. I list the headline of the weblog, the date and the name of the member who wrote it. Now, the name isn't just "Smith", but rather Smith's online status, his nick and his...
5
2973
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 TO TABLE, like storing IMAGE into DB.
0
2305
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
4265
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
8347
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8792
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8571
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7294
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6157
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5605
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4280
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.