Programming in .Net

Status
Not open for further replies.

JustinW

Super Moderator
Staff member
I'm currently taking a VB class in school and had to design a program that takes a .txt file and reads it line by line to display the fields that make up an employee record on a form for the user to view. We had to previously write a program that wrote the data to the file and this program is only supposed to be capable of retrieving and displaying the data from the file.

When the form is loading, the application asks the user for the name of the file the application is to work with via input box, stores the value to a string variable (strFilename) and appends \ at the start and .txt at the end, and is supposed to display the first record (or first 8 lines in the file) in the appropriate labels on the form corresponding to the field names. With this in mind, it checks the root of the device the project is read from and doesn't require the user to enter .txt (only the filename). If the file doesn't exist, it displays an error message and than terminates the application.

The problem I'm having here is with the IO.File.Exists method. I get the input from the user and use an if statement to make sure it exists. If its true, it runs the rest of the code and else it is supposed to terminate as described above. Here is an example of how its setup:

Code:
if IO.File.Exists(strFileName) Then
[INDENT]'Runs the rest of the startup routine as the file exists
[/INDENT]Else
[INDENT]'Terminate the application after displaying an error
[/INDENT]End If

I use test.txt and though the if condition should be true, I step through the code one line at a time to discover it gets to the if statement and than immediately goes to Else and executes the code in there though test.txt is valid.

All code is correct from what my instructor and myself could see (he went ahead and gave me the grade for it, thinking that VS appended another .txt to the end of it automatically, making it impossible to fix). The application runs fine on my laptop but not in the lab (We haft to present our applications before they are accepted to ensure they are working).

I think it has to do with perhaps one of the many hundreds of settings Visual Studio has. Or maybe the inital enviornment configuration? My setup at home is general but the setup in the lab is setup specifically for VB. Any one know of what setting (if one) could be causing this type of behavior to occur or know of a workaround? I'm using VS 2008.
 
Justin, the only thing I can think of is the "Current Directory" value.... it should usually be the same directory as the one that the exe is in.... though that depends on how the exe is being called.

Try using absolute instead of relative paths (C:\absolute\path\to\file vs relative\path\to\file).
 
Okay must have been human error or something VS did, cause I was just looking at my drive to find test.txt.txt. I always show extensions at home but the labs computers don't and I must have forgot about that at the time cause it looked just fine to me (ie. looked like test.txt in the lab). After a few hours of coding fun, you start to forget about the samll stuff.
 
Last edited:
I can't figure why anyone would want their filetype suffix invisible, and MS making it the default seems crazy to me.
Far better to have a sample.exe with a sample.txt file and a sample.bmp spash than have to name them all something different just because you can't tell them apart.
 
Last edited:
Absolutely.

It drives me nuts... Even worse is that Windows cannot handle hidden suffixes intelligently. With suffixes hidden, many times you'll end up creating documents and/or files with .something.somethingelse names; and Windows doesn't give you any good way of realizing wtf you did.

Last semester I was giving a course in PHP programming, and over 80% of the students, upon attempting to use notepad to save something as "my file.php" would end up with a file called "my file.php.txt" except there's no way on earth to (logically) find out that it was a txt file.. especially when they were expecting it to be something else.

But then again, you have Linux which does away with filename extensions completely (relying on the MIME type and the headers of the file itself instead of the extension to determine what type of file it is and what app should be used to open it)... But having extensions is a pretty nice way of categorizing different file types; so on either platform it's a bit of a drag.

I guess OS X is the best compromise.
 
Actually Windows has gotten somewhat better with extensions with Vista. Vista unlike previous versions is smart enough to only highlight the name part of the file before the extension when naming files if you have hide extensions for known file types disabled. Unless the user is paranoid and highlights everything manually, it is less likely to accidently take out the extension.
 
But the problem isn't so much accidently removing the extension as much as it is accidently adding a second, "fake" extension to the file..... and Vista (unfortunately) has yet to address that....

I guess it's easy enough to make it show the extension though...
 
Yeah.. just checking when the user keeps the default settings would help. It wouldn't be necssary otherwise.
 
I'm playing around with some of the other things we didn't get to in class... so here's another question.

