473,320 Members | 1,887 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,320 software developers and data experts.

Cast Question.

Dear Friend .
I have a question about casting in C# . I would highly appreciate if someone could help me.

Expand|Select|Wrap|Line Numbers
  1. var t = (from o in baseContext.Myusers
  2. where o.name == "name"
  3. select o);
  4.  
  5. object.datasource = t;
  6.  
  7.  
in object selectedindexchange event :

Expand|Select|Wrap|Line Numbers
  1. var address = (object.selecteditem as Myuser).address;
  2.  

Now I want to select like this

Expand|Select|Wrap|Line Numbers
  1. var t = (from o in baseContext.Myusers
  2. where o.name == "name"
  3. select new {mynewName = o.name + " *** " + o.family , o.address , o.age );
  4.  
  5. object.datasource = t;
  6.  
  7.  
the question is :
how can get mynewName value in selectedindexchange event .

best.
ali
Mar 30 '09 #1
5 1259
Christian Binder
218 Expert 100+
Hi,

I don't know, which class your "object" is of, but maybe there is a ValueMember or DisplayMember property.
If you set the ValueMember-property to "mynewName"
Expand|Select|Wrap|Line Numbers
  1. object.ValueMember = "mynewName"
you could use it in the SelectedIndexChanged-EventHandler
Expand|Select|Wrap|Line Numbers
  1. object.SelectedValue
Mar 31 '09 #2
dear ChBinder
thank you for your replay.

my problem is : who to access the object's datasource on slectedIndexChanged
event.

your solution is the good way to get the 'mynewName' value , but how could I get the combination of "mynewName" and "address" ?
Mar 31 '09 #3
Christian Binder
218 Expert 100+
Hi,

the problem is, you are using an anonymous type as the result of your select-statement.
If you would use a class defined by yourself, it would make it easier.

Expand|Select|Wrap|Line Numbers
  1. void xyz {
  2.   var t = (from user in baseContext.Myusers
  3.     where user.name == "name"
  4.     select new NewMyUser(o.name + " *** " + o.family , o.address , o.age)).ToArray();  // This returns NewMyUser[]
  5.  
  6.   object.DataSource = t;
  7.   object.DisplayMember = "MyNewName"; // Show MyNewName to User
  8.  
  9. }
  10.  
  11. class NewMyUser {
  12.   public NewMyUser(string myNewName, string address, int age) {
  13.     MyNewName = myNewName;
  14.     Address = address;
  15.     Age = age;
  16.   }
  17.  
  18.   public string MyNewName { get; set; }
  19.   public string Address { get; set; }
  20.   public int Age { get; set; }
  21. }
  22.  
In the SelectedIndexChanged-Event you can cast the SelectedItem and access the properties

Expand|Select|Wrap|Line Numbers
  1. (object.SelectedItem as NewMyUser).Age // or  .Address ...
  2.  
Mar 31 '09 #4
Plater
7,872 Expert 4TB
This is very strange looking C#.
I am guessing those are linq queries?
Mar 31 '09 #5
Christian Binder
218 Expert 100+
@Plater
Yes, the creation of the DataSource (resp. the filtering of the original data) is done via LinQ. But the main problem here is that it's not possible (without using of Reflection imho) to access properties of anonymous types.
Mar 31 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Richard Lee | last post by:
Hi, I have a question when I do a data type cast. the common way when we do a cast, is we know the type we want to cast to, i.e. we want to cast object to string, object xyz = "question";...
15
by: Christopher Benson-Manica | last post by:
If you had an unsigned int that needed to be cast to a const myClass*, would you use const myClass* a=reinterpret_cast<const myClass*>(my_val); or const myClass* a=(const myClass*)myVal; ...
5
by: MC | last post by:
Hi If I have a pointer to a some structure say for example payroll_ptr where struct payroll { ... } has some members and if i use a function argument as int function_process ( (payroll_ptr)...
17
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
1
by: Reza Nabi | last post by:
Bakground: I have a webform (LoadCtl.aspx) which loads the user control to a placeholder dynamically based on the ctlName querystring passed in the URL. Webform (LoadCtl.aspx) also passes a...
5
by: Frederick Gotham | last post by:
Before I begin, here's a list of assumptions for this particular example: (1) unsigned int has no padding bits, and therefore no invalid bit- patterns or trap representations. (2) All types have...
4
by: dba_222 | last post by:
Dear Experts, Ok, I hate to ask such a seemingly dumb question, but I've already spent far too much time on this. More that I would care to admit. In Sql server, how do I simply change a...
4
by: Pavel Minaev | last post by:
There was an earlier discussion regarding which one is faster - a throwing cast - "(Foo)obj", or a non-throwing one - "obj as Foo", when both are available for a given type and value. The consensus...
2
by: =?Utf-8?B?Unlhbg==?= | last post by:
Hi, How can I get around runtime error that says I can not explicit cast List<SubClassto ICollection<Class>? Generic List inhertis generic ICollection and Subclass inherits Class, then...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.