473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

HELP: Cast as Type VS 2003

I would like to cast an object to a value type specified by a variable of
Type

Function ReturnTest(InputVar as Object) as Object

Dim DataType as Type = GetType(System.String)

If TypeOf (InputVar) Is DataType Then
Return CType(InputVar, Datatype)
End If

End Function

I have tried gettype, system.type.gettype and I am getting no where fast.
Anyone have any ideas what I am doing wrong?
Dec 2 '05 #1
5 1183
You are going nowhere, to be honest.

The bottom line, your function is declared as returning an Object. No matter
what casts you do, it will always return Object.

So even if CType could do what you are trying to make it tdo (which I
believe is not possible), it still would not matter because every return of
your function will be seen as an Object, no matter what.

"AMDRIT" <am****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
I would like to cast an object to a value type specified by a variable of
Type

Function ReturnTest(InputVar as Object) as Object

Dim DataType as Type = GetType(System.String)

If TypeOf (InputVar) Is DataType Then
Return CType(InputVar, Datatype)
End If

End Function

I have tried gettype, system.type.gettype and I am getting no where fast.
Anyone have any ideas what I am doing wrong?

Dec 2 '05 #2
Isn't what I was attempting referred to as Unboxing? Doens't the signature
for the value get set to the desired type? Is everything actually an
object?

example:

Private Sub Testing

Dim dblValue As Double
dblValue = 123
Debug.Print(String.Format("Raw {0}", TypeName(dblValue)))
Debug.Print(String.Format("ReturnTest {0}",
TypeName(ReturnTest(dblValue))))
Debug.Print(String.Format("ReturnTest1 {0}",
TypeName(ReturnTest1(dblValue))))
Debug.Print(String.Format("ReturnTest2 {0}",
TypeName(ReturnTest2(dblValue))))

End Sub

Function ReturnTest(ByVal InputVar As Object) As Object
Return CType(InputVar, Integer)
End Function

Function ReturnTest1(ByVal InputVar As Object) As Object
Return CType(InputVar, String)
End Function

Function ReturnTest2(ByVal InputVar As Object) As Object
Return CType(InputVar, Double)
End Function

Output

Raw Double
ReturnTest Integer
ReturnTest1 String
ReturnTest2 Double

"Marina" <so*****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
You are going nowhere, to be honest.

The bottom line, your function is declared as returning an Object. No
matter what casts you do, it will always return Object.

So even if CType could do what you are trying to make it tdo (which I
believe is not possible), it still would not matter because every return
of your function will be seen as an Object, no matter what.

"AMDRIT" <am****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
I would like to cast an object to a value type specified by a variable of
Type

Function ReturnTest(InputVar as Object) as Object

Dim DataType as Type = GetType(System.String)

If TypeOf (InputVar) Is DataType Then
Return CType(InputVar, Datatype)
End If

End Function

I have tried gettype, system.type.gettype and I am getting no where fast.
Anyone have any ideas what I am doing wrong?


Dec 2 '05 #3
In reality, you maybe returning a string, or an ArrayList or whatever.
However, as far as the return type, it is always boxed as an object. As long
as the return type is declared as Object, as far as anything calling the
method is concerned, it is getting an object back. That object can then be
unboxed to whatever it actually is, but as far as the compiler is concerned,
your function is returning something of type Object.

"AMDRIT" <am****@hotmail.com> wrote in message
news:e4**************@TK2MSFTNGP11.phx.gbl...
Isn't what I was attempting referred to as Unboxing? Doens't the
signature for the value get set to the desired type? Is everything
actually an object?

example:

Private Sub Testing

Dim dblValue As Double
dblValue = 123
Debug.Print(String.Format("Raw {0}", TypeName(dblValue)))
Debug.Print(String.Format("ReturnTest {0}",
TypeName(ReturnTest(dblValue))))
Debug.Print(String.Format("ReturnTest1 {0}",
TypeName(ReturnTest1(dblValue))))
Debug.Print(String.Format("ReturnTest2 {0}",
TypeName(ReturnTest2(dblValue))))

End Sub

Function ReturnTest(ByVal InputVar As Object) As Object
Return CType(InputVar, Integer)
End Function

