Flexibility Vs Performance With CType And Generics?
Apr 11, 2012
Recently I found a way to make CType work with generics (FYI, VB 2010 Express with option strict on). Say I have two generic types, T1 and T2, with variables x as T1 and y as T2. y=CType(x,T2) diagnoses with "Value of type 'T1' cannot be converted to 'T2'". y=CType(CObj(x),T2) compiles cleanly, and it works fine provided that T1 can be converted to T2. Failing that, an exception will be raised at run-time. Obviously, a downside of this is the boxing performance hit. Also, it is a half a step backward along the road to strong typing (a type conversion problem is discovered at run-time vice compile-time).
These objections aside, consider the GSU class below. There are two ways to do a generic CType of a variable, and then there is a way to do a CType of an entire array. GSU is sufficient (see sub Test) to convert an item or an array of items between, for example, all the numeric types (byte, short, double, ...). Exception handling remains to be done.
Type conversion code tends to get lengthy, so I think this is a nice result. What do you think? I am guessing that CType has some pre-generic smarts in it, and all I did was find a way to exploit it in the context of generics and option strict. I am mostly interested in generic tricks for value types.
[Code].....
View 3 Replies
ADVERTISEMENT
Jan 14, 2011
in terms of performance (speed), does directcast beat ctype?
View 6 Replies
Jul 7, 2010
have a look at this code Dim colors() As KnownColor = CType(System.Enum.GetValues(GetType(KnownColor)), KnownColor()) Since CType is a function that returns DataType ,But here as in the code it seems that CType() is returning array having values that is assigned to color() array. How this is happening?
View 3 Replies
Jul 24, 2009
I have a number of different object types that I would like to pass into a function. All my objects are created from classes with a .Description property. Is it possible to determine the type of object for use as the 2nd parameter in a CType call at runtime? e.g. Debug.Print CType(MyObject, TypeOf(MyObject)).Description (I have Option Strict On) I get errors when I do this.
View 4 Replies
Mar 24, 2011
In vb.net, I got an Enum defined As Integer:
Enum enUtilityTypeDetailStudentEntryWorkflow As Integer
enUTDSEW_Default = 379
[code].....
This works but it adds a lot of noise to the code and it seems like the Enum is already defined as an Integer, making me think, why do I have to assign it again?
View 1 Replies
Sep 26, 2011
I cant understand why this does not work! Hope for your intelligent answers ;)
With
CType(Controls(PicsGame(GameCount)),
PictureBox)
BorderStyle =
BorderStyle.None
View 4 Replies
Feb 29, 2008
Is it possible to change a datarowview object to an integer?
View 3 Replies
Nov 1, 2009
dim testpanel as new panel
dim test as CType(testpanel, Control))
msgbox(typename(test)) ' it gives me "panel"
'i dun get it, how can typename of test give me Panel when i explicitly call it to be converted to a Control type?
View 9 Replies
Jun 16, 2010
I am an experienced C/C++/C# programmer who has just gotten into VB.NET. I generally use CType (and CInt, CBool, CStr) for casts because it is less characters and was the first way of casting which I was exposed to, but I am aware of DirectCast and TryCast as well. Simply, are there any differences (effect of cast, performance, etc.) between DirectCast and CType? I understand the idea of TryCast.
View 2 Replies
Dec 13, 2009
I turned option strict on, wich gave me some errors. The errors are quite easy to fix, but it seems like there are several ways of doing things. [code] 'previoustime' is declared as a TimeSpan, and the column "sluttid" is defined as a TimeSpan-column.Now, as far as I know, there are two ways of solving this; using CType and using DirectCast, like this: [code]
View 4 Replies
Jul 16, 2010
Assume that I have this declaration (according to MSDN):
Private Declare Function PostMessage Lib "user32" ( _
ByVal hWnd As IntPtr, _
ByVal wMsg As UInteger, _
[code]....
And this is how I use it:
PostMessage(hWnd, 516, CType(2, UIntPtr), CType(0, UIntPtr))
Option Strict is turned On. So the questions are:
1. Do I need to use UIntPtr in declaration or it will be better to use UInteger?
2. Do I need to use CType function in these cases or there is a better way for this or I need to avoid declaring pointers?
View 6 Replies
Oct 25, 2011
does your IDE ever get amnesia?in the line before it is clearly defined.in the following line it is not.Maybe it's a faulty translation from Tangible Software.the C example i'm looking at:
IAnimatedMeshSceneNode* newNode = (IAnimatedMeshSceneNode*) oldNode->clone();
translated as:
Dim newNode As IAnimatedMeshSceneNode = CType(oldNode.clone(), IAnimatedMeshSceneNode)
had to make a dummy for it because i can't use arrays with CType.
View 1 Replies
Jul 12, 2010
The below is a snippet from VB that I am porting to a C# rewrite. My question is what is receipt_date after the assignment? Is it still an object or is it a string?
Dim receipt_date As Object
receipt_date = CType(dr.Item("RECTDT"), String)
Would this be the correct C# counterpart?
object receipt_date;
receipt_date = dr["RECTDT"].ToString();
After both of these execute would the VB version, receipt_date be equal to the C# version?
View 2 Replies
Feb 25, 2011
[Code]...
What happens when a user wants to alocate "" as Item(DurationColumn) to integer? I get an exception. clean solution to avoid this and set 0 for ""?
View 5 Replies
Jan 30, 2012
I have this class: Public Class Foo
[Code]...
In the f = "Bar" assignment I loose the previous X and Y values previously assigned. There's a way to overload the operator to assign to the "Value" member instead of creating a new Foo class? Or a way to obtain the target of the assignment to copy the values??
View 2 Replies
Jan 26, 2011
I am receiving the following error:
Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 21: 'Determine the maximum pictureID for this user
Line 22: Dim results As DataView = CType(maxPictureIDDataSource.Select(DataSourceSelectArguments.Empty), DataView)
Line 23: Dim pictureIDJustAdded As Integer = CType(results(0)(0), Integer)
[Code]....
View 1 Replies
Oct 31, 2009
i've got a problem here whereby my ctype doesn't work.firstly, to replicate my case briefly, i've created an extension of control class
[code]...
father.getheight 'compiler writes: Panel class doesn't support getheight, but I've already converted it to a Control with CType!end sub of course if i dim father as Control it works perfectly, but in my case father may be other objects as well that aren't controls so I had to dim father as a general object, is there a fix so i could properly convert father into a Control object?
View 1 Replies
Sep 24, 2010
I have a class that overrides Ctype operator to have conversion from and to String :
Public Class TotoToString
Property value As String
Public Sub New(ByVal V As String)
Me.value = V
End Sub
[Code] .....
View 4 Replies
Dec 27, 2009
I need to programmaticlly check one RadioButton1 in GroupBox1 in my Form.The name of RadioButton and his parent GroupBox is received from global variable 'names'.How to change GrpBx1 to names(0)?
VB.Net
Dim names as String() 'GrpBx1,RadioButton1
'WORKING but not exactly what I wanna while it's fixed to only one GroupBox:
CType(Me.GrpBx1.Controls(names(1)), RadioButton).Checked = True
[code]....
View 5 Replies
Oct 13, 2009
Public Enum Fruit
Red_Apple = 1
Oranges
Ripe_Banana
End Enum
Private Sub InitCombosRegular()
[Code]...
Why does the Ctype work and the Directcast does not with the same syntax? Yet if I cast the selectedValue to an int before I DirectCast, then it works
View 1 Replies
Oct 12, 2009
Does DirectCast, ctype, etc., are common function? Why it return type, and how do I used it? Is there others function where I need learn to be dot net expert?
View 3 Replies
Jun 27, 2009
what if the session object is not present
what will it return if CType(Session("pid"), String) is not present
View 5 Replies
Apr 24, 2010
Ever since I moved from VB6 to VB.NET somewhere in 2005, I've been using CType to do casting from one data type to another. I do this because it is simply faster to type, used to exist in VB6 and I do not know why I have to be using DirectCast if there is apparently no difference between them.I use TryCast once in a while because I understand that sometimes casting can fail. I however cannot get the difference between CType and DirectCast.
View 3 Replies
Dec 14, 2011
Suppose I have the following code:
Public Class MyDbTransaction
Implements IDbTransaction
Private itsTransaction As IDbTransaction
Public Sub New(ByVal trans As IDbTransaction)
If trans Is Nothing Then Throw New ArgumentNullException("trans")
[Code]...
View 1 Replies
Mar 22, 2010
1. I have three classes. Now I want to know how do I convert object to my class runtime?
for example,
Dim obj as New Object
obj = CType( MyPassedObject, Class1 )
Now, in above code, MyPassedObject can be Class1 or Class2 or Class3 but it is passed as Object. So is there any way that I can convert into CType as per the type of that object, directly to that class? I mean, I can pass object name into parameter and write down CASE statement here to convert to that class but I do not want to write down Case statement for each of my class, so just want to know is it possible anyway?
I mean just for example, Can I do something like obj = CType( MyPassedObject, GetObject('Class1') or any other way?
2. Now for this obj, I am getting properties runtime and setting values and then updating.
for example,
Dim PropertyInfo As Reflection.PropertyInfo() = obj.GetType.GetProperties()
Now, Will it work fine if obj has been declared as Object and then I typeCast it runtime and then I get properties and set its values. Will it work if obj's variable type is Object or it must be that class type itself.
View 3 Replies
Dec 2, 2011
I am wondering if this is a bug. Can't understand why would CType work this way...!
View 1 Replies
Oct 1, 2010
I'm developing a custom control for numeric input that does some basic value range checking and have run into an issue when I set my property ValueMin equal to Long.MinValue. The IDE generates the following line in form1.designer.vb file that subsequently is flagged as generating an overflow.
Me.NumericInputBox1.ValueMin = CType(-9223372036854775808,Long)
I've done some checking using the Immediate Window with the following results.
?Long.MinValue
-9223372036854775808
?ctype(Long.MinValue, Long)
[Code].....
It appears that the CType function does not recognize the minimum Long value when the value is typed into the function, but works fine if it is passed Long.MinValue or a string with the value. It works fine for a typed in value equal to (Long.MinValue + 1). This problem only occurs with the Long (Int64) type.
View 5 Replies
Jul 17, 2010
In my program I have an interface iGraphable that contains two properties: Abscissa and Ordinate. Then I have an xxxx class (actually more than one) implementing iGraphable and a ListOfxxxx class implementing BindingListView(Of xxxx).To draw graphs I have a Graph class with a property called Data whose type is BindingListView(of iGraphable).Why have I a cast exception when I pass a BindingListView(Of xxxx) to the Data property.
View 3 Replies
Dec 14, 2011
I have the a function that is declared like so: Public Sub Modify(Of SIMType As {New, DAOBase})(ByVal obj As SIMType)
I also have a class called Products which is declared like so:
Public Class Products Inherits DAOBase
So as you can see, if I were to call this function like so:
Modify(Of Products)(new Products())
This would not be an issue. The issue actually arises when I try to cast the object being past in to its real type. For example: both do not work. I get a Value of type SIMTYPE cannot be converted to IMS.Products error. Im assuming this is because I am using generics. Is there a way to adjust my function to allow for a casting operation like I am trying to do? In the end, what I need is a reference of the actual type (Products in this case) to the object.
View 2 Replies
Nov 2, 2009
what exactly is the type of the second argument of CType
CType(testobject, Control)
what exactly is Control? how can Control be used as though it is an expression?i nid to know this because i'm trying to build a function that takes the exact parameters of CType, let's call it CType2public function CType2 (byref object as Object, byval thetype as Type) as Object problem is when i do this: CType2(testobject, Control) they give me "Control is a type and cannot be used as an expression".
View 6 Replies