473,382 Members | 1,705 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,382 software developers and data experts.

??problems typecasting in VB.NET??

Hi all,

I'm having problems trying to type cast in VB.Net. I'd like to read
in a 16 bit unsigned integer and convert it to a signed 16 bit
integer. I'm using the following line of code:

Dim wordVal As Short

wordVal = CShort(Val(input.Text))
output.Text = wordVal

Whenever input.Text 32767, I get a run-time error. Looking for some
suggestions.

Thanks in advance,
-weg

Nov 5 '07 #1
11 4979
Try BitConverter class

--
http://www.alvas.net - Audio tools for C# and VB.Net developers
<we***@drexel.edu???????/???????? ? ???????? ?????????:
news:11*********************@v23g2000prn.googlegro ups.com...
Hi all,

I'm having problems trying to type cast in VB.Net. I'd like to read
in a 16 bit unsigned integer and convert it to a signed 16 bit
integer. I'm using the following line of code:

Dim wordVal As Short

wordVal = CShort(Val(input.Text))
output.Text = wordVal

Whenever input.Text 32767, I get a run-time error. Looking for some
suggestions.

Thanks in advance,
-weg

Nov 5 '07 #2
<we***@drexel.eduschrieb
Hi all,

I'm having problems trying to type cast in VB.Net. I'd like to read
in a 16 bit unsigned integer and convert it to a signed 16 bit
integer. I'm using the following line of code:

Dim wordVal As Short

wordVal = CShort(Val(input.Text))
output.Text = wordVal

Whenever input.Text 32767, I get a run-time error. Looking for
some suggestions.
I'm not sure what you are trying to do. If you want to input values 32767,
why not use a different data type?
Armin

Nov 5 '07 #3
I'm not sure what you are trying to do. If you want to input values 32767,
why not use a different data type?
I'm not sure what you are trying to do. If you want to input values 32767,
why not use a different data type?

Armin
To convert from an unsigned int to a signed int, the code would read
something like this:

If unsignedInt 32767 Then
signedInt = unsignedInt - 65535
Else
signedInt = unsignedInt
End If

I would like to do the above conversion by typecasting so I don't have
to call a function like above every time I read in a value. I would
rather do something like:

signedInt = CShort(unsignedInt)

but I get an overflow error.
Nov 5 '07 #4
To convert from an unsigned int to a signed int, the code would read
something like this:

If unsignedInt 32767 Then
signedInt = unsignedInt - 65535
Else
signedInt = unsignedInt
End If

I would like to do the above conversion by typecasting so I don't have
to call a function like above every time I read in a value. I would
rather do something like:

signedInt = CShort(unsignedInt)

but I get an overflow error.

Casting in VB usually implies a conversion. I assume from your original
that you want to do something like sx=(SHORT)ux from the good/bad old days
which does no conversion. The CShort() call is failing because it can't
convert (not cast) 65000 into a Short.

If you want economy and legibility of source code, call a function.

If you really want to do an old C style cast in VB, then you probably have
to make the equivalent of a union, eg
<Runtime.InteropServices.StructLayout(Runtime.Inte ropServices.LayoutKind.Explicit)Public Structure Union
<Runtime.InteropServices.FieldOffset(0)Public xShort As Short
<Runtime.InteropServices.FieldOffset(0)Public xUShort As UShort
<Runtime.InteropServices.FieldOffset(0)Public xInteger As Integer
<Runtime.InteropServices.FieldOffset(0)Public xIntPtr As IntPtr
<Runtime.InteropServices.FieldOffset(0)Public xByte As Byte
etc as with as much as you want.....
End Structure

Then you can have one of these things laying around, eg
Dim z as Union
and cast like this
dim u as UShort
dim s as Short = whatever
z.xShort = s : u = z.xUShort ' no function calls, but two assignments

I personally don't like this kind of thing in VB, and I think the function
call is the better answer. But I think this gets you as close as possible in
VB to the cast you are looking for.

Nov 5 '07 #5
<we***@drexel.eduschrieb
I'm not sure what you are trying to do. If you want to input
values 32767, why not use a different data type?
I'm not sure what you are trying to do. If you want to input
values 32767, why not use a different data type?

Armin

To convert from an unsigned int to a signed int, the code would read
something like this:

If unsignedInt 32767 Then
signedInt = unsignedInt - 65535
Else
signedInt = unsignedInt
End If

I would like to do the above conversion by typecasting so I don't
have to call a function like above every time I read in a value. I
would rather do something like:

signedInt = CShort(unsignedInt)

but I get an overflow error.
/Casting/ is not possible because casting must be type safe (Short and
UShort are not derived from each other). /Converting/ is possible, either
they way you did above, or use the BitConverter class, as suggested by
Alexander before (now that I know what you are trying I also
suggest it).

Dim s As Short
Dim u As UShort = 40000
s = BitConverter.ToInt16(BitConverter.GetBytes(u), 0)

But I guess your own function is faster.

In any case, you should enable Option Strict On.

Shared Function UShortToShort(ByVal u As UShort) As Short
If u 32767 Then
Return CShort(CInt(u) - 65536)
Else
Return CShort(u)
End If
End Function
Armin
Nov 5 '07 #6
This is expected as the max value for a short is 32767. So you'll never be
able to "convert" a ushort value that is 40000 into a short value that would
have the same absolute value (if you meant the internal representation
you'll get a negative value and I'm not sure this is what you want as this
could lead to wrong condition for whatever calculation you would do on this)