Function ReturnTest1(ByVal InputVar As Object) As Object
Return CType(InputVar, String)
End Function

Function ReturnTest2(ByVal InputVar As Object) As Object
Return CType(InputVar, Double)
End Function

Output

Raw Double
ReturnTest Integer
ReturnTest1 String
ReturnTest2 Double

"Marina" <so*****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
You are going nowhere, to be honest.

The bottom line, your function is declared as returning an Object. No
matter what casts you do, it will always return Object.

So even if CType could do what you are trying to make it tdo (which I
believe is not possible), it still would not matter because every return
of your function will be seen as an Object, no matter what.

"AMDRIT" <am****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
I would like to cast an object to a value type specified by a variable of
Type

Function ReturnTest(InputVar as Object) as Object

Dim DataType as Type = GetType(System.String)

If TypeOf (InputVar) Is DataType Then
Return CType(InputVar, Datatype)
End If

End Function

I have tried gettype, system.type.gettype and I am getting no where
fast. Anyone have any ideas what I am doing wrong?



Dec 2 '05 #4
I can see how it confuses you... it's a confusing subject.

Whenever you assign a value (such as the number 15) to an object, it gets
'boxed' automatically. So simply calling this function, which assigns the
value to the parameter InputVar, will automatically box it for you -- you
don't have to do anything else.

Having a function that boxes is actually not necessary. All you need to do
is make sure that the variable you're assigning the value to is of type
object, and it's a done deal.

You may find this article of use:
http://www.codersource.net/csharp_boxing_unboxing.html

Even though it's C# the concepts are the same.

Hope that helps,
John

"AMDRIT" <am****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
I would like to cast an object to a value type specified by a variable of
Type

Function ReturnTest(InputVar as Object) as Object

Dim DataType as Type = GetType(System.String)

If TypeOf (InputVar) Is DataType Then
Return CType(InputVar, Datatype)
End If

End Function

I have tried gettype, system.type.gettype and I am getting no where fast.
Anyone have any ideas what I am doing wrong?

Dec 3 '05 #5
Admrit,

Is this what you want to do?
\\\
test("hello")
test(1)

Public Sub test(ByVal obj As Object)
If TypeOf obj Is String Then
MessageBox.Show(obj.ToString)
Else
MessageBox.Show("I am not a string")
End If
End Sub
///

I hope this helps,

Cor
Dec 3 '05 #6

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

Similar topics

18
by: Graham Nicholls | last post by:
Hi. I'm having some fun with numbers. I've extraced an image sizes from a jpeg file img_x,img_y=image.getsize() then I'm trying to use those sizes to scale the image, but because python...
5
by: Rex_chaos | last post by:
Hi all, I have a question about datatype converting. Consider the following types std::complex<double> and struct MyComplex { double re, im; }
2
by: Vilson farias | last post by:
Greetings, I was executing some queries when I discovered that 7.3.4's CAST is a little different from 7.1.2 when working with timestamps. When working with localized timestamps, I can't use...
10
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some...
11
by: Jim H | last post by:
I am trying to go through my Outlook (2003) address book. The code goes through the Items list and prints all the last names but after it hits 114 (out or 126 contacts) I get a "sepcified cast is...
3
by: Laura T. | last post by:
The following code is driving me so crazy, that I'm thinking it's a "feature" or a.. ....dare I say it... a BUG. I'm using VS 2003. I've created a new value type (struct) called MyInt. MyInt has...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
6
by: pdabak | last post by:
Hello, I had an ASP.NET application running on Windows 2003 machine and it was all working fine. I upgraded the machine to Windows 2003 SP1 and now my application is failing with "Specified cast...
36
by: Cap'n Ahab | last post by:
I have used VB3 - VB6, so learning all this OO stuff is reasonably new to me (although I looked at Java a few years ago). Anyway, I thought I would write a small class to begin with, with a...
2
by: sherifffruitfly | last post by:
Hi, I'm using an adaptation of excel-reading code that's all over the internet - I don't much like or understand it, but it has worked for me in the past.... beggars can't be choosers... : ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...

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.