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 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
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!
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! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
|
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,...
|
by: Radde |
last post by:
HI,
Are ther any pitfalls for dynamic cast in type safe downcasting..
|
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 ;
|
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;
| |
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |