|
|
Comments and Discussions
|
|
 |
|
|

|
Excatly what I was looking for. Great article
|
|
|
|

|
This article is the bomb!
Everything I needed.
Short and to the point.
|
|
|
|

|
detailed explaination but does not serve the purpose of a quik refrence as i'd expect from cheat sheet
|
|
|
|
|

|
Note: the as keyword will only work with reference types since value types can't be null.
|
|
|
|

|
Please answer:
Is there a way to Convert variable with type as a String parameter?
Can you write a function like this:
a = MyTryCast(variable as Object, SystemType As String)
and then use it like:
a = MyTryCast("12.13", "System.Decimal")
without use of
Select Case SystemType
Case "System.String"
return CType(variable, String)
...
|
|
|
|
|

|
No offense, but this article is missing a lot. Each C# & VB.Net casting feature has an equivalent.
For example:
The VB.Net's equivalent to C#'s "as" is TryCast()
|
|
|
|

|
one BIG difference between C# and VB:
C# compiler will never allow
int myInt = (int) "3";
VB.NET compiler, even with Option Strict ON will always allow
Dim myInt as Integer = CType ("3", Integer)
Both languages understand the .NET class System.Convert:
myInt = System.Convert.ToInt32("3")
|
|
|
|

|
How about and example on casting arrays.
Thanks
Schneider
|
|
|
|

|
I would like to figure out how to do this to.
This is something like what I would like to do...
Public Sub SortMyArray(aValues() as Object)
if typeof aValues is String() then
SortStrings(directcast(aValues,string())
elseif typeof aValues is SomeOtherObject() then
SortOtherObjects(directcast(aValues,SomeOtherObject())
end if
End Sub
Private Sub SortStrings(asValues() as String)
'Do the sort
End Sub
Private Sub SortOtherObjects(aValues() as SomeOtherObject)
'Do the sort
End Sub
|
|
|
|

|
I prefer to use ToString rather than CType for certain things. ToString will return an empty string if a column is a NULL in a DataRow. CType will throw an exception on a NULL.
|
|
|
|
|
|
|

|
I have a problem in my code with regard casting. I have a base class object called BaseDAO, a subclass is created from this called CustomerRowSet. I have a function which, through reflection, will create an instance of the subclass.
public static BaseDAO newInstance (DAOAssembly cAssembly, string strProviderID)
{
// lookup instance
BaseDAO cDAO = m_cManager.lookup(cAssembly, strProviderID);
if (cDAO != null)
{
Console.WriteLine("Created DAO Instance");
}
else
{
Console.WriteLine("Failed to create specified DAO handler");
}
// initialise instance
// TODO:
return cDAO;
}
I make a call to the newInstance function and test the type of the returned object, thus:
DAOAssembly x = new DAOAssembly("DAOTest.dll", "d:", "ds.xml", "d:", "TEST");
Object o = CustomerRowSet.newInstance(x, "SELECT_CUSTOMERS");
if (o is DAOTest.CustomerRowSet)
{
Console.WriteLine("Object is of correct type");
}
else
{
System.Type t;
t = o.GetType();
Console.WriteLine(t.FullName);
}
Upon return, execution branches into the else statement and not the if statement, but the output from code is:
Created DAO Instance
DAOTest.CustomerRowSet
This shows that the returned object is of the correct type yet doesn't execute the if branch of the code. In addition, if I try the following code:
CustomerRowSet rs = (CustomerRowSet)CustomerRowSet.newInstance(x, "SELECT_CUSTOMERS");
I get an InvalidCastException.
Grateful for any suggestions or information, many thanks in advance.
Chris.
|
|
|
|

|
If the specified type and the run-time type of the expression are the same, however, the run-time performance of DirectCast is better than that of CType.
So which function gives the best performance under all circumstances? Can you give an example of what type of conversions cause DirectCast to throw!
Thanks
|
|
|
|

|
Please add pointers in the next update, they are still used in C# and casting can be done with them. I am particularly having trouble with pointers in my COM Interop which involves casting.
|
|
|
|

|
>>>C# is a strictly typed language. Whenever types don't match, casting is necessary. This is simply not true. C# allows you to upcast and perform implicit conversions (eg double to int) without needing a cast. The whole issue of implicit vs explicit is not mentioned. The existence of a range of other conversion functions (CInt, CDbl, etc) is ignored. Likewise the Convert functions, ToString(), etc. I'll wait for V2.
|
|
|
|
|
|

|
I liked the style of this article as you consider both C# and VB.NET. I learnt something new - I did not know about the as operator
Do any C++ purists agree with me when I say: casting is never needed in good OO design.
Even in an XML parser you don't need it. Consider a simple example: if you can always predict what nodes you might need to 'cast' into you can provide a function:
CommentElement Node::GetAsComment()
...where Node is the superclass of all Xml element nodes.
|
|
|
|

|
dog_spawn wrote:
Do any C++ purists agree with me when I say: casting is never needed in good OO design.
Just off the top of my head, here are some examples where very good design in C# requires casting:
* Creating a plug-in architecture - try loading and instantiating dynamically assemblies without polymorphic reflection tests and casting.
* Using pretty much any of the Asynchronous programming methods in the FCL - all use boxing and un-boxing, which can't be done without casting.
* Adding queue items that return values to a the ThreadPool requires boxing and un-boxing, which again is casting.
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
|
Describes several casting and type related operations in VB.NET and C#.
| Type | Article |
| Licence | Public Domain |
| First Posted | 22 Sep 2003 |
| Views | 719,311 |
| Bookmarked | 82 times |
|
|