Jump to content

Why is this doing this? VB.net help

WaspishVyper

This is the code I'm using, I'm trying to create a file with a selected date as the file name,

Private Sub CreateRemind()

Dim selectdate As String = CurrentDateAdmin.Value.ToString("dd-MM-yyyy")

Dim remindersfile As String = "D:\Computing Project\ProjectSocialWork\ProjectSocialWork\bin\Program .txt info\" & usercreate & "\" & selectdate & ".txt"

If System.IO.File.Exists(remindersfile) = False Then

IO.File.Create(7, remindersfile)

FileClose(7)

End If

End Sub

When I run the code I get this error message

Conversion from string "D:\Computing Project\ProjectSoci" to type 'Integer' is not valid.

I don't understand why this is happening, and any help towards the solution would be great

Thanks in advance

Waspishvyper

Link to comment
Share on other sites

Link to post
Share on other sites

I haven't done VB for a while so I'm going from memory here. I know that C# requires you to escape the strings because the \ is considered an escape character so in order to get the string to include the \ characters you need to precede the definition with an @ symbol. I don't remember if this applies in VB.

Also, probably better if you define your variable on one line and then assign a value on another line. For example:

Dim numDays As Integer

numDays = 7

Current Rig
AMD Ryzen 5900X - Asus ROG Strix X570-E Gaming WiFi 2 - 32 GB GSkill TridentZ RGB
GeForce RTX 3080 - WD Black SN850 1TB  - Lian Li O11 Dynamic XL

Link to comment
Share on other sites

Link to post
Share on other sites

Are the variables userdate and selectdate possibly integers? The last time I used VB was like, 2009, so i'm not sure if that's an issue or not.

Can you give us a line number?

Link to comment
Share on other sites

Link to post
Share on other sites

Another thing I just noticed. I don't see any definitions for a variable called userCreate in your code above. You'll have to define it to use it.

Current Rig
AMD Ryzen 5900X - Asus ROG Strix X570-E Gaming WiFi 2 - 32 GB GSkill TridentZ RGB
GeForce RTX 3080 - WD Black SN850 1TB  - Lian Li O11 Dynamic XL

Link to comment
Share on other sites

Link to post
Share on other sites

Are you sure Usercreate isn't an integer?

Select date should be all good, I just can't see how you defined usercreate initially.

Link to comment
Share on other sites

Link to post
Share on other sites

Another thing I just noticed. I don't see any definitions for a variable called userCreate in your code above. You'll have to define it to use it.
usercreate is defined globally as a string at the top of the program, I didn't want to paste the whole program cause its like 40 pages long in word
Link to comment
Share on other sites

Link to post
Share on other sites

I haven't done VB for a while so I'm going from memory here. I know that C# requires you to escape the strings because the \ is considered an escape character so in order to get the string to include the \ characters you need to precede the definition with an @ symbol. I don't remember if this applies in VB.

Also, probably better if you define your variable on one line and then assign a value on another line. For example:

Dim numDays As Integer

numDays = 7

provided that its in quotation marks the \ is considered part of the string, in this case a file path, and I do normally define variables the way you suggested, but its easier for me to see certain things, specifically names of objects when they stand out from the rest of my code.
Link to comment
Share on other sites

Link to post
Share on other sites

Are the variables userdate and selectdate possibly integers? The last time I used VB was like, 2009, so i'm not sure if that's an issue or not.

Can you give us a line number?

IO.File.Create(7, remindersfile)

it was this line

Link to comment
Share on other sites

Link to post
Share on other sites

Imports System

Imports System.IO

Imports System.Text

Public Class Test

Public Shared Sub Main()

Dim path As String = "D:\Computing Project\ProjectSocialWork\ProjectSocialWork\bin\Pr ogram .txt info\" & usercreate & "\" & CurrentDateAdmin.Value.ToString("dd-MM-yyyy") & ".txt"

Try

If File.Exists(path) Then

' Note that no lock is put on the

' file and the possibility exists

' that another process could do

' something with it between

' the calls to Exists and Delete.

File.Delete(path)

End If

' Create the file.

Dim fs As FileStream = File.Create(path)

Dim info As Byte() = New UTF8Encoding(True).GetBytes("This is some text in the file.")

' Add some information to the file.

fs.Write(info, 0, info.Length)

fs.Close()

' Open the stream and read it back.

Dim sr As StreamReader = File.OpenText(path)

Do While sr.Peek() >= 0

Console.WriteLine(sr.ReadLine())

Loop

sr.Close()

Catch ex As Exception

Console.WriteLine(ex)

End Try

End Sub

End Class

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×