-
Feb 6th, 2025, 08:38 PM
#1
Thread Starter
PowerPoster
[RESOLVED] I don't understand why I can't copy this function to another application.
Hi,
I've copied most of this code from online, I've changed some of it to make a little test application to determine if my laptop is running on the battery or not and it runs correctly.
Code:
Option Explicit On
Public Class Form1
Dim stat As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
Public Function GetBatteryStatus() As Integer
Dim obj As Object, obj2 As Object, vlu As Integer
'0 No battery.
'1 The battery is discharging.
'2 The system has access to AC so no battery is being discharged.
' However, the battery is not necessarily charging.
'3 Fully Charged.
'4 Low.
'5 Critical.
'6 Charging.
'7 Charging and High.
'8 Charging and Low.
'9 Charging and Critical.
'10 Undefined.
'11 Partially Charged.
vlu = 0
obj = GetObject("winmgmts:").InstancesOf("Win32_Battery")
For Each obj2 In obj 'loop through objects
vlu = obj2.BatteryStatus
Next
Return(vlu)
End Function
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim stat As Integer = GetBatteryStatus()
Label2.Text = stat.ToString
If stat = 1 Then
Label1.Text = "Running on Battery"
Else
Label1.Text = "Running on Power"
End If
End Sub
End Class
However, when I install exactly the same function in a pre-existing application in the same laptop these three lines...
Code:
obj = GetObject("winmgmts:").InstancesOf("Win32_Battery")
For Each obj2 In obj 'loop through objects
chrg = obj2.BatteryStatus
...give these errors:
Option Strict On disallows late binding.
Expression is of type 'Object', which is not a collection type.
Option Strict On disallows late binding.
Both applications are running the same NET.Framework 4.8.1 the OS is Win.11, Option Strict is On in both apps, they are both tested in VS 2019 in the same laptop. I don't understand why it runs correctly in the test app but not in another.
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Feb 6th, 2025, 09:20 PM
#2
Re: I don't understand why I can't copy this function to another application.
Firstly, how about you tell us exactly which lines those error messages appear on, so we don't have top waste our time working out something that you already know?
Option Strict is On in both apps
I don't see how that's possible. Either you're mistaken or the project that does compile is broken. Do you actually understand what Option Strict does and what late binding is? The issue is pretty obvious if you do, so I can only assume that you don't, so you should do some reading and learn. Here:
Code:
Dim obj As Object, obj2 As Object
you declare those variables as type Object. Here:
Code:
For Each obj2 In obj 'loop through objects
vlu = obj2.BatteryStatus
Next
you try to enumerate a variable of type Object and then access a BatteryStatus member of a variable of type Object. Does the Object type implement the IEnumerable interface? No, it does not, so it can't be enumerated. Does the Object type have a member named BatteryStatus? No, it does not, so you can't access such a member. If you expect to use late binding then you can't have Option Strict On for the code that uses it. If you want Option Strict On, stop declaring variables as type Object and declare them as the actual types of the objects they will refer to and cast when assigning to them if necessary. The latter is the better option if it's possible to do. If you must do the former, you should keep Option Strict On at project level and turn it Off at the file level in only those files that require late binding. Even then, you should use partial classes to keep the code in those files to an absolute minimum, meaning that you still get compile-time type-checking for as much of your code as possible.
By the way, there's absolutely zero point to setting Option Explicit On at the file level. Doing so suggests, once again, that you don't know what it does. Unlike Option Strict, Option Explicit is On by default for new projects. There is absolutely no reason or excuse for ever turning it Off. Doing so allows you to use a variable without explicitly declaring it and that is something that there is never a good reason to do, without exception. There are valid reasons to want Option Infer Off, although it's required to use anonymous types. There are valid reasons to want Option Strict Off on the rare occasions that late binding is required. There are no valid reasons to ever want Option Explicit Off. It's a relic of a bygone era.
-
Feb 7th, 2025, 10:30 AM
#3
Re: I don't understand why I can't copy this function to another application.
Another way to say that is that your code is essentially telling the compiler, "don't worry what this type is, it will have the methods I require at runtime." That's what late binding is all about. Of course, that's also why it's usually a bad idea. If the type does NOT have the methods required, then you get a crash at runtime. In this case, you may have to use late binding, in which case you must turn Option Strict OFF for just that file.
My usual boring signature: Nothing
 
-
Feb 7th, 2025, 11:00 AM
#4
Re: I don't understand why I can't copy this function to another application.
 Originally Posted by Shaggy Hiker
Another way to say that is that your code is essentially telling the compiler, "don't worry what this type is, it will have the methods I require at runtime." That's what late binding is all about. Of course, that's also why it's usually a bad idea. If the type does NOT have the methods required, then you get a crash at runtime. In this case, you may have to use late binding, in which case you must turn Option Strict OFF for just that file.
Programmer to Compiler: I know, I know...shut up.
Compiler to Programmer: Ohhhhhhhkayyyyyy...but don't say I didn't try to warn you.
-
Feb 7th, 2025, 11:02 AM
#5
Re: I don't understand why I can't copy this function to another application.
Yeah, that's about it.
Of course, that means that the code shown should not have compiled with Option Strict ON in any way.
My usual boring signature: Nothing
 
-
Feb 7th, 2025, 12:18 PM
#6
Thread Starter
PowerPoster
Re: I don't understand why I can't copy this function to another application.
Thanks guys, I think I've got the hang of that now.
I hadn't noticed that the code I copied initialised the same variable twice, Not sure why it ran correctly in the test.
Poppa
Along with the sunshine there has to be a little rain sometime.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|