473,788 Members | 2,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

??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(inpu t.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 5029
Try BitConverter class

--
http://www.alvas.net - Audio tools for C# and VB.Net developers
<we***@drexel.e du???????/???????? ? ???????? ?????????:
news:11******** *************@v 23g2000prn.goog legroups.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(inpu t.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.e duschrieb
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(inpu t.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(unsigned Int)

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(unsigned Int)

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.Intero pServices.Struc tLayout(Runtime .InteropService s.LayoutKind.Ex plicit)Public Structure Union
<Runtime.Intero pServices.Field Offset(0)Public xShort As Short
<Runtime.Intero pServices.Field Offset(0)Public xUShort As UShort
<Runtime.Intero pServices.Field Offset(0)Public xInteger As Integer
<Runtime.Intero pServices.Field Offset(0)Public xIntPtr As IntPtr
<Runtime.Intero pServices.Field Offset(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.e duschrieb
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(unsigned Int)

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.To Int16(BitConver ter.GetBytes(u) , 0)

But I guess your own function is faster.

In any case, you should enable Option Strict On.

Shared Function UShortToShort(B yVal 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.e dua écrit dans le message de news:
11************* ********@v23g20 00...legro 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(inpu t.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.e duschrieb:
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(inpu t.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.TryPars e' 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.e du" 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(inpu t.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.ed u wrote:
Dim wordVal As Short
wordVal = CShort(Val(inpu t.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

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

Similar topics

3
8186
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 understand what is typecasting in C++. Say I have a Base class and a Derived class, I have a pointer to an object to each. Base *ba = new Base; Derived *de = new Derived;
7
4545
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 returns a 'mytype'. In main() I use the operator on 'Test'-class objects. If 'mytype' is a pointer the operator works fine. But if I instead try to call an overloaded operator, the typecast operator is not invoked, and I get a compiler error...
2
4180
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 code*/
63
3420
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 0.00000 and no 10.0000. Can anybody tell me why it is like that and why typecasting i not done in this case?
11
4425
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 assignment. int b = some_value;
3
1647
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 deduced that the "S" can be replaced with (String __gc *) and the code will compile and run just fine. But what I can't find is what exactly Microsoft calls these macros (I have also seen "L" used the same way) and where they are all documented. I...
8
6004
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: D:\AUT_TEST\workspace\JyFIT>jython fit/JyFitServer2.py localhost 1234 23 localhost Traceback (innermost last): File "fit/JyFitServer2.py", line 146, in ?
12
4481
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 integer as a pointer, but the pointer is 8 bytes while the integer is only 4 bytes. Here's an example function:
9
1693
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 programs even appeared to work. The first case was passing directly a variable of type char **. Here's an example:
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
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
7517
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
6750
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
5398
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...
1
4069
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
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.