|
You don't need reflection. The Enum class provides you with methods to get all information you nead about the enum.
Use the GetNames method to get an array of all the names. If you need their values, use the GetValues method to get an array with all the values.
---
b { font-weight: normal; }
|
|
|
|
|
If it is One Enum then your suggestions is OK. I can use Enum.GetNames(enum), But My situation is likes a structure which will contain lot of enums(say 100) as it member) so i can't write 100's lines of code to display every member
ie.
enum1<br />
{<br />
a=0,<br />
b,<br />
c<br />
}<br />
..<br />
enum100<br />
{<br />
x=0,<br />
y,<br />
z<br />
}<br />
<br />
Struct st<br />
{<br />
enum1 e1;<br />
enum2 e2;<br />
..<br />
enum100 e100;<br />
};<br />
<br />
Enum.GetNames(st.e1);<br />
..<br />
Enum.GetNames(st.e100);
Instead Puting like the above
I am thinking of reflection to reduce the line of code
Like this
Type ty = st.GetType();<br />
FieldInfo[] fin= ty.GetFields();<br />
foreach (FieldInfo fino in fin)<br />
{<br />
fino.GetValue(st).ToString();<br />
}
But This print the enum1..enum100 values like 0 0 0 0 ..
but I want to print the enum member names like a b c ...
Hope I Explained my question Clearyly
Regards,
Srini
|
|
|
|
|
I see. Try this, then:
Enum.GetName(fino.FieldType, fino.GetValue(st))
---
b { font-weight: normal; }
|
|
|
|
|
Exactly thats what i want, i have used the same code but it throws me me ArgumentException, i think since the enum is developed in VC++ .net fino.FieldType doesn't match with System.Enum Type in C#(since we are using Enum.GetName).. I think so.
Try it out and if anything works please share with me.
Regards,
Srini
|
|
|
|
|
Have you tried it out? It's not the same code as you showed in your first post.
---
b { font-weight: normal; }
|
|
|
|
|
Yes I tried out. it throws me the ArgumentException.
ArgumentException - Description given in the MSDN help
enumType is not an Enum.
-or-
value is neither of type enumType nor does it have the same underlying type as enumType.
Enum in VC++ .net and VC# .net are they different?
|
|
|
|
|
Even That is code i have mentioned in my initial Conversation.
|
|
|
|
|
No, it's not. There you used a Type object instead of a FieldInfo object.
---
b { font-weight: normal; }
|
|
|
|
|
Have you tried using the Enum struct? Once you have the type of enum you can use the static methods of the Enum struct to obtain information about the enum type.
chriswa
|
|
|
|
|
I got it!!!!
Thanks Chriswa its working as per your suggestion. i have declared the enum as enum class (ie enum struct) in VC++ dll. then i can able to get the enum member names.
If i declare the enum using enum in VC++, then i am not able to retrieve the names.
What is the difference between enum and enum struct
I extend my thanks to Guffa.
Regards,
Srini
|
|
|
|
|
How does one get the current date and time in C#. Is there any readymade class.
|
|
|
|
|
deepscyberpulse wrote: How does one get the current date and time in C#. Is there any readymade class.
DateTime.Now
Share and enjoy.
Sean
|
|
|
|
|
I think that this is what u want.
DateTime d = new DateTime();
MessageBox.Show(d.ToShortDateString());
|
|
|
|
|
Hi, you can use this:
label1.Text = DateTime.Now.ToLongDateString(); //date
label2.Text = DateTime.Now.ToLongTimeString(); //time
Regards,
Alberto Martinez
|
|
|
|
|
Hello
I am working on an application in c# i want to develop a sofware that block the any address that is contain in the database of my appliction. i.e if i have www.yahoo.com in my database and user enter www.yahoo.com in the address bar of the Internet Explorer this site should not be open.
If any one has worked on it or have idea about it plz le me know.
|
|
|
|
|
This sounds like a task better suited for native code. To block addresses on the client machine, you're going to have to write some native hooks into the OS to truely prevent this. If you're just blocking Internet Explorer navigation, you could write hooks or a plugin for IE that blocks the site. Either way, definitely not a task for C# and .NET.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Moral Muscle
The apostle Paul, modernly speaking: Epistles of Paul
Judah Himango
|
|
|
|
|
Judah Himango wrote: definitely not a task for C# and .NET.
Why not?
I agree though that native code is better, but you can accomplish the same thing in .NET
Ed
|
|
|
|
|
I would agree. In fact this sounds like something that you would want to do in ASP.NET of you are blocking the address from the server. It not actually clear in the grandparent that this has to be done on the client. If this has to be done on the client then you can build a plug in from .NET using COM/Interop or build an IE plug-in from COM. It is easier at times to use .NET to write a COM plug-in
chriswa
|
|
|
|
|
ASP.NET? It'd be possible but I'd have thought I'd be better to have a service running on the server / client which interops with the Win32 API.
You notice that's how most firewalls do it
The only major difference would be that it would be written in .NET with COM Interop rather than in native code.
Ed
|
|
|
|
|
Hi all
i have displayed a CSV file (coma separated) in a richtextbox,
but now need to put it into an Ilist
Anyone got any ideas ??
Thanks lots
John
|
|
|
|
|
Search this site for CSV parsers. There are many out there freely available.
|
|
|
|
|
What would be the equivalent in C# for the following?
Sub CreateADODB()<br />
Dim adoApp As Object<br />
adoApp = CreateObject("ADODB.Connection")<br />
End Sub<br />
Is there a site where you can enter functions of VB and see what the C# (and/or other languages) are?
thanks!
No hurries, no worries.
|
|
|
|
|
This is simple and tough to answer - so here goes.
Simple: ADODB.COnnection was replaced by OleDbConnection in the System.Data assembly
Tough: CreateObject was used in VB and VBScript in create an instance of a COM compatible object. ADODB.Connection was a very commonly used COM Object for connecting to a variet of databases. .NET, and therefore C#, has much interoperability with COM if you did want to look that up and use the, now mostly defunk, ADODB COM libraries.
|
|
|
|
|
actually it was the CreateObject I needed, the ADO stuff was just an example.
I need to use a COM object and in VB they've used a CreateObject to call it.
I've tried via the tlb and exe file, but I get an errormessage. Now I thought to use the same code they've used in VB, but in C#.
thanks a lot!
[EDIT]I've found a little exe here in CP, but it isn't wat I need...[/EDIT]
No hurries, no worries.
-- modified at 8:23 Friday 17th March, 2006
|
|
|
|
|
Ah, in which case if you are using Visal Studio, just add a COM reference to the project (right click references>Add reference>COM Tab>Browse) and VS will do the rest for you, allowing you to call the COM object as if it were a .NET class. Visual Studio just does some boiler-plating for you.
eg, if I did this with ADODB.COnnection I could go
ADODB.ConnectionClass myConn = new ADODB.ConnectionClass();
note it adds the suffix "Class" to the COM Object's true name.
|
|
|
|