For now, possible options would be :
- use Ushort variables
- use a wider datatype such as integer so that you can store a value such as
40000 that you can't store as such in a Short
- the last resort would be to use the same storage pattern but then the
value will be negative so it really depends what you are trying to do and
could be risky if you have some computation later in your code...

<we***@drexel.edua écrit dans le message de news:
11*********************@v23g2000prn.googlegroups.c om...
Hi all,

I'm having problems trying to type cast in VB.Net. I'd like to read
in a 16 bit unsigned integer and convert it to a signed 16 bit
integer. I'm using the following line of code:

Dim wordVal As Short

wordVal = CShort(Val(input.Text))
output.Text = wordVal

Whenever input.Text 32767, I get a run-time error. Looking for some
suggestions.

Thanks in advance,
-weg

Nov 5 '07 #7
<we***@drexel.eduschrieb:
I'm having problems trying to type cast in VB.Net. I'd like to read
in a 16 bit unsigned integer and convert it to a signed 16 bit
integer. I'm using the following line of code:

Dim wordVal As Short

wordVal = CShort(Val(input.Text))
output.Text = wordVal

Whenever input.Text 32767, I get a run-time error. Looking for some
suggestions.
'Short' is a signed data type ('Int16'). You may want to use 'UShort'
instead. I suggest to use 'UShort.TryParse' to parse the user input instead
of 'CShort(Val(...))'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 5 '07 #8
You can not convert all unsigned int16s to signed int16s
An unsigned int16 has 16 bits of data, a signed int16 has a sign bit and 15
bits of data, so it will not fit.
You will have to convert it to an int32/integer

Guy

"we***@drexel.edu" wrote:
Hi all,

I'm having problems trying to type cast in VB.Net. I'd like to read
in a 16 bit unsigned integer and convert it to a signed 16 bit
integer. I'm using the following line of code:

Dim wordVal As Short

wordVal = CShort(Val(input.Text))
output.Text = wordVal

Whenever input.Text 32767, I get a run-time error. Looking for some
suggestions.

Thanks in advance,
-weg

Nov 6 '07 #9
we***@drexel.edu wrote:
Dim wordVal As Short
wordVal = CShort(Val(input.Text))
Whenever input.Text 32767, I get a run-time error.
Looking for some suggestions.
Well; what do you know?
The old, VB "Proper" trick of "side-stepping" out to Hexadecimal and
back again still works!

Dim unsigned as UInt16 = 40000
Dim signed as Int16 _
= CShort(Val("&H" & Hex(unsigned)))

HTH,
Phill W.
Nov 6 '07 #10
Yes Phil, but 40000 then becomes -25536 :-)

Guy

"Phill W." wrote:
we***@drexel.edu wrote:
Dim wordVal As Short
wordVal = CShort(Val(input.Text))
Whenever input.Text 32767, I get a run-time error.
Looking for some suggestions.

Well; what do you know?
The old, VB "Proper" trick of "side-stepping" out to Hexadecimal and
back again still works!

Dim unsigned as UInt16 = 40000
Dim signed as Int16 _
= CShort(Val("&H" & Hex(unsigned)))

HTH,
Phill W.
Nov 7 '07 #11
guy wrote:
but 40000 then becomes -25536 :-)
Exactly what I would expect.

Read up about the Short Data Type and its limitations.

You can't fit a quart into a pint pot; if it holds the pint, it's doing
the best it can...

Regards,
Phill W.
Nov 7 '07 #12

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

Similar topics

3
by: Kapil Khosla | last post by:
Hi, I have been trying to understand this concept for quite sometime now somehow I am missing some vital point. I am new to Object Oriented Programming so maybe thats the reason. I want to...
7
by: Nicolay Korslund | last post by:
Hi! I'm having a little trouble with the typecast operator, can anybody help me with the rules for when the this operator is invoked? In the class 'Test' in the code below, the typecast operator...
2
by: Arun Prasath | last post by:
Hi all, I have the following question regd pointer typecasting. Is the following type of pointer typecasting valid? #define ALLOC(type,num) ((type *)malloc(sizeof(type)*num)) /*begin...
63
by: andynaik | last post by:
Hi, Whenever we type in this code int main() { printf("%f",10); } we get an error. We can remove that by using #pragma directive t direct that to the 8087. Even after that the output is...
11
by: Vinod Patel | last post by:
I have a piece of code : - void *data; ...... /* data initialized */ ...... struct known_struct *var = (struct known_struct*) data; /*typecasting*/ How is this different from simple...
3
by: jdm | last post by:
In the sample code for the SortedList class, I see the use of a string typecasting macro consisting of a single letter "S". i.e.: Sortedlist->Add(S"Keyval one", S"Item one"); Now I have...
8
by: Mark Fink | last post by:
I try to port a server application to Jython. At the moment I use Jython21\Lib\socket.py Currently I do face problems with casting the string "localhost" to the desired value:...
12
by: bwaichu | last post by:
What is the best way to handle this warning: warning: cast from pointer to integer of different size I am casting in and out of a function that requires a pointer type. I am casting an...
9
by: bwaichu | last post by:
I am starting this thread after having run into two separate programming problems, where the compiler offered no help. The compiler did not even warn. The programs compiled fine. And the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.