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

Me is unknown

Hello,
I have only one form with a listbox:lbFileContent, and I have this code in
Public Class Form1.

Public Shared Sub UploadFile(ByVal fileNamePath As String)
Dim reader As StreamReader = New StreamReader(fileNamePath)

Try
Me.lbFileContent.Items.Clear()
Do
Me.lbFileContent.Items.Add(reader.ReadLine)
Loop Until reader.Peek = -1

Catch
Me.lbFileContent.Items.Add("File is empty")

Finally
reader.Close()
End Try
End Sub

It seems Me is not defined there. What should I do here?
Thanks,
Jim.

Jul 21 '05 #1
3 1232
The sub is Shared. Meaning, it is NOT an instance method. However, your
listbox is an instance member of the class - i.e. you have to have an
instance of the form, to have an instance of the listbox.

Shared methods cannot work with instance members of the class, because they
do not execute as part of an instance. Remove the 'Shared' keyword.

"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:ED**********************************@microsof t.com...
Hello,
I have only one form with a listbox:lbFileContent, and I have this code in
Public Class Form1.

Public Shared Sub UploadFile(ByVal fileNamePath As String)
Dim reader As StreamReader = New StreamReader(fileNamePath)

Try
Me.lbFileContent.Items.Clear()
Do
Me.lbFileContent.Items.Add(reader.ReadLine)
Loop Until reader.Peek = -1

Catch
Me.lbFileContent.Items.Add("File is empty")

Finally
reader.Close()
End Try
End Sub

It seems Me is not defined there. What should I do here?
Thanks,
Jim.

Jul 21 '05 #2

Hi Marina
I am actually using the following code from MS help and I am calling the
function UploadFile in OnChanged function. If I remove shared it gives me the
following message.

“ Cannot refer to an instance member of a class from within a shared method
or shared member initializer without an explicit instance of the class.”

Is it secure to remove all shared in all functions?

Thanks,
Jim.

Public Class Watcher

Public Shared Sub Main()
Dim args() As String = System.Environment.GetCommandLineArgs()
' If a directory is not specified, exit the program.
If args.Length <> 2 Then
' Display the proper way to call the program.
Console.WriteLine("Usage: Watcher.exe (directory)")
Return
End If

' Create a new FileSystemWatcher and set its properties.
Dim watcher As New FileSystemWatcher()
watcher.Path = args(1)
' Watch for changes in LastAccess and LastWrite times, and
' the renaming of files or directories.
watcher.NotifyFilter = (NotifyFilters.LastAccess Or
NotifyFilters.LastWrite Or NotifyFilters.FileName Or
NotifyFilters.DirectoryName)
' Only watch text files.
watcher.Filter = "*.txt"

' Add event handlers.
AddHandler watcher.Changed, AddressOf OnChanged
AddHandler watcher.Created, AddressOf OnChanged
AddHandler watcher.Deleted, AddressOf OnChanged
AddHandler watcher.Renamed, AddressOf OnRenamed

' Begin watching.
watcher.EnableRaisingEvents = True

' Wait for the user to quit the program.
Console.WriteLine("Press 'q' to quit the sample.")
While Chr(Console.Read()) <> "q"c
End While
End Sub

' Define the event handlers.
Private Shared Sub OnChanged(source As Object, e As FileSystemEventArgs)
' Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " & e.FullPath & " " & e.ChangeType)

' upload file
UploadFile(e.FullPath)

End Sub

Private Shared Sub OnRenamed(source As Object, e As RenamedEventArgs)
' Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath,
e.FullPath)
End Sub

End Class

"Marina" wrote:
The sub is Shared. Meaning, it is NOT an instance method. However, your
listbox is an instance member of the class - i.e. you have to have an
instance of the form, to have an instance of the listbox.

Shared methods cannot work with instance members of the class, because they
do not execute as part of an instance. Remove the 'Shared' keyword.

"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:ED**********************************@microsof t.com...
Hello,
I have only one form with a listbox:lbFileContent, and I have this code in
Public Class Form1.

Public Shared Sub UploadFile(ByVal fileNamePath As String)
Dim reader As StreamReader = New StreamReader(fileNamePath)

Try
Me.lbFileContent.Items.Clear()
Do
Me.lbFileContent.Items.Add(reader.ReadLine)
Loop Until reader.Peek = -1

Catch
Me.lbFileContent.Items.Add("File is empty")

