Page 1 of 2 12 LastLast
Results 1 to 40 of 47

Thread: [RESOLVED] Visual Basic ... trouble with writing to a file

  1. #1

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    51

    Resolved [RESOLVED] Visual Basic ... trouble with writing to a file

    I'm trying to write variable values to a file from a Console App. The code I'm using is below. There is also a namespace saved as a separate file that's also shown. Can anyone tell me what's wrong with this?

    Code:
            'plot r and z
            Dim file As System.IO.StreamWriter
            Dim TestStr As String
            file = My.Computer.FileSystem.OpenTextFileWriter("C:\Users\denny\source\repos\atomizer\test.txt", True)
    
            For jz = 0 To nz
                For ir = 0 To nr
                    'TestStr = Format(ir, jz, r(ir, jz), "Scientific", z(jz), "Scientific")
                    'file.WriteLine("TestStr")
                    file.WriteLine("writing")
                Next ir
            Next jz
    
            file.Close()
    Code:
    Namespace My
        Friend Class Computer
            Public Shared Property FileSystem As Object
        End Class
    End Namespace
    This is the line that generates an exception:

    file = My.Computer.FileSystem.OpenTextFileWriter("C:\Users\denny\source\repos\atomizer\test.txt", True)

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Visual Basic ... trouble with writing to a file

    Quote Originally Posted by DennyGinson View Post
    This is the line that generates an exception:

    file = My.Computer.FileSystem.OpenTextFileWriter("C:\Users\denny\source\repos\atomizer\test.txt", True)
    What exception? That is diagnostic information that you don't keep from the people you want to diagnose the issue for you. ALWAYS provide a FULL and CLEAR explanation of the problem. What actually happens is part of that.

    Also, did you write that second code snippet yourself? If so, get rid of it.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Visual Basic ... trouble with writing to a file

    Also, it is good practice to use a Using block to create and implicitly close/dispose objects that implement the IDisposable interface, which the StreamWriter class does:
    vb.net Code:
    1. Using writer = My.Computer.FileSystem.OpenTextFileWriter("file path here", True)
    2.     'Write to file here.
    3. End Using

  4. #4

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    51

    Re: Visual Basic ... trouble with writing to a file

    Here's the exception referencing the afor mentioned line of code.

    Exception thrown: 'System.NullReferenceException' in Microsoft.VisualBasic.dll
    An unhandled exception of type 'System.NullReferenceException' occurred in Microsoft.VisualBasic.dll
    Object variable or With block variable not set.

  5. #5

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    51

    Re: Visual Basic ... trouble with writing to a file

    Do I still need a namespace named My with a Class named Computer? I'm not new to programming but I am relatively new to OOP (as an explanation for what might be a dumb question)

  6. #6
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: Visual Basic ... trouble with writing to a file

    There is already already a "My" namespace, just

    VB.NET Code:
    1. Using writer = My.Computer.FileSystem.OpenTextFileWriter("C:\AJunk2015\test.txt", True)
    2.             writer.WriteLine("Here is the first string.")
    3.         End Using

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Visual Basic ... trouble with writing to a file

    Quote Originally Posted by DennyGinson View Post
    Do I still need a namespace named My with a Class named Computer? I'm not new to programming but I am relatively new to OOP (as an explanation for what might be a dumb question)
    In the IDE, Intellisense should be trying to help you out by showing you a list of things it already knows about as you type.
    When you typed "m", you should have gotten a list of things starting with "M". When you typed "y", then "My{}" should have been one of the things on the list.
    If you type "." (you've now type "my."), the editor should have changed it to "My." and showed you a list of items that could possibly follow "My.", which "Computer" should have been on that list. You should have been able to type the "c" and "Computer" would have been selected, so if you hit the Tab key, the editor would have filled that in for you.

    I don't necessarily type all that quickly, but another programmer who was watching me enter some VB code in Visual Studio made a comment about not having seen someone type in code so quickly. It wasn't that I was so quick, but that I'm very use to watching the list and after typing a few letters (often one or two), the items desired is selected in the list, so I hit tab and a period to get to the next level of the hierarchy (if needed).
    In the particular case he was observing, I was checking for a button in a mouse event, so that can look impressive to the uninitiated because of the length of the text the editor can fill in for you as you type.
    In particular I typed in
    "If e.b{tab} = l{tab}"
    Which the editor modified to
    "If e.Button = Windows.Forms.MouseButtons.Left"

    I've probably typed that so many times that I don't have to hesitate when watching the list, so my quick typing of 10 characters results in 43 characters on the screen, so I can see how that looks like you're typing exceeding fast. Of course after I type "then" at the end of the line and hit return, the editor changes it to "Then" and automatically inserts an "End If" below the new blank line so you don't have to type that in later.

  8. #8

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    51

    Re: Visual Basic ... trouble with writing to a file

    Computer does not come up after My. and the compiler issues an error (BC30456) "Computer is not a member of atomizer.My" and (BC30451) "Format is not declared …"

  9. #9
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Visual Basic ... trouble with writing to a file

    Quote Originally Posted by DennyGinson View Post
    Computer does not come up after My. and the compiler issues an error (BC30456) "Computer is not a member of atomizer.My" and (BC30451) "Format is not declared …"
    That's because you put this into your project that you shouldn't have:
    vb Code:
    1. Namespace My
    2.     Friend Class Computer
    3.         Public Shared Property FileSystem As Object
    4.     End Class
    5. End Namespace
    If you delete all of that code the rest of your code will work correctly again.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  10. #10

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    51

    Re: Visual Basic ... trouble with writing to a file

    I deleted the file … still get the same errors.

    Here's the code as it is now:
    Code:
           'plot r and z
            Dim TestStr As String
            Using writer = My.Computer.FileSystem.OpenTextFileWriter("C:\Users\denny\source\repos\atomizer\test.txt", True)
                Format(jz, "0")
                Format(ir, "0")
                Format(r, 0000E+00)
                Format(z, 0000E+00)
                For jz = 0 To nz
                    For ir = 0 To nr
                        TestStr = (jz, ir, r(ir, jz), z(jz))
                        writer.WriteLine("TestStr")
                    Next ir
                Next jz
            End Using
    6 errors are returned:
    Severity Code Description Project File Line Suppression State
    Error BC30311 Value of type '(jz As Long, ir As Long, Double, Double)' cannot be converted to 'String'. atomizer C:\Users\denny\source\repos\atomizer\Program.vb 2672 Active
    Error BC30456 'Computer' is not a member of 'atomizer.My'. atomizer C:\Users\denny\source\repos\atomizer\Program.vb 2665 Active
    Error BC30451 'Format' is not declared. It may be inaccessible due to its protection level. atomizer C:\Users\denny\source\repos\atomizer\Program.vb 2666 Active
    Error BC30451 'Format' is not declared. It may be inaccessible due to its protection level. atomizer C:\Users\denny\source\repos\atomizer\Program.vb 2667 Active
    Error BC30451 'Format' is not declared. It may be inaccessible due to its protection level. atomizer C:\Users\denny\source\repos\atomizer\Program.vb 2668 Active
    Error BC30451 'Format' is not declared. It may be inaccessible due to its protection level. atomizer C:\Users\denny\source\repos\atomizer\Program.vb 2669 Active
    Last edited by DennyGinson; May 13th, 2019 at 11:56 AM.

  11. #11

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    51

    Re: Visual Basic ... trouble with writing to a file

    Caught a couple errors … corrected them:

    Code:
           'plot r and z
            Using writer = My.Computer.FileSystem.OpenTextFileWriter("C:\Users\denny\source\repos\atomizer\test.txt", True)
                Format(jz, "0")
                Format(ir, "0")
                Format(r, "0000E+00")
                Format(z, "0000E+00")
                For jz = 0 To nz
                    For ir = 0 To nr
                        writer.WriteLine(jz, ir, r(ir, jz), z(jz))
                    Next ir
                Next jz
            End Using
    Error codes:
    Severity Code Description Project File Line Suppression State
    Error BC30456 'Computer' is not a member of 'atomizer.My'. atomizer C:\Users\denny\source\repos\atomizer\Program.vb 2664 Active
    Error BC30451 'Format' is not declared. It may be inaccessible due to its protection level. atomizer C:\Users\denny\source\repos\atomizer\Program.vb 2665 Active
    Error BC30451 'Format' is not declared. It may be inaccessible due to its protection level. atomizer C:\Users\denny\source\repos\atomizer\Program.vb 2666 Active
    Error BC30451 'Format' is not declared. It may be inaccessible due to its protection level. atomizer C:\Users\denny\source\repos\atomizer\Program.vb 2667 Active
    Error BC30451 'Format' is not declared. It may be inaccessible due to its protection level. atomizer C:\Users\denny\source\repos\atomizer\Program.vb 2668 Active

  12. #12
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Visual Basic ... trouble with writing to a file

    I would test using a new project.
    Select to create a project, and choose which type you want. I would recommend a Windows Form project, but you can try a Console application again.
    If you Console Application, you should be presented with a Main Sub, and I would try typing in "My." in that sub and see what happens.

    I don't have VS2019 installed anywhere, but I assume it hasn't changed that much, but since there were different variations of the install depending on your target before, i.e. Windows Desktop or Net Core, etc. perhaps it is possible you're not using the variation you should be using for Windows Desktop application development.

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Visual Basic ... trouble with writing to a file

    What happens if you get rid of My.Computer? You don't need to add all the decorations, and it may well be that just getting rid of the My.Computer. will allow the compiler to find the right type. On the other hand, I don't use VS2019, yet, and things do change.
    My usual boring signature: Nothing

  14. #14

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    51

    Re: Visual Basic ... trouble with writing to a file

    The project was started the way passel mentioned … as for getting rid of My.Computer, as adding
    Code:
    Private ReadOnly FileSystem As Object
    (which cleared the "not declared" error) I've got this (but Format is still "not declared"):
    Code:
           'plot r and z
            Using writer = FileSystem.OpenTextFileWriter("C:\Users\denny\source\repos\atomizer\test.txt", True)
                Format(jz, "0")
                Format(ir, "0")
                Format(r, "0000E+00")
                Format(z, "0000E+00")
                For jz = 0 To nz
                    For ir = 0 To nr
                        writer.WriteLine(jz, ir, r(ir, jz), z(jz))
                    Next ir
                Next jz
            End Using

  15. #15

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    51

    Re: Visual Basic ... trouble with writing to a file

    I also got an error …
    Severity Code Description Project File Line Suppression State
    Error BC35000 Requested operation is not available because the runtime library function 'Microsoft.VisualBasic.CompilerServices.Conversions.ChangeType' is not defined. atomizer C:\Users\denny\source\repos\atomizer\Program.vb 2679

    It's for the line writer.writeLine(...

    I'm lost!
    Last edited by DennyGinson; May 13th, 2019 at 03:23 PM.

  16. #16
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Visual Basic ... trouble with writing to a file

    I still think you should try a new project. Your current project may be jacked up, so that continuing to patch it isn't getting rid of crud that has built up within it.
    Trying to fix something that should be easy, but has been damaged in some unknown way, can often lead to frustration. Starting with a clean slate, and avoiding the mistakes that may have got you into the woods, is advisable.

    Starting with a simple concept and making sure that works before adding the next "simple" bit of code instead of adding all the code in a big bang approach, can also help save time. It is easier to test one thing, than to troubleshoot 20 problems.

    An example of a simple test of writing strings to a file for instance, although I don't have 2019, and I haven't started using 2017 for much yet since it isn't what we currently use at work, I do have 2017 installed on this computer, and after getting the Microsoft account straightened out, and the license validated, I can do a test using the VS2017 installed on this computer.

    So, I selected Create New project.
    Then picked "Visual Basic", then "Windows Desktop" and finally "Console App".

    The IDE presented me a Module1.vb file with the following contents.
    Code:
    Module Module1
    
        Sub Main()
    
        End Sub
    
    End Module
    I added five lines to the Module
    Code:
    Module Module1
    
        Sub Main()
            Using writer = My.Computer.FileSystem.OpenTextFileWriter("C:\c\test.txt", False)
                For i As Integer = 1 To 10
                    writer.WriteLine(String.Format("Line {0}", i))
                Next
            End Using
        End Sub
    
    End Module
    I clicked the "Start" selection on the toolbar. After a quick compile, the console window open briefly, the code was run and the console window closed.
    I checked the "C:\c" directory and doubleClicked on the Text.txt file, and the file opened in Notepad with the following contents.
    Code:
    Line 1
    Line 2
    Line 3
    Line 4
    Line 5
    Line 6
    Line 7
    Line 8
    Line 9
    Line 10
    The program ran as I would expect, and produced the file with the contents I expected. This is a quick and simple test.
    If you start a new project, and do this same test, and it doesn't work, then that is an indication that there is a critical problem with the way you have things set up.
    Last edited by passel; May 13th, 2019 at 05:44 PM.

  17. #17
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Visual Basic ... trouble with writing to a file

    I thoroughly second that suggestion, especially after reading the first snippet in #14. That makes me queasy, as it sounds more like tossing code at a problem until the errors go away, rather than understanding the solution. I'm not quite sure what

    Code:
    Private ReadOnly FileSystem As Object
    even is. If the ReadOnly wasn't there, then this would be declaring a new variable of type Object with the name FileSystem. Yes, that would mean that FileSystem was declared...it isn't the FileSystem you are trying to use later on, because that object is hidden by the new one you declared. However, I've never seen ReadOnly used in an object declaration. Therefore, that looks a whole lot like a property declaration, except that it appears to be missing the word Property for it to be a property.

    So, what is that, and why do you think it fixes something?

    Nevermind the first part. I didn't know you could put ReadOnly into a variable declaration. In all these years, I've never done that, and don't see much use for it. However, in this case it is pretty bad. All that line does is delays finding a problem until runtime, at that point, the FileSystem object will not have the desired method and the program will crash.
    Last edited by Shaggy Hiker; May 13th, 2019 at 05:47 PM.
    My usual boring signature: Nothing

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Visual Basic ... trouble with writing to a file

    You shouldn't be using Format anyway. If you're using VB.NET then use VB.NET, not VB6. You can call String.Format for composite formatting or just call ToString on the value if formatting a single value. The code you have is pointless anyway because Format returns a value and you're not using any of the results anyway, so what's the point?

  19. #19
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Visual Basic ... trouble with writing to a file

    There probably isn't a point at this point. You have someone quite familiar with FORTRAN and not familiar with Visual Basic of any version at this point. The way FORTRAN uses Format statements to control the format of data written to files compared to the way Visual Basic does file I/O is a world of difference. I think we just have a case of trying to jump too quickly into taking FORTRAN code and trying to port it to .Net without learning the basics of VB.Net first.

    In this case, a FORTRAN person may know you have to create format strings to pass as a parameter to the Write statement to controls how the variables are encoded and end up searching for Format in .Net and think there is something analogous in their use. There are bound to be some missteps along the way.

    I think this is a case of being a bit too enthusiastic and jumping from the diving board when some swimming lessons in the shallow end first are in order.

  20. #20

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    51

    Re: Visual Basic ... trouble with writing to a file

    Interesting … the "Private ReadOnly FileSystem As Object" is what Visual Studio 2019 (Intellisense) offered as a fix … as was the Namespace and Class that was deleted earlier. Yes, I am a 40 year veteran of Fortran who was ordered to do a VBA/Excel project that blew Excel up (froze it like a rock). The code was developed in VBA and copied to Visual Studio. It works fine (in the Excel environment) and all I'm trying to do is convert the parts of the code that wrote to cells in a spreadsheet to write to an external file. Doesn't sound hard does it? Well … it is. So … you're right … I dove in … but the diving board was a plank I was forced to walk. You can't spend time studying when you're busy treading water.

    passel … thanks … I'll try a new project … all I'm doing is chasing my tail at this point.

  21. #21
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Visual Basic ... trouble with writing to a file

    The My namespace and a lot of functionality in it is provided for free in many VB projects. If you write code that tries to use a type that doesn't exist then VS will try to be helpful and offer generic solutions to the problem but creating your own My namespace is never a solution. The question that needs to be answered is why you don't have a My namespace to begin with. Maybe it's a bug in VS 2019, which is still fairly new. Maybe it has something to do with the type of project you created in the first place or something that you did to it after creating it. I just created a VB Console app project targeting .NET 4.7.2 in VS 2017 and My.Computer.FileSystem was available to me. I don't have VS 2019 installed yet to test it. Have you tried creating a new Console app project and checking whether it's available to you there?

    That said, you really don't have to use My.Computer.FileSystem in most cases anyway. You can achieve the same result as from calling OpenTextFileWriter by invoking a constructor of the StreamWriter class. Both will result in the creation and return of a StreamWriter object, so I'd just go ahead and do that. Personally, I'd be doing that all the time anyway. I only use the My namespace when it adds genuine value over the alternative. As someone who codes in C#, where there is no My namespace, as well as VB, I prefer to write as close to the same code in both languages as I can.

  22. #22

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    51

    Re: Visual Basic ... trouble with writing to a file

    jmcilhinney … thanks! Would you give me an example of "invoking a constructor of the StreamWriter class"?

  23. #23
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Visual Basic ... trouble with writing to a file

    You invoke a constructor using the New keyword, which I expect that you have done before. If not, you should do some reading on the subject. If you want to know what constructors are available on that type, Intellisense can tell you or you can read the documentation, which you can navigate to directly or find with a search using the obvious keywords, i.e. "streamwriter constructor".

  24. #24
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Visual Basic ... trouble with writing to a file

    Quote Originally Posted by DennyGinson View Post
    Interesting … the "Private ReadOnly FileSystem As Object" is what Visual Studio 2019 (Intellisense) offered as a fix … as was the Namespace and Class that was deleted earlier.
    Yeah, VS tries to be helpful. That doesn't mean it's right. When the suggestion is basically, "I don't recognize this thing, would you like me to create it?" The answer is NO!!!

    If the help is a suggestion to import this or that, then go for it. When it wants to create something that you think should exist, then you are misunderstanding something, and that help is not helpful. The reason that is there is so that people can write along the lines of "there will be this function, I just haven't written it, yet", at which point VS is offering to create a stub for you to flesh out later. If that's the way you work, then go for it. However, when it is offering to flesh out something that you think is part of the language, or part of a library, and is NOT something that you fully expect and intend to write yourself, then you should not accept that "help".
    My usual boring signature: Nothing

  25. #25

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    51

    Re: Visual Basic ... trouble with writing to a file

    Thanks folks! I appreciate the help!

  26. #26

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    51

    Re: Visual Basic ... trouble with writing to a file

    Update … I started a new project in VS 2019 and tried the code snippet to write to a file … and got the same error. Apparently either there's a bug in VS 2019 or something regarding this topic has changed. I'm downloading VS 2017 and trying this again.

  27. #27
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Visual Basic ... trouble with writing to a file

    I just happened to install VS2019 community a few minutes ago on this machine.
    Seems like a reasonable first test.
    The menu is a little bit different.
    First, selected "Create New Project".
    Then select "Console App", and sub item "Windows" (had "C++" , "Windows", and "Console" as my choices in this installation).

    I'm not sure what the difference is between the sub items "Windows" and "Console". I guess if you're running some kind of non-gui command line server interface, you would choose Console?
    hmmm, that didn't seem to do anything.

    There are a lot of options, so scrolling further down the selections and we have "Console App (.Net Framework).
    Clicking on that doesn't seem to do anything either.

    Lets see what else we have... Ok filter selections at the top.
    So, for Project type, let's select "Desktop".
    For Platform, let's select "Windows".
    For Language, let's select "Visual Basic"
    hmm, I don't see Console app as an option, only WPF (.Net), Win Forms (.Net), WPF Browser (.Net), Win Service (.Net), Empty project (.Net) and Shared Project.

    Go back to Project Type filter selection, and select Console App.
    Now only have one selection showing, Console App (with sub items "Visual Basic" "Windows" "Console"). The sub items must just be the specifics of the type of project, not selections themselves.

    I think I see my issue, I'm suppose to click on the project type then select "Next" button at the bottom (or double click project).
    On the next page, select Create button.

    Now I get the familiar starting code snippet
    Code:
    Module Module1
    
        Sub Main()
    
        End Sub
    
    End Module
    And add the four lines a
    Code:
    Module Module1
    
        Sub Main()
            Using writer = My.Computer.FileSystem.OpenTextFileWriter("C:\c\test.txt", False)
                For i As Integer = 1 To 10
                    writer.WriteLine(String.Format("Line {0}", i))
                Next
            End Using
        End Sub
    
    End Module
    Click the start button, and it runs and produces the file as before. So, VS2019 works as expected, although there are a lot of options and perhaps somehow you chose a console App without .Net support, which is why you're having issues, although I wouldn't think that would be possible with a VB.Net project. Of course, perhaps you don't have a VB.Net project, and are typing VB.Net code into a console project that is neither vb.net or .Net in general.

  28. #28

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    51

    Re: Visual Basic ... trouble with writing to a file

    Ok … the 2017 version works with the method described above. I'm sticking with that.

  29. #29

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    51

    Re: Visual Basic ... trouble with writing to a file

    Almost there! The code below works ...

    Code:
    Module Module1
    
        Sub Main()
            Dim r As Double
            Dim z As Double
    
            Using writer = My.Computer.FileSystem.OpenTextFileWriter("C:\users\denny\source\repos\atomizer model\test.txt", False)
                r = 1.0
                z = 2.0
                For j = 1 To 3
                    For i As Integer = 1 To 4
                        writer.WriteLine(String.Format("{0}", j, "{0}", i, "{0.0000E0}", r, "{0.0000E0}", z))
                    Next
                Next
            End Using
        End Sub
    End Module
    What I want is this:

    1 1 1.0000e01 2.0000E01
    1 2 1.0000e01 2.0000E01
    1 3 1.0000e01 2.0000E01
    1 4 1.0000e01 2.0000E01
    2 1 1.0000e01 2.0000E01
    2 2 1.0000e01 2.0000E01
    2 3 1.0000e01 2.0000E01
    2 4 1.0000e01 2.0000E01
    3 1 1.0000e01 2.0000E01
    3 2 1.0000e01 2.0000E01
    3 3 1.0000e01 2.0000E01
    3 4 1.0000e01 2.0000E01

    What I get is this:

    1
    1
    1
    1
    2
    2
    2
    2
    3
    3
    3
    3

    If I use Write(" .... I get this:

    111122223333

    The variables r and z are not writing and the format is not what I want … how do I do this correctly?

  30. #30
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Visual Basic ... trouble with writing to a file

    In Visual Studio, if you put your cursor on the word "Format" in that line, and press the F1 key, you should get help with how to use the method. I would look at the numeric formats, there should be one for engineering notation.

  31. #31

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    51

    Re: Visual Basic ... trouble with writing to a file

    Here's the code now:

    Code:
    Module Module1
    
        Sub Main()
            Dim r As Double
            Dim z As Double
    
            Using writer = My.Computer.FileSystem.OpenTextFileWriter("C:\users\denny\source\repos\atomizer model\test.txt", False)
                r = 1.0
                z = 2.0
                For j = 1 To 3
                    For i As Integer = 1 To 4
                        writer.Write(String.Format("{0}", j))
                        writer.Write(String.Format("    {0}", i))
                        writer.Write(String.Format("    "))
                        writer.Write(String.Format("0.0###E+00", r))
                        writer.Write(String.Format("    "))
                        writer.Write(String.Format("0.0###E+00", z))
                        writer.WriteLine()
                    Next
                Next
            End Using
        End Sub
    End Module
    Here's the output:

    1 1 0.0###E+00 0.0###E+00
    1 2 0.0###E+00 0.0###E+00
    1 3 0.0###E+00 0.0###E+00
    1 4 0.0###E+00 0.0###E+00
    2 1 0.0###E+00 0.0###E+00
    2 2 0.0###E+00 0.0 ###E+00
    2 3 0.0###E+00 0.0###E+00
    2 4 0.0###E+00 0.0###E+00
    3 1 0.0###E+00 0.0###E+00
    3 2 0.0###E+00 0.0###E+00
    3 3 0.0###E+00 0.0###E+00
    3 4 0.0###E+00 0.0###E+00

    As you can see, I've got the arrangement the way I want it … now how do I get the variable values to be written … it's just writing the string that specifies the format of the variable value. There is a space between each value written that's not showing up here.
    Last edited by DennyGinson; May 14th, 2019 at 01:34 PM.

  32. #32
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Visual Basic ... trouble with writing to a file

    There are multiple ways you can format. After reading a bit from the pages found by hitting the Help key (F1), I tried this line:

    writer.WriteLine(String.Format("{0} {1} {2} {3}", j, i, r.ToString("e4"), z.ToString("e4")))
    It produced
    Code:
    1 1 1.0000e+000 2.0000e+000
    1 2 1.0000e+000 2.0000e+000
    1 3 1.0000e+000 2.0000e+000
    1 4 1.0000e+000 2.0000e+000
    2 1 1.0000e+000 2.0000e+000
    2 2 1.0000e+000 2.0000e+000
    2 3 1.0000e+000 2.0000e+000
    2 4 1.0000e+000 2.0000e+000
    3 1 1.0000e+000 2.0000e+000
    3 2 1.0000e+000 2.0000e+000
    3 3 1.0000e+000 2.0000e+000
    3 4 1.0000e+000 2.0000e+000
    Not quite the format you wanted. You may have to look at the link of custom numeric formats on that page, to see if you can coerce to the format you want.
    (p.s. but based on the previous post you posted while I tried this, perhaps this format is acceptable).
    Last edited by passel; May 14th, 2019 at 01:44 PM.

  33. #33
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Visual Basic ... trouble with writing to a file

    I changed r to be 12 and z to be 23, to see if it produced a non-zero exponent, and this was the result.
    Code:
    1 1 1.2000e+001 2.3000e+001
    1 2 1.2000e+001 2.3000e+001
    1 3 1.2000e+001 2.3000e+001
    1 4 1.2000e+001 2.3000e+001
    2 1 1.2000e+001 2.3000e+001
    2 2 1.2000e+001 2.3000e+001
    2 3 1.2000e+001 2.3000e+001
    2 4 1.2000e+001 2.3000e+001
    3 1 1.2000e+001 2.3000e+001
    3 2 1.2000e+001 2.3000e+001
    3 3 1.2000e+001 2.3000e+001
    3 4 1.2000e+001 2.3000e+001

  34. #34
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Visual Basic ... trouble with writing to a file

    As explained on the page that F1 takes you to, you could add spacing to your string if you wanted fixed number of spaces between fields, or you could specify a "field" size, so the field will be padded with a variable number of space depending on the number of characters in the value, to help keep things aligned in columns.

    In the 3rd case below, you see I added the e4 format to the field tag, rather than in the .ToString method of the variable.
    Code:
    Module Module1
        Private r As Double, z As Double
    
        Sub Main()
            Using writer = My.Computer.FileSystem.OpenTextFileWriter("C:\c\test.txt", False)
                r = 12.0
                z = 23.0
                For j = 1 To 3
                    For i As Integer = 1 To 4
                        writer.WriteLine(String.Format("{0}      {1} {2}     {3}", j, i, r.ToString("e4"), z.ToString("e4")))
                    Next
                Next
                writer.WriteLine()
                For j = 1 To 3
                    For i As Integer = 1 To 4
                        writer.WriteLine(String.Format("{0} {1,6} {2} {3,15}", j, i, r.ToString("e4"), z.ToString("e4")))
                    Next
                Next
                writer.WriteLine()
                For j = 1 To 3
                    For i As Integer = 1 To 4
                        writer.WriteLine(String.Format("{0} {1,6} {2:e4} {3,15:e4}", j, i, r, z))
                    Next
                Next
    
            End Using
        End Sub
    
    End Module
    All three loops create the same output, with this set of numbers. Of course the first one with fixed number of spaces between fields will differ from the second two if the number of characters in a given field increase (i.e. more significant digits in the number).
    Code:
    1      1 1.2000e+001     2.3000e+001
    1      2 1.2000e+001     2.3000e+001
    1      3 1.2000e+001     2.3000e+001
    1      4 1.2000e+001     2.3000e+001
    2      1 1.2000e+001     2.3000e+001
    2      2 1.2000e+001     2.3000e+001
    2      3 1.2000e+001     2.3000e+001
    2      4 1.2000e+001     2.3000e+001
    3      1 1.2000e+001     2.3000e+001
    3      2 1.2000e+001     2.3000e+001
    3      3 1.2000e+001     2.3000e+001
    3      4 1.2000e+001     2.3000e+001
    
    1      1 1.2000e+001     2.3000e+001
    1      2 1.2000e+001     2.3000e+001
    1      3 1.2000e+001     2.3000e+001
    1      4 1.2000e+001     2.3000e+001
    2      1 1.2000e+001     2.3000e+001
    2      2 1.2000e+001     2.3000e+001
    2      3 1.2000e+001     2.3000e+001
    2      4 1.2000e+001     2.3000e+001
    3      1 1.2000e+001     2.3000e+001
    3      2 1.2000e+001     2.3000e+001
    3      3 1.2000e+001     2.3000e+001
    3      4 1.2000e+001     2.3000e+001
    
    1      1 1.2000e+001     2.3000e+001
    1      2 1.2000e+001     2.3000e+001
    1      3 1.2000e+001     2.3000e+001
    1      4 1.2000e+001     2.3000e+001
    2      1 1.2000e+001     2.3000e+001
    2      2 1.2000e+001     2.3000e+001
    2      3 1.2000e+001     2.3000e+001
    2      4 1.2000e+001     2.3000e+001
    3      1 1.2000e+001     2.3000e+001
    3      2 1.2000e+001     2.3000e+001
    3      3 1.2000e+001     2.3000e+001
    3      4 1.2000e+001     2.3000e+001
    Last edited by passel; May 14th, 2019 at 02:06 PM.

  35. #35

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    51

    Re: Visual Basic ... trouble with writing to a file

    Outstanding! It works … would you explain the ("{0} {1} {2} {3} ..." it obviously works but, what, specifically, is it doing?

  36. #36
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: Visual Basic ... trouble with writing to a file

    Quote Originally Posted by DennyGinson View Post
    Outstanding! It works … would you explain the ("{0} {1} {2} {3} ..." it obviously works but, what, specifically, is it doing?
    https://docs.microsoft.com/en-us/dot...tframework-4.8

  37. #37
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Visual Basic ... trouble with writing to a file

    Quote Originally Posted by DennyGinson View Post
    Outstanding! It works … would you explain the ("{0} {1} {2} {3} ..." it obviously works but, what, specifically, is it doing?
    Try changing the string to ("{3}, {2}, {1}, {0}" or ("{0} {0}, {0}, {1}, {1}" and see the result and see if you can empirically figure out what those might be.
    Or, you could read the documentation as PlausiblyDamp suggests.

    Part way down that page, you'll see a bunch of examples, such as this one, which should also help divine what those tags correlate to.
    Code:
    Dim s As String = String.Format("At {0}, the temperature is {1}°C.",
                                    Date.Now, 20.4)
    ' Output similar to: 'At 4/10/2015 9:29:41 AM, the temperature is 20.4°C.'

  38. #38

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    51

    Re: Visual Basic ... trouble with writing to a file

    Thanks! I will definitely check that out.

    BUT … I'm getting an error on the call of this subroutine (call is below):
    Code:
           'Calculate and store grid cell face areas, skew terms, and normal face unit vectors
            GridProp(n, nr, nz, r, z, n1r_west, n1r_east, n1r_south, n1r_north, n1z_west, n1z_east, n1z_south, n1z_north,
            n2r_west, n2r_east, n2r_south, n2r_north, n2z_west, n2z_east, n2z_south, n2z_north,
            aface_west, aface_east, aface_south, aface_north, delta_xsi1_west, delta_xsi1_east, delta_xsi1_south, delta_xsi1_north,
            delta_xsi2_west, delta_xsi2_east, delta_xsi2_south, delta_xsi2_north, aface_low, aface_high, w_index, e_index, s_index, n_index,
            se_index, ne_index, sw_index, nw_index)
    Here's the error:

    Severity Code Description Project File Line Suppression State
    Error BC30521 Overload resolution failed because no accessible 'GridProp' is most specific for these arguments:
    'Public Sub GridProp(n As Integer, nr As Integer, nz As Integer, r As Double(*,*), z As Double(), n1r_west As Double(), n1r_east As Double(), n1r_south As Double(), n1r_north As Double(), n1z_west As Double(), n1z_east As Double(), n1z_south As Double(), n1z_north As Double(), n2r_west As Double(), n2r_east As Double(), n2r_south As Double(), n2r_north As Double(), n2z_west As Double(), n2z_east As Double(), n2z_south As Double(), n2z_north As Double(), aface_west As Double(), aface_east As Double(), aface_south As Double(), aface_north As Double(), delta_xsi1_west As Double(), delta_xsi1_east As Double(), delta_xsi1_south As Double(), delta_xsi1_north As Double(), delta_xsi2_west As Double(), delta_xsi2_east As Double(), delta_xsi2_south As Double(), delta_xsi2_north As Double(), aface_low As Double(), aface_high As Double(), w_index As Integer, e_index As Integer, s_index As Integer, n_index As Integer, se_index As Integer, ne_index As Integer, sw_index As Integer, nw_index As Integer)': Not most specific.
    'Public Sub GridProp(n As Integer, nr As Integer, nz As Integer, r As Double(*,*), z As Double(), n1r_west As Double(), n1r_east As Double(), n1r_south As Double(), n1r_north As Double(), n1z_west As Double(), n1z_east As Double(), n1z_south As Double(), n1z_north As Double(), n2r_west As Double(), n2r_east As Double(), n2r_south As Double(), n2r_north As Double(), n2z_west As Double(), n2z_east As Double(), n2z_south As Double(), n2z_north As Double(), aface_west As Double(), aface_east As Double(), aface_south As Double(), aface_north As Double(), delta_xsi1_west As Double(), delta_xsi1_east As Double(), delta_xsi1_south As Double(), delta_xsi1_north As Double(), delta_xsi2_west As Double(), delta_xsi2_east As Double(), delta_xsi2_south As Double(), delta_xsi2_north As Double(), aface_low As Double(), aface_high As Double(), w_index As Integer, e_index As Integer, s_index As Integer, n_index As Integer, se_index As Integer, ne_index As Integer, sw_index As Integer, nw_index As Integer)': Not most specific. atomizer model C:\Users\denny\source\repos\atomizer model\atomizer model\Module1.vb 92 Active

    Here's the beginning of the subroutine:

    Code:
            Public Sub GridProp(n As Integer, nr As Integer, nz As Integer, r As Double(,), z As Double(), n1r_west As Double(), n1r_east As Double(), n1r_south As Double(), n1r_north As Double(),
            n1z_west As Double(), n1z_east As Double(), n1z_south As Double(), n1z_north As Double(), n2r_west As Double(), n2r_east As Double(), n2r_south As Double(), n2r_north As Double(),
            n2z_west As Double(), n2z_east As Double(), n2z_south As Double(), n2z_north As Double(), aface_west As Double(), aface_east As Double(), aface_south As Double(), aface_north As Double(),
            delta_xsi1_west As Double(), delta_xsi1_east As Double(), delta_xsi1_south As Double(), delta_xsi1_north As Double(), delta_xsi2_west As Double(), delta_xsi2_east As Double(),
            delta_xsi2_south As Double(), delta_xsi2_north As Double(), aface_low As Double(), aface_high As Double(), w_index As Integer, e_index As Integer, s_index As Integer,
            n_index As Integer, se_index As Integer, ne_index As Integer, sw_index As Integer, nw_index As Integer)
    This was ok in VS2019. The main thing is I don't understand the error and the documentation is just not very helpful. Thoughts?

  39. #39
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Visual Basic ... trouble with writing to a file

    Just hopping in to this to make this one suggestion and then I'm wishing you good luck and won't be able to assist further.

    You are trying to do way too much at once without intermediate testing. My suggestion is to pare down your subroutine parameters to perhaps the first 5, then try to call that subroutine while passing 5 appropriate values and make sure your code in the subroutine is properly handling those initial values. Once that is working, add the next 5 parameters. And so on, until they are all added.

  40. #40

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    51

    Re: Visual Basic ... trouble with writing to a file

    Ok … thanks!

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width