473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Can I get an array contining the fields of the DataFormat class.

DataFormat is a class who's fields are static strings.
I'd like a foreach loop that loops over those strings.

Actually any way to get a copy of all those strings into an array would
work.

Can you think of any way that could be done?

Thanks in advance




Jun 27 '08 #1
9 1218
AAaron123 wrote:
DataFormat is a class who's fields are static strings.
I'd like a foreach loop that loops over those strings.

Actually any way to get a copy of all those strings into an array would
work.

Can you think of any way that could be done?
For inspiration:

using System;
using System.Reflection;

namespace E
{
public class Foobar
{
public static string a = "A";
public static string b = "BB";
public static string c = "CCC";
}
public class Program
{
public static void Main(string[] args)
{
Foobar fb = new Foobar();
foreach(FieldInfo fi in fb.GetType().GetFields(BindingFlags.Public |
BindingFlags.Static))
{
Console.WriteLine(fi.GetValue(fb));
}
Console.ReadKey();
}
}
}

Arne
Jun 27 '08 #2
Arne Vajhøj wrote:
AAaron123 wrote:
>DataFormat is a class who's fields are static strings.
I'd like a foreach loop that loops over those strings.

Actually any way to get a copy of all those strings into an array
would work.

Can you think of any way that could be done?

For inspiration:
using System.Reflection;
foreach(FieldInfo fi in
fb.GetType().GetFields(BindingFlags.Public | BindingFlags.Static))
{
Console.WriteLine(fi.GetValue(fb));
}
Note that the use of reflection has an impact on performance.

You should consider based on your context.

Arne
Jun 27 '08 #3
Two things:

1) I dropped the "s". It should have been: DataFormats

2) I would appreciate it if you would look at the DataFormats doc and see if
you think your solution would work.
DataFormats seem to lack a constructor and the method GetType.

Thanks for your interest

"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:48***********************@news.sunsite.dk...
Arne Vajhøj wrote:
>AAaron123 wrote:
>>DataFormat is a class who's fields are static strings.
I'd like a foreach loop that loops over those strings.

Actually any way to get a copy of all those strings into an array would
work.

Can you think of any way that could be done?

For inspiration:
>using System.Reflection;
> foreach(FieldInfo fi in
fb.GetType().GetFields(BindingFlags.Public | BindingFlags.Static))
{
Console.WriteLine(fi.GetValue(fb));
}

Note that the use of reflection has an impact on performance.

You should consider based on your context.

Arne

Jun 27 '08 #4
AAaron123 wrote:
Two things:

1) I dropped the "s". It should have been: DataFormats

2) I would appreciate it if you would look at the DataFormats doc and see if
you think your solution would work.
DataFormats seem to lack a constructor and the method GetType.
All objects have a GetType method.

The code should work on all clases.

When I look at System.Windows.DataFormat I can not
see any fields, so the code would not make much sense.

Arne
Jun 27 '08 #5

"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:48***********************@news.sunsite.dk...
AAaron123 wrote:
>Two things:

1) I dropped the "s". It should have been: DataFormats

2) I would appreciate it if you would look at the DataFormats doc and see
if you think your solution would work.
DataFormats seem to lack a constructor and the method GetType.

All objects have a GetType method.
I thought so but in the DataFormats Members doc I see only one method,
GetDataFormat, which has 2 overloads.
>
The code should work on all clases.

When I look at System.Windows.DataFormat I can not
see any fields, so the code would not make much sense.
It's System.Windows.DataFormats.
Note the "s"
And note that all the fields are static
>
Arne
Thanks again
Jun 27 '08 #6
AAaron123 wrote:
"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:48***********************@news.sunsite.dk...
>AAaron123 wrote:
>>Two things:

1) I dropped the "s". It should have been: DataFormats

2) I would appreciate it if you would look at the DataFormats doc and see
if you think your solution would work.
DataFormats seem to lack a constructor and the method GetType.
All objects have a GetType method.
I thought so but in the DataFormats Members doc I see only one method,
GetDataFormat, which has 2 overloads.
>The code should work on all clases.

When I look at System.Windows.DataFormat I can not
see any fields, so the code would not make much sense.

It's System.Windows.DataFormats.
Note the "s"
And note that all the fields are static
Ah. It is a static class, so you do not have an object and therefore
no GetType.

using System;
using System.Reflection;
using System.Windows.Forms;

namespace E
{
public class Program
{
public static void Main(string[] args)
{
foreach(FieldInfo fi in
typeof(DataFormats).GetFields(BindingFlags.Public | BindingFlags.Static))
{
Console.WriteLine(fi.Name + "=" + fi.GetValue(null));
}
Console.ReadKey();
}
}
}

Arne
Jun 27 '08 #7
Thanks, works great.