Finally
reader.Close()
End Try
End Sub

It seems Me is not defined there. What should I do here?
Thanks,
Jim.


Jul 21 '05 #3
You can't call instance methods from shared subs, just like you can't access
instance variables from shared sub.

So this means you have to make the sub that calls UploadFile an instance
method, etc. This may have other reprecussions, as I don't know your code
or what you are doing exactly. It sounds though, like you should read up on
shared vs. instance methods and variables to get a clear understanding of
what each one is for.

"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:28**********************************@microsof t.com...

Hi Marina
I am actually using the following code from MS help and I am calling the
function UploadFile in OnChanged function. If I remove shared it gives me the following message.

" Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class."

Is it secure to remove all shared in all functions?

Thanks,
Jim.

Public Class Watcher

Public Shared Sub Main()
Dim args() As String = System.Environment.GetCommandLineArgs()
' If a directory is not specified, exit the program.
If args.Length <> 2 Then
' Display the proper way to call the program.
Console.WriteLine("Usage: Watcher.exe (directory)")
Return
End If

' Create a new FileSystemWatcher and set its properties.
Dim watcher As New FileSystemWatcher()
watcher.Path = args(1)
' Watch for changes in LastAccess and LastWrite times, and
' the renaming of files or directories.
watcher.NotifyFilter = (NotifyFilters.LastAccess Or
NotifyFilters.LastWrite Or NotifyFilters.FileName Or
NotifyFilters.DirectoryName)
' Only watch text files.
watcher.Filter = "*.txt"

' Add event handlers.
AddHandler watcher.Changed, AddressOf OnChanged
AddHandler watcher.Created, AddressOf OnChanged
AddHandler watcher.Deleted, AddressOf OnChanged
AddHandler watcher.Renamed, AddressOf OnRenamed

' Begin watching.
watcher.EnableRaisingEvents = True

' Wait for the user to quit the program.
Console.WriteLine("Press 'q' to quit the sample.")
While Chr(Console.Read()) <> "q"c
End While
End Sub

' Define the event handlers.
Private Shared Sub OnChanged(source As Object, e As FileSystemEventArgs) ' Specify what is done when a file is changed, created, or deleted. Console.WriteLine("File: " & e.FullPath & " " & e.ChangeType)

' upload file
UploadFile(e.FullPath)

End Sub

Private Shared Sub OnRenamed(source As Object, e As RenamedEventArgs)
' Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath,
e.FullPath)
End Sub

End Class

"Marina" wrote:
The sub is Shared. Meaning, it is NOT an instance method. However, your
listbox is an instance member of the class - i.e. you have to have an
instance of the form, to have an instance of the listbox.

Shared methods cannot work with instance members of the class, because they do not execute as part of an instance. Remove the 'Shared' keyword.

"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:ED**********************************@microsof t.com...
Hello,
I have only one form with a listbox:lbFileContent, and I have this code in Public Class Form1.

Public Shared Sub UploadFile(ByVal fileNamePath As String)
Dim reader As StreamReader = New StreamReader(fileNamePath)

Try
Me.lbFileContent.Items.Clear()
Do
Me.lbFileContent.Items.Add(reader.ReadLine)
Loop Until reader.Peek = -1

Catch
Me.lbFileContent.Items.Add("File is empty")

Finally
reader.Close()
End Try
End Sub

It seems Me is not defined there. What should I do here?
Thanks,
Jim.


Jul 21 '05 #4

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

Similar topics

0
by: Eugen Walcher | last post by:
Hello All, I'm getting this error in my error_log in apache. Can anyone offer any assistance in fixing the problem? I'm using RH 9 with apache 2.0.53 and PHP v4.3.11
3
by: Marcus | last post by:
I'm running into a situation that has me adding a value of "Unknown" to a reference table. I am being pulled between two trains of thought, and was curious to get other's input on in. I give an...
9
by: Klaus Johannes Rusch | last post by:
IE7 returns "unknown" instead of "undefined" when querying the type of an unknown property of an object, for example document.write(typeof window.missingproperty); Has "unknown" been defined...
1
by: tactech | last post by:
Hello Can somebody help me and tell me why I cant change the background color of the second PN2 panel. I get this error about unknown source Exception in thread "AWT-EventQueue-1"...
4
by: omono84 | last post by:
I know that this should be rather simple but i seem to be missing a step to get it to work. and have been unable to find a solution on the net. The aim is that I click on the open button to find...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.