473,693 Members | 2,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TryCast operand must be reference type

Hi,

The code below generates a compile error on line 3 (dim d...)

Dim o As Object = "1900-01-01"
Dim s As String = TryCast(o, String)
Dim d As DateTime = TryCast(o, DateTime)

The error is : TryCast operand must be reference type, but 'Date' is a value
type.

How can I resolve this?

The second line of code does not generate a compile error at all.

Thanks.

Leo Leys

Jun 2 '07 #1
4 24567
Hello,

Yes, TryCast can only work with reference types because it needs to be
able to return Nothing in case the cast fails and you can't set a value
type to Nothing. You need to use CType instead.

Dim d As DateTime = CType(o, DateTime)

Kelly

Leo Leys wrote:
Hi,

The code below generates a compile error on line 3 (dim d...)

Dim o As Object = "1900-01-01"
Dim s As String = TryCast(o, String)
Dim d As DateTime = TryCast(o, DateTime)

The error is : TryCast operand must be reference type, but 'Date' is a value
type.

How can I resolve this?

The second line of code does not generate a compile error at all.

Thanks.

Leo Leys
Jun 2 '07 #2
I'm used to C# and therefore I'm not too up to date with VB.Net syntax.

However, you should not try a cast operation in your case (a cast is used to
convert between compatible type (types for which an implicit or explicit
convertion is defined).
Your second line works simply because your "o" object is a string and you
convert it to a string. In any case, because ToString() method is defined
for all object, you will be always able to cast to a string. The 3rd line
has a problem. Your "o" object has no knowledge about how to convert to a
DateTime type.
What you should do is a parse operation:

Module Module1
Sub Main()
Dim str As String = "1900-01-01"
Dim dt As DateTime
If (DateTime.TryPa rse(str, dt)) Then
Console.WriteLi ne(dt)
End If
Console.ReadLin e()
End Sub
- José
"Leo Leys" <Le*****@discus sions.microsoft .coma écrit dans le message de
news: C6************* *************** **...icrosof t.com...
Hi,

The code below generates a compile error on line 3 (dim d...)

Dim o As Object = "1900-01-01"
Dim s As String = TryCast(o, String)
Dim d As DateTime = TryCast(o, DateTime)

The error is : TryCast operand must be reference type, but 'Date' is a
value
type.

How can I resolve this?

The second line of code does not generate a compile error at all.

Thanks.

Leo Leys

Jun 2 '07 #3
Thanks for your answer. In my application I'm dependent on user input, which
is stored in the variable o.
CType is not suitable for me because it throws an exception if the o
variable contains an invalid date string.
Kind regards
Leo Leys

"Killian35" wrote:
Hello,

Yes, TryCast can only work with reference types because it needs to be
able to return Nothing in case the cast fails and you can't set a value
type to Nothing. You need to use CType instead.

Dim d As DateTime = CType(o, DateTime)

Kelly

Leo Leys wrote:
Hi,

The code below generates a compile error on line 3 (dim d...)

Dim o As Object = "1900-01-01"
Dim s As String = TryCast(o, String)
Dim d As DateTime = TryCast(o, DateTime)

The error is : TryCast operand must be reference type, but 'Date' is a value
type.

How can I resolve this?

The second line of code does not generate a compile error at all.

Thanks.

Leo Leys
Jun 4 '07 #4
Hi,
Thanks for your reply. The tryparse method is exactly what I needed.
Many thanks.
Kind regards
Leo Leys

"José Joye" wrote:
I'm used to C# and therefore I'm not too up to date with VB.Net syntax.

However, you should not try a cast operation in your case (a cast is used to
convert between compatible type (types for which an implicit or explicit
convertion is defined).
Your second line works simply because your "o" object is a string and you
convert it to a string. In any case, because ToString() method is defined
for all object, you will be always able to cast to a string. The 3rd line
has a problem. Your "o" object has no knowledge about how to convert to a
DateTime type.
What you should do is a parse operation:

Module Module1
Sub Main()
Dim str As String = "1900-01-01"
Dim dt As DateTime
If (DateTime.TryPa rse(str, dt)) Then
Console.WriteLi ne(dt)
End If
Console.ReadLin e()
End Sub
- José
"Leo Leys" <Le*****@discus sions.microsoft .coma écrit dans le message de
news: C6************* *************** **...icrosof t.com...
Hi,

The code below generates a compile error on line 3 (dim d...)

Dim o As Object = "1900-01-01"
Dim s As String = TryCast(o, String)
Dim d As DateTime = TryCast(o, DateTime)

The error is : TryCast operand must be reference type, but 'Date' is a
value
type.

How can I resolve this?

The second line of code does not generate a compile error at all.

Thanks.

Leo Leys


Jun 4 '07 #5

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

Similar topics

6
2840
by: Lenn | last post by:
Hi, Could someone clarify my confusion regarding passing reference types to a method with ref keyword and explain when it's practical to use it. It's my understanding that in .NET reference types hold a reference to an object as opposed to object data itself. So, when reference type parameter is passed into a method, a copy of objects reference is passed in, so called method can do whatever to "original" object and a caller will see...
13
2788
by: Maxim | last post by:
Hi! A have a string variable (which is a reference type). Now I define my Method like that: void MakeFullName(string sNamePrivate) { sNamePrivate+="Gates" }
3
6607
by: Scott M. | last post by:
If I pass a reference type ByVal, am I making a copy of the object on the heap or am I making a copy of a pointer to the object on the heap? If I pass a string object (reference type) into a sub ByVal and modify it, the original string doesn't change. If I pass it ByRef it does. This makes it seem like the actual type (reference or value) that is being passed is irrelevant and that only the ByVal or ByRef keyword matters. Is this...
6
13572
by: BlueTrin | last post by:
Hello I was adapting a C version of SolvOpt in C++ to use it within a virtual class. However I am stuck with the overriding of evaluation and gradiant functions. cStepCurveEvaluator.cpp cStepCurveEvaluator.cpp(14) : error C2296: '.*' : illegal, left operand
9
1889
by: Edward Diener | last post by:
Can one use 'ref' ( or 'out' ) on a reference type to create a reference to a reference in C#. I know one can use it on a value type to create a reference to that value.
3
9553
by: DaTurk | last post by:
I was just curious. I know that a struct is value type by definition, and will be placed on the stack, but what if you have a reference type in the struct, say a string, or an array. Where will that be placed?
29
23193
by: aarthi28 | last post by:
Hi, I have written this code, and at the end, I am trying to write a vector of strings into a text file. However, my program is nor compiling, and it gives me the following error when I try to write to the file: error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) I don't know what I am doing wrong. I have posted my entire program
2
7154
by: Mark05 | last post by:
#include<conio.h> #include<iostream> using namespace std; int main() { double ManfCode,ProdCode; int Checkdigit, SecondManf, FourthManf; int FirstProd,ThirdProd,FifthProd; int SecondProd,ForthProd;
1
12858
by: Terry Archer | last post by:
error C2296: '%' : illegal,left operand has type 'double' #include <iostream> #include <fstream> #include <iomanip> using namespace std; const int OUNCES_PER_POUND = 16; const double POUNDS_PER_KILOGRAM = 2.2;
0
9107
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
8971
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
7647
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6479
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
5825
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4332
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
4567
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2992
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
1970
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.