Actually I made subs out of the two methods you showed (for npn-static and
for static classes).

They work well but it made me wonder if the class being investigate could be
brought in as a parameter. That is, can a parameter be subsituted for
DataFormats and for fb? Then I realized I have no idea how to do that.

Is that possible?
Thanks again

public static void StaticTmp()
{
foreach(FieldInfo fi in typeof(DataFormats).GetFields(BindingFlags.Public
| BindingFlags.Static))
{
Console.WriteLine(fi.Name + "=" + fi.GetValue(null));
}
}
public static void NonStaticTmp()
{
Foobar fb = new Foobar();
foreach(FieldInfo fi in fb.GetType().GetFields(BindingFlags.Public |
BindingFlags.Static))
{
Console.WriteLine(fi.GetValue(fb));
}
}


Jun 27 '08 #8
AAaron123 wrote:
Actually I made subs out of the two methods you showed (for npn-static and
for static classes).

They work well but it made me wonder if the class being investigate could be
brought in as a parameter. That is, can a parameter be subsituted for
DataFormats and for fb? Then I realized I have no idea how to do that.
>public static void StaticTmp()
{
foreach(FieldInfo fi in typeof(DataFormats).GetFields(BindingFlags.Public
| BindingFlags.Static))
{
Console.WriteLine(fi.Name + "=" + fi.GetValue(null));
}
}

>public static void NonStaticTmp()
{
Foobar fb = new Foobar();
foreach(FieldInfo fi in fb.GetType().GetFields(BindingFlags.Public |
BindingFlags.Static))
{
Console.WriteLine(fi.GetValue(fb));
}
}
What about something like:

public static void PrintStatic(Type t)
{
foreach(FieldInfo fi in t.GetFields(BindingFlags.Public |
BindingFlags.Static))
{
Console.WriteLine(fi.Name + "=" + fi.GetValue(null));
}
}

and use as:

PrintStatic(fb.GetType());
PrintStatic(typeof(DataFormats));

?

Arne
Jun 27 '08 #9
thanks a lot

"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:48***********************@news.sunsite.dk...
AAaron123 wrote:
>Actually I made subs out of the two methods you showed (for npn-static
and for static classes).

They work well but it made me wonder if the class being investigate could
be brought in as a parameter. That is, can a parameter be subsituted for
DataFormats and for fb? Then I realized I have no idea how to do that.
>>public static void StaticTmp()
{
foreach(FieldInfo fi in
typeof(DataFormats).GetFields(BindingFlags.Publi c |
BindingFlags.Static))
{
Console.WriteLine(fi.Name + "=" + fi.GetValue(null));
}
}

>>public static void NonStaticTmp()
{
Foobar fb = new Foobar();
foreach(FieldInfo fi in fb.GetType().GetFields(BindingFlags.Public |
BindingFlags.Static))
{
Console.WriteLine(fi.GetValue(fb));
}
}

What about something like:

public static void PrintStatic(Type t)
{
foreach(FieldInfo fi in t.GetFields(BindingFlags.Public |
BindingFlags.Static))
{
Console.WriteLine(fi.Name + "=" + fi.GetValue(null));
}
}

and use as:

PrintStatic(fb.GetType());
PrintStatic(typeof(DataFormats));

?

Arne

Jun 27 '08 #10

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

Similar topics

9
by: Steve Wasser | last post by:
I need to sort a two-dimensional array. Each day I process a file with 9 comma-delimited fields, and varying amount of records (lines). I want to pull in each line, split it according to the comma...
1
by: Jeff | last post by:
I am struggling with the following How do I marshal/access a pointer to an array of strings within a structure Than Jef ----------------------------------------------------------------
3
by: Mark Dunmill | last post by:
I can't create a Constant/Read-only array field in managed C++ classes - doesn't allow the keyword const pointer to const object on array fields in managed C++ classes. e.g. Want to define a...
7
by: John Grandy | last post by:
My ASP.NET Web Service project has a Web Method that returns an array filled with instances of a custom class. The custom class is defined in a Class Library that is included in the web-service...
9
by: Daan | last post by:
Say I have an Object of the following class: public class SomeClass { public string var1; public int var2; public SomeType var3; } Now I want to print all the three vars. Is it possible to...
5
by: sherifffruitfly | last post by:
Hi, I'm just learning cpp, and the exercise I'm working on is basically as follows: 1) Create a struct type with 4 members (char, char, char, int). 2) Create an array of, say 3 instances of...
14
by: Peter Hallett | last post by:
I would like to set up a string array as a class member, or field, and then populate this array by reading in from a text file, but I cannot find the appropriate syntax. The getter and setter are...
5
by: no1zson | last post by:
I have been reading through many of the array questions and cannot find one that addresses my issue. Maybe someone can help me out. Same story, I am learning Java and have just written a CD...
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.