Darth Sidious

Hi, everyone! I have a problem with describing methods in classes. I want to build main, inheritable class for some file formats and I want to describe methods Open and Save with such parameters as file name, file extension and file type. I use these private string variables: strNameOfFile, strTypeOfFile, strExtensionOfFile (I draw your attention that next code is not accomplished):

Code Snippet

Public Function Open(ByVal strTypeOfFile, ByVal strNameOfFile, ByVal strExtensionOfFile As String)

Dim OpenFileDialog1 As New System.Windows.Forms.OpenFileDialog

End Function

But IDE underlines the last variable (strExtensionOfFile) for some reason when this last one is declared. IDE writes that "All parameters must be explicitly typed if any are". But I swear on my honour that I have copied and pasted the name of this variable. What is wrong in this code




Re: Visual Basic Express Edition describing "open" and "save" methods for files

Tall Dude

Public Class Form1

Public Function Open(ByVal strTypeOfFile As String, _

ByVal strNameOfFile As String, _

ByVal strExtensionOfFile As String) As Boolean

Dim OpenFileDialog1 As New System.Windows.Forms.OpenFileDialog

' Other code ...

'

' If no error

If True Then

Return True

Else

Return False

End If

End Function

End Class






Re: Visual Basic Express Edition describing "open" and "save" methods for files

spotty

The error you are generating is probably.

error BC30529: All parameters must be explicitly typed if any of them are explicitly typed.

This occurs as you have specified a type for at least on parameter in the Method Declaration. Either type all of them by adding as string after each, or remove the as string on the last parameter so you dont specify any of the parameter types and object will be use allowing you to pass any object to the method.

Personally, I'd add as string and correctly type the parameters. Its called early binding and then will allow you to get intellisense when using these variables in the development environment.





Re: Visual Basic Express Edition describing "open" and "save" methods for files

Darth Sidious

Thank you very much, guys!




Re: Visual Basic Express Edition describing "open" and "save" methods for files

Darth Sidious

Hi, everyone! I need to describe the method "Save" but I have some problems. I have described the method "SaveAs" where this method always calls SaveFileDialog. It's a simplest method! But in "Save" method this dialog will be called only if file never has been saved before. How to save file if it is saved before and there are some changes in it after its last saving Is there some system command which does it every time we save our files If yes, what is its name
How to delete methods "New" and "Finalize" or how to make them to be invisible for user





Re: Visual Basic Express Edition describing "open" and "save" methods for files

spotty

What you would do is after when you open the file you set a flag variable which determines the file is in a clean state. If when you use your application after that you do anything which is going to change the contents then you set this flag variable to indicate that a change occured and the file is now dirty.

In you save method, if the flag indicate its dirty and you have a filename used to load the file - then you can save without propmpting. If you have the filename used to open the file and the flag indicates clean - you dont need to save anything and if its a newly created file which you havent yet got a filename or saveas action then you can call the saveas method which would display a dialog to prompt you for a name and then save the file.





Re: Visual Basic Express Edition describing "open" and "save" methods for files

Darth Sidious

I already have foreseen flag variable about which you have wtritten. I test the condition if file has been saved before or not. But I don't know what command I need to write in the "Save" method's code. If you don't understand I give you one example. When I write the code for "SaveAs" method I call the object named SaveFileDialog, right And what object do I need to call (in my method "Save") for saving file if it has got a filename and been changed after working with it What is the name of this object What namespace is it from I think that this object is a command of operating system but what is its name is mystery vestured by darkness.






Re: Visual Basic Express Edition describing "open" and "save" methods for files

spotty

The savefiledialog simply present you with a dialog to get a filename. You still have to use the filename you get to actually write the contents to a file. To do a save you dont need to get a filename so simply - you dont need to use the SaveFileDialog because you should already have a filename - Then use the same code as your probably using to save the file contents anyway.





Re: Visual Basic Express Edition describing "open" and "save" methods for files

Darth Sidious

spotty2428 wrote:

Then use the same code as your probably using to save the file contents anyway.

Sorry for my bad English (I'm Russian): sometimes I can't understand you. May I place here this code for you May be you show me what do you mean I can't imagine what do you explain me.

Code Snippet

Public Function Save() As Boolean

Dim dialogSaveFile As New System.Windows.Forms.SaveFileDialog

Dim GraphicFile As New GraphicFile

If blnIsFileSavedBefore = False Then

dialogSaveFile.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments

dialogSaveFile.Filter = "File of engraving (*.avbengrav)|*.avbengrav|File of tool (*.avbtool)|*.avbtool|File of tool cutting(*.avbcut)|*.avbcut|File of drafting (*.avbdrw)|*.avbdrw|File of electrical scheme (*.avbsch)|*.avbsch|File of design project 2D (*.avbdsn2d)|*.avbdsn2d|File of design project 3D (*.avbdsn3d)|*.avbdsn3d|File of photomask (*.avbpmask)|*.avbpmask|File of part (*.avbprt)|*.avbprt|File of assemly (*.avbasm)|*.avbasm|All files (*.*)|*.*"

dialogSaveFile.FilterIndex = 11

dialogSaveFile.RestoreDirectory = True

GraphicFile.FileName = dialogSaveFile.FileName

ElseIf blnIsFileChanged = True And blnIsFileSavedBefore = True Then

dialogSaveFile.FileName = GraphicFile.FileName

End If

If True Then

Return True

Else

Return False

End If

End Function






Re: Visual Basic Express Edition describing "open" and "save" methods for files

spotty

Here's a simple example using a text file

Code Snippet

Public Class Form1
Dim isDirty As Boolean = False
Dim Filename As String = ""

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
isDirty = True '//Set dirty flag is changed
End Sub

Private Sub LoadFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'//Load File - set dirty flag to false and filename
Dim x As New OpenFileDialog
If x.ShowDialog = Windows.Forms.DialogResult.OK Then
TextBox1.Text = My.Computer.FileSystem.ReadAllText(x.FileName)

'//Set Flag Variables
Filename = x.FileName
isDirty = False
End If
End Sub


Private Sub Save_Button(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Filename.Length = 0 Then '//New File
Filename = SaveAsDialog()
If Filename.Length > 0 Then
SaveContent(Filename, TextBox1.Text)
isDirty = False
End If
ElseIf isDirty And Filename.Length > 0 Then '//Existing Dirty File
SaveContent(Filename, TextBox1.Text)
End If
End Sub

Function SaveAsDialog() As String
Dim dialogSaveFile As New SaveFileDialog
dialogSaveFile.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
dialogSaveFile.Filter = "Text Files(*.txt)|All files (*.*)|*.*"
dialogSaveFile.RestoreDirectory = True
If dialogSaveFile.ShowDialog = Windows.Forms.DialogResult.OK Then
Return dialogSaveFile.FileName
Else
Return ""
End If
End Function

Function SaveContent(ByVal filename As String, ByVal content As String)
My.Computer.FileSystem.WriteAllText(filename, content, False)
End Function


End Class

When you create and type stuff into textbox and save - it will prompt for a filename. if you make further changes and same it will just save without asking for filename (as it already has the filename).

If you open an exisiting file - it populates the textbox and you can make changes and it wont bother asking for a filename as its already got the filename which was used to open the file.

This should show you the general concept.






Re: Visual Basic Express Edition describing "open" and "save" methods for files

Darth Sidious

Thank you very much. I'll try to use your code in my situation