Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help me clean up some VB.NET coding
New Posts  All Forums:Forum Nav:

Help me clean up some VB.NET coding

post #1 of 3
Thread Starter 
I have option strict on and this is what happened:

Else
FoodItemsInteger += 1
TotalCalDecimal = (FatTextBox.Text * FAT_Integer) + (CarbsTextBox.Text * CARB_Integer) + (ProteinTextBox.Text * PROTEIN_Integer)
CalTextBox.Text = TotalCalDecimal.ToString
GramsCalTextBox.Text = CalTextBox.Text * FatTextBox.Text * CarbsTextBox.Text * ProteinTextBox.Text
FoodItemsInteger += 1
TotalItemsTextBox.Text = FoodItemsInteger.ToString
AvgTextBox.Text = GramsCalTextBox.Text / TotalItemsTextBox.Text
End If

What I underlined are errors after I turned on option strict. I guess this is because text =/= integer and therefore the calculations cannot be made. How can this be fixed?
Random name
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 4930k Asus P9X79 LE ATI HD5850 G.SKILL 4x4gb 2133mhz 
Hard DriveHard DriveCoolingOS
Samsung EVO 840 Seagate Barracuda Noctua NH-D14 W 8.1 
MonitorKeyboardPowerCase
Dell 24" 1920x1200 Tt eSports Poseiden brown Seasonic X-850 Fractal F4 
MouseMouse PadAudioAudio
Logitech G9x Steelseries Musiland 02 DAC KRK RP5 & Dayton SUB-80 
  hide details  
Reply
Random name
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 4930k Asus P9X79 LE ATI HD5850 G.SKILL 4x4gb 2133mhz 
Hard DriveHard DriveCoolingOS
Samsung EVO 840 Seagate Barracuda Noctua NH-D14 W 8.1 
MonitorKeyboardPowerCase
Dell 24" 1920x1200 Tt eSports Poseiden brown Seasonic X-850 Fractal F4 
MouseMouse PadAudioAudio
Logitech G9x Steelseries Musiland 02 DAC KRK RP5 & Dayton SUB-80 
  hide details  
Reply
post #2 of 3
Convert the strings to integers on the fly with CInt(expression)? I'm a complete newbie with VB.NET so there may be a better way.

EDIT: So it would become something like:

Code:
Else
 FoodItemsInteger += 1
 TotalCalDecimal = (CInt(FatTextBox.Text) * FAT_Integer) + (CInt(CarbsTextBox.Text) * CARB_Integer) + (CInt(ProteinTextBox.Text) * PROTEIN_Integer)
 CalTextBox.Text = TotalCalDecimal.ToString
 GramsCalTextBox.Text = CStr(CInt(CalTextBox.Text) * CInt(FatTextBox.Text) * CInt(CarbsTextBox.Text) * CInt(ProteinTextBox.Text))
 FoodItemsInteger += 1
 TotalItemsTextBox.Text = FoodItemsInteger.ToString
 AvgTextBox.Text = CStr(CInt(GramsCalTextBox.Text) / CInt(TotalItemsTextBox.Text))
End If
It looks complicated, so it must work.

EDIT 2: It doesn't work because there are integers and decimal numbers mixed. I couldn't 100% figure out your code but some CInt need to be replaced with CDec (or CSng or CDbl depending on the datatype).

Like so:

Code:
Else
 FoodItemsInteger += 1
 TotalCalDecimal = (CDec(FatTextBox.Text) * FAT_Integer) + (CDec(CarbsTextBox.Text) * CARB_Integer) + (CDec(ProteinTextBox.Text) * PROTEIN_Integer)
 CalTextBox.Text = TotalCalDecimal.ToString
 GramsCalTextBox.Text = CStr(CDec(CalTextBox.Text) * CDec(FatTextBox.Text) * CDec(CarbsTextBox.Text) * CDec(ProteinTextBox.Text))
 FoodItemsInteger += 1
 TotalItemsTextBox.Text = FoodItemsInteger.ToString
 AvgTextBox.Text = CStr(CDec(GramsCalTextBox.Text) / CInt(TotalItemsTextBox.Text))
