.net - Difference Between Casting In C# And It
Dec 9, 2009
The next code works fine in C#:
[Code]...
But this code crash with a OverflowException in VB.Net.
[Code]...
Both codes seems the same to me. What is the difference and how can I get the C# code converted to VB.Net?
View 6 Replies
ADVERTISEMENT
Mar 13, 2009
I have the following problem with MEF: Interface definition to be used by host:
Public Interface IExecuteDoSomething
Inherits IAddinSettings
Event DataReceived As EventHandler(Of DataReceivedEventArgs)
Function DoSomething() As Boolean
End Interface
[Code]...
Everything seems to work fine until the Button2_Click routine is executed, then an InvalidCastException is thrown with the info:Unable to cast object of type 'System.Collections.Generic.List1[SharedLibrary.IExecuteDoSomething]' to type 'System.Collections.Generic.List1[SharedLibrary.IAddinSettings]'.
How can i solve this problem, because the imported object implements both of the interfaces?
View 1 Replies
Aug 19, 2011
I have a object type variable (control .Tag) that I need to cast to a structured type, and change a member in. This is a contrived but representative example
[code]...
View 2 Replies
Oct 16, 2009
get around this. I have a data base field that is a decimal data type and I want to display it in a text box with the following txtWeekdayRate = CType(RoomsDataRow!WeekdayRate, TextBox)I get the following error when I run my program.Unable to cast object of type 'System.Decimal' to type 'System.Windows.Forms.TextBox'.
View 20 Replies
Mar 1, 2009
I am getting the followin error from my code. System.InvalidCastException was unhandled Message="Unable to cast object of type 'QADBASE.DMRDataLists' to type 'QADBASE.DMRData'." Source="QADBASE" I am getting the error in the code below on the createDMRIssue
[code]...
View 1 Replies
Apr 20, 2010
Public Function CastToT(Of T)(ByVal GenericType(Of Object) data) As GenericType(Of T)Return DirectCast(data, GenericType(Of T))End Function
The above clearly does not work. Is there any way to perform this cast if I know that all objects inside data are in fact of Type T?
View 2 Replies
Jan 12, 2011
I have an Entity class which Implements IWeightable:
Public Interface IWeightable
Property WeightState As WeightState
End Interface
I have a WeightCalculator class:
Public Class WeightsCalculator
Public Sub New(...)
..
End Sub
Why can I not do wc.Calculate(entities)? I receive:
Unable to cast object of type
'System.Collections.Generic.List1[mynameSpace.Entity]'
to type
'System.Collections.Generic.IList1[myNamespace.IWeightable]'.
If Entity implements IWeightable why is this not possible?
View 2 Replies
Mar 11, 2011
I am passing Date Field to SQL Database. Field is declared as String in the VB.Net Application. I wrote a piece of code for casting to Date
[Code]...
View 2 Replies
Apr 14, 2009
I'm a C# programmer who is forced to use VB (eh!!!!). I want to check multiple controls state in one method, in C# this would be accomplished like so:
if (((CheckBox)sender).Checked == true)
{
// Do something...
[code]...
View 4 Replies
Apr 27, 2012
I have tried everything to no avail. I need to cast the value from a user selected dropdownlist to another dropdownlist on another page. The data in the textboxes are a series of numbers. Also is there a way to display the value in the second dropdown list as well as other values? For example, my dropdown has the values 1-6, user selects 4, which displays first in the dropdown on page 2 but the other values also display in case they want to change there selection??
If Not Page.PreviousPage Is Nothing Then
Dim table As Control = PreviousPage.Controls(0).FindControl("table1")
Dim ddl As DropDownList = CType(table.FindControl("ddlB_Codes"),c DropDownList)
[code].....
View 1 Replies
Mar 1, 2010
I'm trying to obfuscate some VB.NET 2003 app.The resulting assemblyes are obfuscated and "run" with some errors.I cleaned all potential reflection problems, but I'm not being able to read the selected value of a combobox.
I load the Combobox using their Datasource properties, using a collection of "VTPair" (a class created by me with 2 properties: one of string type and other of object type to store the value)
This combobox handle pairs like "Male | M" or "Female | F".When trying to see what is selected, I use if mycombo1.SelectedValue = "M" then.This code, after obfuscation, throws me an exception that cannot cast type "XX" to string "M".
So, I changed the code to something more correct, explicitly casting the selected value to String:
if ctype(mycombo1.SelectedValue,string) = "M" then But the error is the same.Debugin my original code, the SelectedValue property is of type "Object" but it is a string.I tried using the ComboBox.SelectedItem property that is also an object but this time what is inside is of type "VTPair" (my own class) and then trying to access its "Value" property (which is of type Object) and trying to cast to string fails again.
Does anyone have an idea to "translate" this piece of code to work OK after Dotfucate it?
View 2 Replies
May 14, 2010
I have an object that inherits from an interface. I can create and return the following:
Dim o As IRequest
o = New Request
Return o
This works fine. Now I want to return a List(Of Requests), so I try the following:
Dim o As List(Of IRequest)
o = New List(Of Request)
Return o
The error below is being given on o = New List(Of Request)
System.Collections.Generic.List(Of MyObjects.Request)' cannot be converted to System.Collections.Generic.List(Of MyInterfaces.IRequest)'.Consider using 'System.Collections.Generic.IEnumerable(Of MyInterfaces.IRequest)'
View 2 Replies
Nov 4, 2010
I don't know where it thinks I'm casting.
Here is my property
''' <summary>
''' Gets the GetX509Certificate from a file.
''' </summary>
[Code]....
and heres where it says im doing the illegal cast:
Dim cert As System.Security.Cryptography.X509Certificates.X509Certificate2 = Me.GetX509Certificate
Why is it saying that? Everything is defined in the right type but it still thinks I'm casting.
View 1 Replies
Apr 20, 2012
in vb, you can use text which is in textbox instead of numerical expressions. I mean, if I write 50 in textbox1 and also I can write like that code:
Dim result As Double = TextBox1.Text + 0.4
as you see, I didnt convert any type but it works in vb.net
But I want to learn what are disadvantages of this using?
(I am just talking about arithmetic operations)
View 2 Replies
Jun 22, 2009
i have a property (items) that i want to use for either a checkedlistbox or a standard listbox depending on the value of another property.
heres the code i wrote that doesn't work:
[Code]...
View 5 Replies
Nov 14, 2010
Say I have a interface called IProperties and I have a collection called colMyProperties that is a collection of IProperties. Then I have an object called clsProperty that implements the IProperty interface. Now, in my code where I add objects to the colMyProperties collection I add a bunch of clsProperty objects. so far so good.
Now when I try to access an object in the collection I can only access properties and methods of the IProperties interface, not the clsProperty object. What am I doing wrong? Do I need to cast the object?
View 2 Replies
Sep 4, 2009
I usually avoid VB's built-in conversion functions (CStr, CDate, CBool, CInt, etc.) unless I need to do an actual conversion. If I'm just casting, say from an object to a string, I normally use either DirectCast or TryCast, under the assumption that CStr, etc., are doing some extra stuff I don't need. But sometimes the DirectCast syntax is a little cumbersome, as in the following example.[code]SqlDataReader.Item returns an Object, which needs to be cast to a String. CStr is easier to read, type, and explain (IMO). My question is, does it matter which one I use? Should I just go with CStr (and CDate and CBool, etc.) and not worry about the extra work I assume those functions are doing? Is there any other downside to using these functions?
View 3 Replies
Feb 28, 2012
i have the following code which walks through an Excel file and this works fine note that the following line does work well (folder is being set earlier in the code)
[Code]...
View 1 Replies
May 14, 2009
When I have many controls on a Form (i.e. Label, Button etc) that do almost the same thing, I often use one method to handle all the controls Click, MouseDown, MouseUp events.But to know which of the controls throwing the event and access the properties of that control I need to cast the "sender" object to the correct type.
The thing is that I always know which type it is, I don't really have to "TryCast", "DirectCast" and check if the operation returns true. I some times use CType as well.
[Code]...
View 2 Replies
Jun 18, 2009
Delegate Sub UpdateTextHandler(ByVal recbuffpass() As Integer)
Private Sub someSub()
Dim f As frmMain = My.Application.OpenForms("frmMain")
f.Invoke(New UpdateTextHandler(AddressOf f.UpdateTextMethod), New Object() {recbuf})
[code].....
View 2 Replies
Sep 30, 2009
I've got a project called Service.Inside the project, i've got 2 windows classes.ClassA is the main windows class which opens up when I run the program.On ClassA, I've got a button which, when I click it, opens up ClassB which is the other windows class. Inside the button's eventhandler (click), I've got a string which contains the name of ClassB's class ie "ClassB". I will probably get this from a table. Is it possible to initiate the object ClassB without using if statments to check what the type of object is. In the code below, i've managed to create a o of type object, but i need to new it with the ClassB
vb.net Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
[Code]...
View 7 Replies
Aug 1, 2011
Apparently, this is not allowed? Is there a way to cast to an UInt16?
zero2 = CUInt16((Ch1OutputResults.range10Zero - 0) * 65535)
View 8 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
Aug 18, 2010
I have the following VB.NET code that I am using to sort a Data.DataTable by column count.
For Each dtTarget As Data.DataTable In _
From x In Target.Tables _
Where DirectCast(x, Data.DataTable).Rows.Count > 0 _
Order By DirectCast(x, Data.DataTable).Columns.Count
...
Next
Is there a way to indicate that x is a Data.DataTable without having to DirectCast it each time it is referenced (twice in this case) in the LINQ query?
View 1 Replies
Oct 22, 2009
I have to pack and unpack a 16bit Int from/into a Ushort in VB.net This is how I thought I could do it (doesn't work, gives me overflow exception)
'Pack Int16 into ushort '
Dim usPacked = CType(Data, UShort)
'unpack Int16 from ushort '
Dim unpacked = CType(data,Int16)
View 2 Replies
Jun 28, 2010
I wonder if this is not possible dim mystring as string="1" dim myint As Nullable(Of Integer) myint=0 myint=mystring It crashes on the last line why is that? mystring is already integer (1) , i dont have to cast using cint, i thought. option strict off- if this cause the problem
View 7 Replies
Aug 21, 2009
In C# you can use the implicit keyword to define an implicit user-defined type conversion operator.In VB.NET you can define a CType conversion operator that will explicitly convert a user-defined type into another type.Is there a way to declare an implicit conversion operator in VB.NET?
View 1 Replies
Feb 29, 2012
I'm trying to implement a grid based RPG style inventory (teaching myself by making a game keeps me focused ) and I have an array of "item" objects. Part of this array is a picture box, which I then lay out in code to display the inventory. how to detect when one of these picture boxes is clicked. However, I'm having a little trouble getting my head around the Directcast command.
When a box is clicked, I would like the function 'boardclicked' to pass over not only the picture box, but the whole object it is associated with it, so I can print these values out the screen (detailing the objects name, value etc) [Code]. As you can see, this passes the picture box element of the object, but I'd like to be able to pass the whole structure, so I can retrieve the other associated values.
View 3 Replies
Apr 12, 2010
The question is intended for lazy VB programmers. In vb I can do and I won't get any errors.
Example 1
Dim x As String = 5
Dim y As Integer = "5"
Dim b As Boolean = "True"
[Code]...
View 4 Replies
Feb 26, 2009
When writing some Outlook code in VS2005, I came across a situation that, to me, does not make any sense. For reference, the version of Outlook running on this machine is Outlook 2003.The program I'm writing is designed to iterate through the emails in the inbox, to identify certain types of emails based on their subjects and bodies, and then to generate new emails based on the existing ones. However, the problem that I'm having is with simply iterating through the collection of items that comes from the Inbox folder. Specifically, what happens is that this line consistently fails on items that should be processed:
Current_Item = CType(The_Items.Item(Counter), Outlook.MailItem)
View 4 Replies