name of anonymos types

  • Thread starter Thread starter Rainer Queck
  • Start date Start date
R

Rainer Queck

Hello NG,

is it possible, to localize the name of a anonymos type?
In my application I am creating a list of anonymos typed sturcture like:
var myObject = new
{
name = <aName>,
age = <aAge>,
.....
}
List<Object> objList = new List<Object>;
objList.Add(myObject);

This list is assigned as a datasource to a DataGridView which as the column
header shows "name, age, ..."
Since I must localice my application, I would like to assigne the a
localized string to myObject.<localized_name>.

var myObject = new
{
<resouce strings>.name = <aName>,
<resouce strings>age = <aAge>,
.....
}

Can this be done?
Thanks for any help and hints.

Regards
Rainer
 
Can this be done?
Thanks for any help and hints.

I think so, but you have to use anonymous methods.

So try this:

delegate int MyAnonMethodName (int i);

//then, in your class somewhere

MyAnonMethodName ARandomPlaceholderNameHereJustMakeItUp = (int x) =>
{return x*x;};

//or, to use another example not involving just a method...

class Film
{
public string Name {get; set;}
int date;
public void MyFunction () {}
}

var films = new List <Film>
{
new Film {Name = "Jaws", date = 1975},
new Film {Name = "Oz", date = 1939}
};

//now , for example, use the Action delegate, which is a public void
delegate for something not returning a value...
Action<Film> printAFilm = x => {Console.Write(x);};

// now you can use LINQ notation for example...
var ListOldFilms = films.FindAll(x => x.date < 1960); //returns a list
called ListOldFilms of old films before 1960...
films.FindAll(x => x.date < 1960).ForEach(printAFilm); //prints out
the list of old films before 1960...
films.ForEach(MyFunction); //does something to each element of films,
according to the user-defined function "MyFunction"

Hope this helps.
Keep in mind I'm learning the language and I did not check to see if
the above compiles, but it should work.

RL
 
Since I must localice my application, I would like to assigne the a
localized string to myObject.<localized_name>.

var myObject = new
{
     <resouce strings>.name = <aName>,
     <resouce strings>age = <aAge>,
     .....

}

Can this be done?

No. You shouldn't be basing user-visible strings on property names -
and the names of the properties are fixed at compile time anyway, so
they can't possibly depend on an execution time locale setting. You
should translate from the (neutral) property name to the localised
name at execution time.

I don't know enough about DataGridView to be able to give you the code
straight out, but I'm sure it can't be that hard.

Jon
 
Thanks to all responders!

As it looks, my intent to localize properties is not the right way.
I think I will drop this idea.

Regards
Rainer
 
Thanks very much for RL and Jon's help.

Hi Rainer,

Thanks for your post. My name is Hongye Sun [MSFT] and it is my pleasure to
work with you on this issue.

Jon's answer is exactly right. You shouldn't localize the application by
using property names. They are fixed in compile time and you cannot change
it for different locale.

DataGridView designer supports to auto generates resource file for
localizable columns. You don't need to write any piece of code. Here is the
detailed steps:
1. View form properties, which contains the DataGridView.
2. Find property "Localizable" and change it to true, it will immediately
generates a resx file with localizable elements within this form.
3. View DataGridView properties, and find property "Columns" and edit it.
4. Click "Add" to add one column.
5. In "Add Column" window, choose "Unbound column" and type column's Name,
Type and HeaderText.
Name should be unique in on grid.
Type should be TextBoxColumn for display data use.
HeaderText should be the text in default language and it will be saved into
resx file.
6. Click Ok and go back to "Edit Columns" window.
7. At "Unbound Column Properties" at right side, find property
"DataPropertyName" and type the property name of your anonymous type. For
example: name, age. By setting this property, DataGridView will
automatically bind the data to this column.
8. Click Ok and go back to Form designer, save all, and double click resx
file.
9. It shows you one item with name "<Column Name>.HeaderText" and value is
what you typed just now. This indicates that the value will be displayed in
default language.
10. Go back to Form designer and open form's properties, find property
"Language", change it to one language.
11. Go to edit columns again and change the HeaderText to the specified
language text, click Ok and save all.
12. The designer will immediately generated another .<language code>.resx
file which contains the modified HeaderText.

After that, Visual Studio will compile different language resource into
satellite DLLs within different language code folders. When deploying, you
must keep them in same folder structure.

Here is the documentation for localizing windows form:
http://msdn.microsoft.com/en-us/library/9xdxwwkc.aspx.

Have a nice weekend.

Regards,
Hongye Sun ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hello Hongye,

thank you very much for your answer.

Actually I am aware of the localization mechanismn. My problem is a
different one.
I am looking for a way to "flip" the visualisation of one of my data tables.
Please refere to my thread "Rotate DataGridView".
My plan was to "transform" my datatable by generating a array of objects to
swich columns/rows.
In the meanwhile - also due to the response of Jon - I think that this is
not the way to get the result I am looking for.
The only idea I have at the moment to get the grid view as desired is to
dynamically build a "converted" DataTable from my typed DataTable which only
holds string columns wich I then would fill from it. Still I would end up
with a whole bunch of limitations.

This is why I started the "Rotate DataGridView" Thread, hoping for some
hints, that would point out a better solution.

Have a nice weekend too.

Regards
Rainer
 
This is why I started the "Rotate DataGridView" Thread, hoping for some
hints, that would point out a better solution.

As you appear to be a professional programmer (I just code for fun),
you might check out some commercial packages I saw in a trade magazine
the other day that specifically are geared towards GridView and all
the other visual candy that comes with front end database design,
which seems to be what you are doing. I notice the packages are
reasonably priced ($50 to $500) and something you or your employer can
afford I would imagine.

Seems to me that a good portion of what is coded in Forms (I deal with
Forms and C#) is visual stuff--and that's as important to the user as
the back end "engine" that drives the app. As an aside I'm looking
forward to learning WPF which apparently is a new paradigm and makes
coding the graphics stuff easier (also I'm sure they have library
routines for making stuff transparent, LOL).

Happy coding,

RL
 
Thanks very much for RL and Jon's help.

I'm saving this email. It's the first time I've been put into the
same flattering sentence as Jon Skeet, the Scot Myers of the decade,
and thanked by a MSFT employee (which I hold stock in--good company,
but like Intel really beat up now).

Pretty good for a dumb troll hack!

RL
 
Hi Rainer,

Thanks for your response.

I have replied you in "Rotate DataGridView" thread. As you said, anonymos
type is not the right way to solve your problem. It is better for us to
close this conversation and continue discussing in that thread. Do you
think so?

For your convenience, I put my reply here, too.
----------------------------------------
This is a very interesting question. If you are using DataTable type to
hold the data, please use the following function to build another rotated
data table:
---------------------------------------------
public DataTable RotateTable(DataTable dt)
{
DataTable table = new DataTable();
for (int i = 0; i <= dt.Rows.Count; i++)
{
table.Columns.Add(Convert.ToString(i));
}
DataRow r = null;
for (int k = 0; k < dt.Columns.Count; k++)
{
r = table.NewRow();
r[0] = dt.Columns[k].ToString();
for (int j = 1; j <= dt.Rows.Count; j++)
r[j] = dt.Rows[j - 1][k];
table.Rows.Add(r);
}

return table;
}
---------------------------------------------
In your example, the rotated table will look like:
1 2 3 4
No 1 2 3
Name Frank Klaus Jeff
Age 25 43 34

Set DataGridView's ColumnHeadersVisible to false, so that the numeric
column will not display.

----------------------------------------

Thanks for your cooperation.

Regards,
Hongye Sun ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
 
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top