I'm playing around with timers to do stuff in my applications. From my understanding timers once enabled work continously until you disable them. In a test application I put two timers on a form. One of them is for the amount of time while the other is to update a label on the form with the amount of time left on a second basis (like a digital clock). I know the Timer.Interval property can be used to display the amount of time the timer has been set for, but is there another property I'm missing that constantly updates with the amount of time left before the next tick event so I can display it on the form or do I need to have the interval reset each time/use variables/loops to keep track of it?

EDIT: Guess not. Just looked through the entire list from intellisense. Unless one of the parameters to the tick event is the time left. Oh well, thats what variables/loops are for.

Addendum:

Well if theres a better way great but I tried resetting the interval each second and it worked! :smile:
 
Last edited:
You would need another timer, this one with an interval of one second.... the handler for the timer's TICK event would decrement a global variable containing the interval of the first timer by 1 second.

The TICK handler for the first timer would reset that global variable each time it is called.
 
I'm trying to declare a global constant array of type Long values in a module an application I am building will need to reference. I declared and initialized the array on the same line as follows:

Code:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Const[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] g_TIMES(9) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Long[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = {300000L, 600000L, 900000L, [/SIZE]
[SIZE=2]1200000L, 1800000L, 2700000L, 3600000L, 5400000L, 6300000L, 7200000L}[/SIZE]

I get this error with my mouse over g_Times: Constants must be of an intrinsic or enumerated type, not a class, structure, type parameter, or array type.

And I get this error when I move my mouse over (9): Explicit initialization is not permitted for arrays declared with explicit bounds.

From basic understanding I see that vb won't accept arrays in constants (or at least I think), so I tried a public array itself and got rid of the Const keyword so it wouldn't be considered a named constant anymore. Unfortunately, I am still getting the errors, so is the Long data type not capable of being assigned to arrays? I don't think an integer could hold those values, so I guess my answer here is to use string for the data type and convert when I need to use the values...

EDIT: Went with private variables at the class level for each form in the application that requires it. I guess a couple of extra lines here and there is what it takes.
 
Last edited:
This language bloat was one of the reasons I dropped VB for C#, but in the end they're all good :smile:
 
Yeah I see C# is very similar, but I'm not those type that can easily connect the dots from one to another without practice. I am taking a C# class hopefully sometime this year.

One more thing... i've created some custom classes to use in my program and for one of them I need to make an array of it. Though it is declared at the class level, will adding to the array in a subroutine only last for the scope of that routine or will the changes be permanent to the array as long as the class stays in memory?
 
With arrays of classes, the changes are persistent as long as the object is valid.

.NET creates arrays and copies classes by reference, not value... so changing that will change the original object as well.
 
Umm... how could I set up a button or what code would I need to make it possible to launch another file (like another application stored locally on the computer) or hyperlink?

Addendum:

Is this right as far as declaring an array of a custom class in a module?

Public AnyArray(99) As New CustomClassName

or this....

Public AnyArray(99) As CustomClassName

The first one flat out gives me errors in the code window saying I can't use the New keyword with arrays, but not using the New keyword causes null exceptions to be thrown at runtime because it needs to be an instance of the class. What would be the correct way of going about this? Is it possible to create an array property in a custom class to take care of it? I need to get this working because I already have over 400 lines of code that utilize the class I've spent the last 3 days working on. Definetly enough to make me think programmers are crazy...

This is a learning proccess for me, and I admit I've hit some bumps along the way. Thanks for helping :smile:
 
Last edited:
Well I'm thinking of using a structure for the array instead but surely there MUST be someway of declaring an array of a class....

Couldn't I set up a new array not initalizing it and than copy the custom class into my array as a new instance? Kinda like this:

Code:
Public AnyArray(99) As CustomClass1

And than in a subroutine at runtime, use:

Code:
AnyArray = New CustomClass1

The reason for me wanting to get this to work so badly is that properties of different data types are used to store data into the array of the custom class. While I think structures could be used, its gonna be a little tricky for sure and I'm gonna need to do a lotta work to fix it up this way...
 
Last edited:
Status
Not open for further replies.
Back
Top