473,725 Members | 2,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic data casting

Hi,
I have a series of functions which do the following:

ValidateData( args ); //args if of type ArrayList
// Work out if the data is valid or not, and work out the type of
args[1] - int, string, whatever?

if((bool)args[0]) // valid data
{
WriteDataToDB( args[1] );
}

However, it fails, as arg[1] is of type object, and I need to cast it
to type int, string whatever.
Rather than do a big switch statement, testing the type of args[1], is
there a way of
simply casting it using a function like:

ValidateData( args, ref type TypeOfArg1 ); //args if of type ArrayList
// Work out if the data is valid or not, and work out the type of
args[1] - int, string, whatever?
// work out type of args[1]

if((bool)args[0]) // valid data
{
WriteDataToDB( (TypeOfArg1)arg s[1] );
}

TIA

Trev

Nov 17 '06 #1
3 2105
Hi Trev,

No, there isn't a way to cast a boxed System.Int32, for example, into a
System.Int32 variable or method argument at runtime unless it's hard-coded
using an explicit cast. Even conversion methods such as Convert.ChangeT ype
return System.Object, so even if you really mean conversion, not casting, it
still wouldn't work.

Why do you need it cast in the first place?

Have you overloaded the WriteDataToDB method to take typed arguments such as
bool and string?

Just add one more overload: object :)

--
Dave Sexton

"Trev" <t.*********@bt internet.comwro te in message
news:11******** **************@ j44g2000cwa.goo glegroups.com.. .
Hi,
I have a series of functions which do the following:

ValidateData( args ); //args if of type ArrayList
// Work out if the data is valid or not, and work out the type of
args[1] - int, string, whatever?

if((bool)args[0]) // valid data
{
WriteDataToDB( args[1] );
}

However, it fails, as arg[1] is of type object, and I need to cast it
to type int, string whatever.
Rather than do a big switch statement, testing the type of args[1], is
there a way of
simply casting it using a function like:

ValidateData( args, ref type TypeOfArg1 ); //args if of type ArrayList
// Work out if the data is valid or not, and work out the type of
args[1] - int, string, whatever?
// work out type of args[1]

if((bool)args[0]) // valid data
{
WriteDataToDB( (TypeOfArg1)arg s[1] );
}

TIA

Trev

Nov 17 '06 #2
Hi Dave,
The reason why I thought about run-time casting was because the
original data comes from a spreadsheet, and the values come as strings.
I then need to do a check as different cells in the sheet can only have
specified data types (eg row 1 can only be ints, row 2 only doubles
etc.). So, once I know the value and do a lookup on the datatype, I
then have to cast the string to ....whatever, as the database is very
fussy about such things.

Hope this explains it!

Nov 17 '06 #3
Hi,

What is the signature of your WriteDataToDB method?

If you're converting the data in your ValidateData method to the appropriate
Type, it still has to return System.Object. That doesn't mean the Type is not
what it was converted to, it's just being passed around as an object. I
assume that your WriteDataToDB method really only needs to accept
System.Object.

You may want to post the WriteDataToDB code if you are unsure what I mean and
I'll try to explain it better.

--
Dave Sexton

"Trev" <t.*********@bt internet.comwro te in message
news:11******** *************@e 3g2000cwe.googl egroups.com...
Hi Dave,
The reason why I thought about run-time casting was because the
original data comes from a spreadsheet, and the values come as strings.
I then need to do a check as different cells in the sheet can only have
specified data types (eg row 1 can only be ints, row 2 only doubles
etc.). So, once I know the value and do a lookup on the datatype, I
then have to cast the string to ....whatever, as the database is very
fussy about such things.

Hope this explains it!

Nov 17 '06 #4

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

Similar topics

12
2172
by: Jason Tesser | last post by:
I work for at a college where I am one of 2 full-time developers and we are looking to program a new software package fro the campus. This is a huge project as it will include everything from registration to business office. We are considering useing Java or Python. I for one don't like Java because I feel the GUI is clunky. I also think that we could produce quality programs faster in Python.
0
1875
by: Angelos Karantzalis | last post by:
Hi y'all, here's a tricky one: I've written a wrapper around a Com Interop-generated assembly in C#. It's called SDEHandler & implements a previously defined interface, so that I can use it as a provider, and load instances of it dynamically. In other words, I use it as a "plugin implementation" of a well-known API (a Provider?). Inside it I create a DataSet, which gets filled with at least a column containing values of type SDEPoint,...
5
8013
by: Radde | last post by:
HI, Are ther any pitfalls for dynamic cast in type safe downcasting..
4
1515
by: Alfonso Morra | last post by:
Hi, I'm trying to write a simple generic container. I have the ff: typedef union { long int_val ; char* string_val ; double dbl_val ; void* vptr_val ; }Element ;
2
673
by: Martin Hart - Memory Soft, S.L. | last post by:
Hi all: I still very new to the .NET world and don't know if what I am asking is due to an over-imaginative imagination or the fact that I have read too many fiction books!! Let me show you a very basic scenario: using System;
6
6402
by: Philipp Schumann | last post by:
Hi, I have a need for "dynamic type casting": in other words, in a "MyConvert" method I get passed an Object "value" and a Type "type" and the method should attempt to convert value into type. Of course it first tries to obtain the appropriate TypeConverter. However, for some types there are no applicable type converters. Consider a custom class "Dummy" from a linked assembly (in my scenario, neither the Dummy type nor its assembly...
13
2031
by: coosa | last post by:
Dear all, Using the conio implementation i wanted to create a dynamic string, whereby its size would be determined after each keyboard hit; in other words, i don't want to ask the user to specify the the size, but rather keep him/her typing and after each keyboard hit, the function getch() determines whether he/she entered the ENTER key to end the process; otherwise, increases the dynamic size or also decreases it if the back key was...
8
338
by: mfc | last post by:
Suppose I have a Cookie class and a Factory Class. There are many types of Cookies and maybe one or more Factories with a ProcessCookie Method. Suppose all the PutInBox method does is decide what colour box to put the cookie into. But since the colour of the box depends on whatever Factory is boxing them (Different branding etc) the PutInBox method can't be in the Cookie class and has to be in the Factory class. The problem is pretty soon...
4
5067
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or deleted, however it must be initialized by a function during run-time to contain so many users which each contain so many directories of which each contain so many files. I've completed the program and have it running flawlessly without implementing...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.