End If
Wow, I managed to confuse myself in the process. Sorry about that, perhaps I should stick with studying VB.NET 101.
Edited by nostalgia - 10/20/11 at 12:07pm
The Black Box
(15 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE ASUS M5A99X EVO ASUS GTX 560 TI 448 Cores DCII Corsair Vengeance 2X4GB 1600MHz 
Hard DriveOptical DriveCoolingOS
Samsung SSD 830 Series 64GB Optiarc BD-RW BD-5300S Thermalright Ultima 90 Microsoft Windows 7 Home Premium 64-bit 
MonitorKeyboardPowerCase
BenQ G2320HDB Razer Arctosa Corsair AX750 Lian Li PC-C60 
MouseMouse PadAudio
Razer DeathAdder Razer Vespula Creative Sound Blaster X-Fi Titanium HD 
  hide details  
Reply
The Black Box
(15 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE ASUS M5A99X EVO ASUS GTX 560 TI 448 Cores DCII Corsair Vengeance 2X4GB 1600MHz 
Hard DriveOptical DriveCoolingOS
Samsung SSD 830 Series 64GB Optiarc BD-RW BD-5300S Thermalright Ultima 90 Microsoft Windows 7 Home Premium 64-bit 
MonitorKeyboardPowerCase
BenQ G2320HDB Razer Arctosa Corsair AX750 Lian Li PC-C60 
MouseMouse PadAudio
Razer DeathAdder Razer Vespula Creative Sound Blaster X-Fi Titanium HD 
  hide details  
Reply
post #3 of 3
Option Strict: On prevents automatic conversion of types (eg. your trying to multiply strings not Integers) As stated before you will have to convert the values. CInt is the conversion method from VB6...while supported in .NET its not suggested to use. Instead use the Convert.ToInt32 (or 16 or 64 or whatever you want). Listed below is the extremely large amount of Convert.(whatevers) I used based on what your code showed me about what type each was.

Another thing I noticed (its just me being picky) is that FoodItemsInteger has two separate += 1. From an efficiency stand point it would be better to do FoodItemsInteger += 2 before you set it in the box (unless for some reason I'm missing some usage of it in between the two separate += 1)

Code:
Else
    TotalCalDecimal = (Convert.ToInt32(FatTextBox.Text) * FAT_Integer) + (Convert.ToInt32(CarbsTextBox.Text) * CARB_Integer) + (Convert.ToInt32(ProteinTextBox.Text) * PROTEIN_Integer)
    CalTextBox.Text = TotalCalDecimal.ToString
    GramsCalTextBox.Text = Convert.ToString(TotalCalDecimal * Convert.ToDecimal(FatTextBox.Text) * Convert.ToDecimal(CarbsTextBox.Text) * Convert.ToDecimal(ProteinTextBox.Text))
    FoodItemsInteger += 2
    TotalItemsTextBox.Text = FoodItemsInteger.ToString
    AvgTextBox.Text = Convert.ToString(Convert.ToDecimal(GramsCalTextBox.Text) / Convert.ToDecimal(TotalItemsTextBox.Text))
End If
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II x4 955 ASUS Crosshair 3 EVGA 465GTX 1GB 8GB DDR3 1600mhz GSkill/ADATA 
Hard DriveOSMonitorKeyboard
40GB Intel SSD 1TB Western Digital Black Edition Windows 7 x64 Pro Dual 21.5 Sceptre Saitek Eclipse 
PowerCaseMouseMouse Pad
850watt Silverstone Element Coolermaster Haf 932 Razer Lachesis Razer Vespula 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II x4 955 ASUS Crosshair 3 EVGA 465GTX 1GB 8GB DDR3 1600mhz GSkill/ADATA 
Hard DriveOSMonitorKeyboard
40GB Intel SSD 1TB Western Digital Black Edition Windows 7 x64 Pro Dual 21.5 Sceptre Saitek Eclipse 
PowerCaseMouseMouse Pad
850watt Silverstone Element Coolermaster Haf 932 Razer Lachesis Razer Vespula 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help me clean up some VB.NET coding