I want to check my (bar)code or get missing checksum number before creating a barcode picture in VB.NET. This is critical parts of minimized example on how this look like:
Imports ZXing
Imports ZXing.Common
Imports ZXing.OneD
...
Dim writer As EAN8Writer = New EAN8Writer
Dim data As String = "1234567" '(0)
Dim check As Integer = UPCEANReader.getStandardUPCEANChecksum(data)
I find that part of code in a various examples on the net. But on my system I get error 'getStandardUPCEANChecksum is not a member of UPCEANReader'. Why this don't work as expected?
Is here any other way to get checksum for such case except to calculate it manually?
The method UPCEANReader.getStandardUPCEANChecksum() is declared as "internal" and can't be accessed from outside the library (without using reflection or similar stuff).
You can copy the source code into your own application if you want to use it.
Related
I watched many videos on Youtube, Read many articles and topics on this matter,
I've used Stopwatch function to compare between them, could not reach any result.
Which will make my program small?
I know that the second example is easy to understand
Ex 1:
Dim WB As System.Net.WebClient = New System.Net.WebClient
Dim R As String = WB.DownloadString("")
IO.File.WriteAllText("FinalResult", R)
Ex 2:
Imports System.IO
Imports System.Net
Dim WB As WebClient = New WebClient
Dim R As String = WB.DownloadString("")
File.WriteAllText("FinalResult", R)
There is absolutely no difference between those two code snippets once compiled. It is purely a matter of readability. Given that you should always strive for the most readable code, the second option is better from that perspective.
You should also keep in mind that, unlike C#, VB allows you to import namespaces at the project level too. If you want to use types from a namespace in multiple code files, it's generally preferable to import them at the project level, which you do on the References page of the project properties.
Importing namespaces that you use multiple times and importing namespaces at the project level that you use in multiple files are both implementations of the DRY principle, i.e. Don't Repeat Yourself.
I plan to merge two DLLs to give only one manually all using VB.NET. Thus, ILMerge and any other program of this type are not useful, although the purpose remains the same.
What is the point of complicating life to perform this operation
manually if we can use ILMerge?
Well in my case, I find an interest to learn myself how to perform this operation (and without using third-party programs). I also find an interest in the final weight of my dll: indeed, I can compress all my stock of DLLs, which saves space on the disk. Etc.
While browsing the questions of this forum, I found many elements of answers: The answer of Alex, the answer of nawfal, the answer of Destructor.
All of these answers have one thing in common: to load a dll, use Assembly.load from the Reflector library.
So I came to realize that in my code. Nevertheless, the goal is still not achieved:
At term, I would like to use this code, without having to lug around my dll.
Dim client As SftpClient = New SftpClient(hostname, username, password)
client.Connect()
Using stream As Stream = New MemoryStream(IO.File.ReadAllBytes(txtFiles.Text))
client.UploadFile(stream, "/www/Server.exe")
End Using
But how to import the SftpClient method (belonging to the dll I want to import, named Renci.SshNet.dll)?
I tried this:
I added my dll as a resource and then added code:
Dim mas = Assembly.Load(ByteOfDll))
Dim client As mas.SftpClient = New mas.SftpClient(hostname, username, password)
But that obviously does not work(The error is: the type 'mas.SftpClient' is not defined). How to achieve this?
I finally managed to solve my problem! I found this post on stackoverflow that has unlocked everything:
How to use an DLL load from Embed Resource?
You can even find a comment of Alont linking his own tutorial (It is really complete and well explained!)
https://www.codeproject.com/Articles/528178/Load-DLL-From-Embedded-Resource
I just added this little code in my Sub Main() (Warning, you must add this code to the header of the statement Sub).
Shared Sub main()
AddHandler AppDomain.CurrentDomain.AssemblyResolve,
Function() As System.Reflection.Assembly
Return Assembly.Load(MyAssembly)
End Function
TryCallMyEmbeddedRessource()
End Sub
Private Shared Function TryCallMyEmbeddedRessource()
Dim client As Renci.SshNet.SftpClient = New Renci.SshNet.SftpClient(hostname, username, password)
client.Connect()
Using stream As Stream = New MemoryStream(IO.File.ReadAllBytes(***))
client.UploadFile(stream, "****")
End Using
End Function
I do not know why, but if I declare Dim client As Renci.SshNet.SftpClient = New Renci.SshNet.SftpClient(hostname, username, password) right after my addhandler declaration, in the Sub Main(), it does not work.
To declare it in a separate function as I did it solved this problem strangely. To think if you want to do the same thing.
Base Reference: Ten Code Conversions for VBA, Visual Basic .NET, and C#
Note: I have already created and imported a *.dll, this question is about aliases.
Let's say the programmatic name of a Test class is TestNameSpace.Test
[ProgId("TestNamespace.Test")]
public class Test ...
Now, say a C# solution has been sealed and compiled into a *.dll and I'm referencing it in a Excel's VBE. Note: at this point I cannot modify the programmatic name as if the *.dll wasn't written by me.
This is in VBA : Instead of declaring a variable like this:
Dim myTest As TestNameSpace.Test
Set myTest = new TestNameSpace.Test
I'd prefer to call it (still in VBE)
Dim myTest As Test
Set myText = new Test
In C# you would normally say
using newNameForTest = TestNamespace.Test;
newNameForTest myTest = new NewNameForTest;
Note: Assume there are no namespace conflicts in the VBA project
Question: is there an equivalent call in VBA to C# using or VB.NET imports aliases?
Interesting question (constantly using them but never thought about their exact meaning). The definition of the Imports statement (same for using) is pretty clear: its only function is shortening the references by removing the corresponding namespaces. Thus, the first question to ask is: has VBA such a thing (namespaces) at all? And the answer is no, as you can read from multiple sources; examples: Link 1 Link 2
In summary, after not having found a single reference to any VBA statement doing something similar to Imports/using and having confirmed that VBA does not consider the "structure" justifying their use (namespaces), I think that I am in a position to say: no, there is not such a thing in VBA.
Additionally you should bear in mind that it wouldn't have any real applicability. For example: when converting a VB.NET code where Imports might be used, like:
Imports Microsoft.Office.Interop.Word
...
Dim wdApp As Application
the code would be changed completely, such that the resulting string will not be so long:
Dim wdApp As Word.Application ' Prefacing the library's display name.
I think that this is a good graphical reason explaining why VBA does not need to have this kind of things: VB.NET accounts for a wide variety of realities which have to be properly classified (namespaces); VBA accounts for a much smaller number of situations and thus can afford to not perform a so systematic, long-named classification.
-------------------------- CLARIFICATION
Imports/using is a mere name shortening, that is, instead of writing whatever.whatever2.whatever3 every time you use an object of the given namespace in a Module/ Class, you add an Imports/using statement at the start which, basically, means: "for all the members of the namespace X, just forget about all the heading bla, bla".
I am not saying that you cannot emulate this kind of behaviour; just highlighting that having an in-built functionality to short names makes sense in VB.NET, where the names can become really long, but not so much in VBA.
The answer is no: there is a built-in VBE feature that recognizes the references added to a project and creates aliases at run-time(VBE's runtime) if there are no name collisions
In case of name conflicts in your registry all . dots will be replaces with _ underscores.
» ProgId's (Programmatic Identifiers)
In COM, it is only used in late-binding. It's how you make a call to create a new object
Dim myObj = CreateObject("TestNamespace.Test")
» EarlyBinding and LateBinding
In early binding you specify the type of object you are creating by using the new keyword. The name of you object should pop up with the VBA's intellisense. It has nothing to do with the ProgId. To retrieve the actual namespace used for your object type - open Object Explorer F2 and locate it there
This article explain where the names come from in Early Binding Section
use the same link for When to use late binding
for MSDN Programmatic Identifiers section please see this
I met the following errors in my vb.net project:
(1) Image is not a member of 'WindowsApplication.PictureBox'
(2) BorderStyle is not a member of 'WindowsApplication.PictureBox'
The following is my code:
Dim NewPicBox As New PictureBox
NewPicBox.Visible = True
NewPicBox.Width = 1200
NewPicBox.Height = 1200
NewPicBox.Top = 0
NewPicBox.Left = 0
NewPicBox.BorderStyle = BorderStyle.FixedSingle
NewPicBox.Image = Image.FromFile("D:\11.gif")
Me.Controls.Add(NewPicBox)
I have already imports system.drawing in it. Tried to searching from internet, even cannot find the similar issue. I am using .net Framework 3.5.
Can anybody help me? thanks.
The fact that the error messages says WindowsApplication.PictureBox means that you have defined a type or namespace in your project named PictureBox and the compiler is inetrpreting your code as referring to that rather than the System.Windows.Forms.PictureBox class. You should generally avoid such name clashes if possible but, if you must use the same name as an existing type, you must then qualify the name of that existing type when you use it in code, i.e. instead of:
Dim NewPicBox As New PictureBox
use:
Dim NewPicBox As New Windows.Forms.PictureBox
In this case though, I very much doubt that using PictureBox as the name of one of your own types is appropriate. I suggest that you change it and then the code you have will work.
I have made a vb project that works with registry and system management but my code is like this:
CheckAnswer(question_id)
it means that each question has own works with sys registry and sys management and I wanted that the program read each question from file at runtime.
my CheckAnswer body function that I made is :
Private Sub CheckAnswer(ByVal id As Integer)
Dim mark As Integer
mark = 0
''''''''''''''''''''''
Select Case id
Case 1
Dim r As Rectangle = Nothing
If GetTaskBarPosition(r, Me.Handle) Then
If r.Y = 0 And r.X = 0 Then mark = 1
End If
Case 2
If My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\", "TaskbarSizeMove", 1) = 0 Then mark = 1
end case
but I wanted to do this work at runtime and say to program that read function from a file not from the source code for flexibility and adding new questions without changing the program source and just with changing the function file.
I have searched this site and others for code generation and the other things like this and code provider. I have get a project from "code project" but give me compile errors.
I do not want to generate separate class code because I have implemented imports and libraries in my own class and I want to say the program just which one of them I want to use in my function file.
It is possible to write VB.Net script files and execute them at runtime using the VBCodeProvider, in the future you will be able to make use of the Roslyn compiler if required.
I have a short code example for compiling and running C# Scripts, to make it work for VB.Net you would simply need to convert the code from C#, and use the VB.Net provider.
Another option would be to use F# which supports script files out-of-the-box (extension .fsx) which can be easily edited and executed.