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

cast

Hello,
I don't know how to do the following.
In a method I get a parameter with type object. These can be about 10
different types and I want to store the object in a list with the correct
type (so, not type object, but type A or B or C). I could use a switch on
gettype and then do a cast and put it in the list. But if new types are
added I have to change this method and I am too lazy to do that. I need
something like:
list.add((gettype(object))object)
The '(gettype(object))' is meant as a cast, but this is not possible in c#.
How can I add the correct type in a generic way? So I don't need to change
anything if a new type is created.

Thanks
Frank
Aug 7 '07 #1
3 1944
On Aug 7, 12:53 pm, "Frank" <frankn...@advitronic.nlwrote:
I don't know how to do the following.
In a method I get a parameter with type object. These can be about 10
different types and I want to store the object in a list with the correct
type (so, not type object, but type A or B or C). I could use a switch on
gettype and then do a cast and put it in the list. But if new types are
added I have to change this method and I am too lazy to do that. I need
something like:
list.add((gettype(object))object)
The '(gettype(object))' is meant as a cast, but this is not possible in c#.
How can I add the correct type in a generic way? So I don't need to change
anything if a new type is created.
What's the type of your list? If you're adding it to the same list
whatever happens, then just add it as it is. Casting wouldn't actually
change the type of the object anyway.

If you could provide a short but complete program which demonstrates
the problem, that would make things clearer.

See http://pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Jon

Aug 7 '07 #2
This might sound stupid, but why not just create a list of objects, to
which you can add whatever you'd like, and let the function that gets
the data from the list perform the cast?

Aug 7 '07 #3
you are absolutely right, it's a stupid question. I didn't think of the
consequences.
Frank
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@d55g2000hsg.googlegr oups.com...
On Aug 7, 12:53 pm, "Frank" <frankn...@advitronic.nlwrote:
>I don't know how to do the following.
In a method I get a parameter with type object. These can be about 10
different types and I want to store the object in a list with the correct
type (so, not type object, but type A or B or C). I could use a switch on
gettype and then do a cast and put it in the list. But if new types are
added I have to change this method and I am too lazy to do that. I need
something like:
list.add((gettype(object))object)
The '(gettype(object))' is meant as a cast, but this is not possible in
c#.
How can I add the correct type in a generic way? So I don't need to
change
anything if a new type is created.

What's the type of your list? If you're adding it to the same list
whatever happens, then just add it as it is. Casting wouldn't actually
change the type of the object anyway.

If you could provide a short but complete program which demonstrates
the problem, that would make things clearer.

See http://pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Jon

Aug 17 '07 #4

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

Similar topics

0
by: Aaron W. West | last post by:
Fun with CAST! (Optimized SQLServerCentral script posts) I found some interesting "tricks" to convert binary to hexadecimal and back, which allow doing 4 or 8 at a time. Test code first: --...
3
by: Mike | last post by:
I am using MS-Access as a front end for my MS-SQL DB. I have a sql view that uses the following: SELECT TOP 100 PERCENT RECID, PATNUMBER AS , SVCCODE AS , QTY, PROF_CHRGS AS , AMOUNT,...
4
by: Ray | last post by:
When a single-bit bitfield that was formed from an enum is promoted/cast into an integer, does ANSI C say anything about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is...
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...
3
by: mra | last post by:
I want to cast an object that I have created from a typename to the corresponding type. Can anycone tell me how to do this? Example: //Here, Create the object of type "MyClass" object...
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...
6
by: Lore Leunoeg | last post by:
Hello I derived a class MyControl from the Control class. Public Class MyControl Inherits Control Sub New() MyBase.New() End Sub End Class
9
by: Frederick Gotham | last post by:
Let's assume that we're working on the following system: CHAR_BIT == 8 sizeof( char* ) == 4 (i.e. 32-Bit) Furthermore, lets assume that the memory addresses are distributed as follows: ...
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...
7
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
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: 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...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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.