5 Replies - 535 Views - Last Post: 20 August 2012 - 01:43 PM Rate Topic: -----

#1 JaxxUK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-August 12

[Question] How to code Save dialog in a class, then call upon it.

Posted 19 August 2012 - 09:37 AM

Hello,

I have created a small program which returns simple outputs to the user. The user enters numbers into this program and from here I can return the highest/smallest values, or the sum of all the numbers, etc.

However I wish to design a method of saving the returned value into a text file. I have been able to accomplish this by using the SaveFileDialog feature on VB.NET ( 2008 ) which does indeed save the values to a text file.

But the issue is that I unable to figure out how I go about placing this SaveFileDialog into a separate class fromo the form which I can call upon in my main form.

I am new to VB and I have the basic grasp of classes. For instance my algorithms to work out the highest values is in a class which I understand I need to call upon then return the value to the main form (This is my code to call upon the algorithm in my class) but when it comes to a saveFileDialog you're not returning a value you are calling upon that function, which confuses me.
 ArrayClass.SumInteger(Array)
        TheAnswer = ArrayClass.ArrayAnswer4
        TextBox10.Text = TheAnswer




This is the SaveFileDialog code I have been using which works on the main form, but not from a class:

Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        
        Dim myStream As Stream
        Dim saveFileDialog1 As New SaveFileDialog()

        saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        saveFileDialog1.FilterIndex = 2
        saveFileDialog1.RestoreDirectory = True

        If saveFileDialog1.ShowDialog() = DialogResult.OK Then
            myStream = saveFileDialog1.OpenFile()

            Using sw As StreamWriter = New StreamWriter(myStream)
                ' Add some text to the file.
                sw.WriteLine(TextBox1.Text)
                sw.WriteLine(TextBox2.Text)
                sw.WriteLine(TextBox3.Text)
                sw.WriteLine(TextBox4.Text)
                sw.WriteLine(TextBox5.Text)
                sw.WriteLine(TextBox6.Text)
                sw.WriteLine("--------")
                sw.WriteLine("This is the large value " & (TextBox7.Text))
                sw.WriteLine("This is the smallest value" & (TextBox8.Text)
               
                sw.Close()
            End Using

            If (myStream IsNot Nothing) Then
                ' Code to write the stream goes here.
                myStream.Close()
            End If
        End If

    End Sub




I would like to say I have tried many ways to get this working but that would be incorrect, I am generally struggling to understand how I would get this feature to work. I just don't know where to start.

Can you explain to me how I go about accomplishing my task. I feel like a bit of a dummy.

-Edward

Is This A Good Question/Topic? 0
  • +

Replies To: [Question] How to code Save dialog in a class, then call upon it.

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5537
  • View blog
  • Posts: 11,868
  • Joined: 02-June 10

Re: [Question] How to code Save dialog in a class, then call upon it.

Posted 19 August 2012 - 09:52 AM

Just make a new instance of the SaveFileDialog programmatically the use it just like the one you drag and dropped.


    Private Sub MyFancySaveMethod()
        Dim sfd As SaveFileDialog = New SaveFileDialog
        ' Now you manipulate it just like the one you created through drag and drop
    End Sub


Of course, now that it is in another class you have to communicate between the two classes to share the information.


How do I?

[*]Q: ...get Form 'A' to make a change or talk to Form 'B'
Spoiler

Was This Post Helpful? 0
  • +
  • -

#3 JaxxUK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-August 12

Re: [Question] How to code Save dialog in a class, then call upon it.

Posted 20 August 2012 - 09:07 AM

View PosttlhIn`toq, on 19 August 2012 - 09:52 AM, said:

Just make a new instance of the SaveFileDialog programmatically the use it just like the one you drag and dropped.


    Private Sub MyFancySaveMethod()
        Dim sfd As SaveFileDialog = New SaveFileDialog
        ' Now you manipulate it just like the one you created through drag and drop
    End Sub


Of course, now that it is in another class you have to communicate between the two classes to share the information.


How do I?

[*]Q: ...get Form 'A' to make a change or talk to Form 'B'
[spoiler]
NOTE: Don't try to access GUI controls across forms. Its wrong. Nobody will hire you if you do this sort of crap. It violates every guideline for 'black box' programming, Separation of Responsibility, loose binding of components, and event driven programming. Read the tutorials and learn to do it right the first time so you don't develop bad habits that you just have to un-learn later.
A:[list]


Hi tlhIn`toq, thanks for the quick response. I believe I correctly implemented what you suggested. Here is the new code of the Save function in a separate class like I wanted.


Imports System.IO

Public Class Save
    'Private testdata As Integer
  

    Public Sub MyFancySaveMethod()
        Dim myStream As Stream
        Dim saveFileDialog1 As New SaveFileDialog()

        saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        saveFileDialog1.FilterIndex = 1
        saveFileDialog1.RestoreDirectory = True

        If saveFileDialog1.ShowDialog() = DialogResult.OK Then
            myStream = saveFileDialog1.OpenFile()

            Using sw As StreamWriter = New StreamWriter(myStream)

                sw.WriteLine("Text goes here, blah blah")
                
            sw.Close()
            End Using

            If (myStream IsNot Nothing) Then
                ' Code to write the stream goes here.
                myStream.Close()
            End If
        End If
    End Sub
End Class




And to call it I put this code into a button.

SaveClass.MyFancySaveMethod()



It seems to work great, I feel like a right dummy not figuring that out.

However my dilemma now is that I need a method of getting my variables from my main form to my save class.

To be specific, I need the text that the user put in in TextBox1 saved.
Simply doing This:
 sw.WriteLine("TextBox1.Text")

wont work as the TextBox1 is in a different form.

I know I could just do this
 sw.WriteLine(Form1.TextBox1.Text) 


But this doesn't feel 'proper' to me.

After reading your links I'm sure I need put the contents of Textbox1.Text into a variable then make it available to my Save class by some of new and get property( I think ) however I am still struggling.

May you assist me again, please.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5537
  • View blog
  • Posts: 11,868
  • Joined: 02-June 10

Re: [Question] How to code Save dialog in a class, then call upon it.

Posted 20 August 2012 - 09:33 AM

It isn't proper, not by a long shot.

From form1, send the text to your new class. Send TO the new class, don't try to have the new class suck it FROM the form.

It is very important to get used to separation of responsibilities. The save class shouldn't know anything about any other class. It is a black box in a dark room. It only know what it is told. It is the responsibility of the call class (form1) to tell it everything it needs to know, like the information to be saved.

You could probably just make some public properties in your save class.
So from your form1 instance:
  • make a new save class.
  • Send the data to be saved (set the values of a property with the data).
  • Tell it to SaveNow (Run the SaveNow() method)


That means you need to make a couple changes to your save class.
First rename MyFancySave to SaveNow
Next it needs a public property for the data.
Last update your form1 method

Dim saveClassInstance as new SaveClass()
saveClassInstance.Data = StuffToBeSaved
saveClassInstance.SaveNow

Was This Post Helpful? 0
  • +
  • -

#5 JaxxUK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-August 12

Re: [Question] How to code Save dialog in a class, then call upon it.

Posted 20 August 2012 - 10:40 AM

View PosttlhIn`toq, on 20 August 2012 - 09:33 AM, said:

It isn't proper, not by a long shot.

From form1, send the text to your new class. Send TO the new class, don't try to have the new class suck it FROM the form.

It is very important to get used to separation of responsibilities. The save class shouldn't know anything about any other class. It is a black box in a dark room. It only know what it is told. It is the responsibility of the call class (form1) to tell it everything it needs to know, like the information to be saved.

You could probably just make some public properties in your save class.
So from your form1 instance:
  • make a new save class.
  • Send the data to be saved (set the values of a property with the data).
  • Tell it to SaveNow (Run the SaveNow() method)


That means you need to make a couple changes to your save class.
First rename MyFancySave to SaveNow
Next it needs a public property for the data.
Last update your form1 method

Dim saveClassInstance as new SaveClass()
saveClassInstance.Data = StuffToBeSaved
saveClassInstance.SaveNow



Another fast response 'tlhIn`toq, thank you.

I believe I understand what you are saying however implementing your advice is far harder for me.

I understand I need to send my data from form1 to my Save class, instead of having the Save class pull it from form1.

But the issue is using the public property in my save class. I have used it in the past to assign values to a variable for the main form to use, but this Save function feels different, so I am quite confused.

So far I have the Public Property that I think is correct but I don't know/understand what is to be filled inside of it. Once I understand this part I assume I can just place the new variable into the Save function ( I need four variables, see below) ;


Imports System.IO

Public Class Save

    Public ReadOnly Property StuffToBeSaved() As Integer
        Get
            Return <---Confused--->
        End Get
    End Property

    Public Sub SaveNow()
        Dim myStream As Stream
        Dim saveFileDialog1 As New SaveFileDialog()

        saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        saveFileDialog1.FilterIndex = 1
        saveFileDialog1.RestoreDirectory = True

        If saveFileDialog1.ShowDialog() = DialogResult.OK Then
            myStream = saveFileDialog1.OpenFile()

            Using sw As StreamWriter = New StreamWriter(myStream)

                'sw.WriteLine("This is the large value" & ****A variable?**** )
                'sw.WriteLine("This is the Smallest value" & ****A variable?**** )
                'sw.WriteLine("This is the Sum of the values" & ****A variable?**** )
                'sw.WriteLine("This is the Average of the values" & ****A variable?**** )

                sw.Close()
            End Using

            If (myStream IsNot Nothing) Then
                ' Code to write the stream goes here.
                myStream.Close()
            End If
        End If
    End Sub
End Class




And like you said I placed that code into my button to tell it to save( to run the save method ) however there are errors for the time being.

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click



        Dim saveClassInstance As New SaveClass' <-- Both of these are errors for the time being

        saveClassInstance.Data = StuffToBeSaved'  <-- Both of these are errors for the time being

        SaveClass.SaveNow()
End Sub
 




I hope you understand my issue, thanks.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5537
  • View blog
  • Posts: 11,868
  • Joined: 02-June 10

Re: [Question] How to code Save dialog in a class, then call upon it.

Posted 20 August 2012 - 01:43 PM

Quote

But the issue is using the public property in my save class. I have used it in the past to assign values to a variable for the main form to use, but this Save function feels different, so I am quite confused.

There is little I can do about your feelings.
This has nothing to do with whether or not the properties are in main or in another class: Properties are constructed in only one way. It sounds more like you are confused about properties in general. The MSDN to the rescue:
http://msdn.microsof...y/dd293589.aspx

Your other errors are because you copied my explanation code without thinking. SaveClass isn't meant to be literal... its the type of your class that does the saving... like Button or Form.
Just like you did here:
13 Dim saveFileDialog1 As New SaveFileDialog()
is Dim saveFileDialogInstance As New SaveFileDialogClass
You are making an instance of a class. That is what I tried to represent with those variable names.


StuffToBeSaved was meant to be an explanation. You know, send it the stuff to be saved, whatever the name of that variable in your program is.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1