Try Catch all declared variables values

rubinov

New member
Joined
Mar 20, 2007
Messages
4
Programming Experience
10+
I have jumped into VB.NET from FoxPro/VFP coding where I used to document all declared variables and their current values at the moment of error. The single line of code does it in FoxPro/VFP:

list memory to filename

Is it possible to implement something similar in VB.NET, getting all declared variables and their values without hard coding them one by one?

Thank you in advance,

Yuri Rubinov
 
urg.. this was asked recently and my answer was: no,without significant use of reflection.

While it may seem that modern programming languages are a bit lame compared to foxpro in this regard, you need to consider:

Exception handling is type specific, and several courses of actions can be programmed in advance
Modern error messages are very good
Thousands of other programmers dont miss such fnctionality, so debugging an app based on error messages you write is usually not difficult
Having a snapshot of all the variable values doesnt tell you much more than what you can already know. Encountering an System.IO.IOException that says "The filename parameter of File.OpenText() cannot be null" kinda tells you that a null was passed in. It wouldnt really help to have a debug system that tells you the parameter was null. Why is it null? maybe the user didnt pick anything. Should you check that they have picked something, or jsut catch the IOException and tell them to pick something? Choices choices... :D
 
I think I'm right when saying that reflection only operate on the types/classes (ie blueprints) and can create and discover members and create instances, but not discover instances in memory.

I don't know if what you ask is possible, if it was I would think it was part of the System.Diagnostics namespace, but I support cjards statements here about primarily using reasonable validation and secondary backup using exception handling as the concensus of error handling in .Net. Most code do not error, and in many cases there is no way an exception can happen if the input is valid. Even if some given class method is documented to give possible exceptions you don't have to Try-Catch it. When an exception may occur you already "know" it from code analysis and know why (its why you decided to Try-Catch the codeblock in the first place!), and Catch it where you can log the info you need and display some meaningful info to user.

The most common exception situations are when input from user is invalid or difficult to validate, or erronous other choosen input (files/addresses), or the output of a calculation result in invalid input for next calculation. Think about the flow of data in the application, where does it come from - where does it go.
 
Thanks, Cjard and JohnH. I appreciate your responses.
But with some sort of sour feeling about what you call as “modern” languages.

It is definitely the good news: I need to debug the application when something totally unexpected happened because there is no way to gather entire information otherwise. So, I may have the opportunity for supervising my product forever, and get my piece from the user/customer pie.

Sure in programming there are often (if not always) several ways to implement the particular action. But anyway the programming language is a tool, and what is the reason to make it less convenient or less effective?

Thanks again,

Yuri Rubinov
 
I think I'm right when saying that reflection only operate on the types/classes (ie blueprints) and can create and discover members and create instances, but not discover instances in memory.

Mmh, I always felt that Reflection could be used on instances-> It can in java. I knocked together this example (sorry it's in C#):

VB.NET:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Reflection;

namespace WindowsApplication1
{
  static class Program
  {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
      Thing t = new Thing();

      MemberInfo[] members = t.GetType().GetMembers();
      foreach(MemberInfo mi in members) {
        if(mi.MemberType == MemberTypes.Field)
          Console.Out.WriteLine(mi.Name + "=" + (mi as FieldInfo).GetValue(t));
      }
    }
  }

  internal class Thing
  {
    public string a = "string value";
    public int b = 1;
  }
}

The console contains:
a=the value
b=1


The problem is, where do you stop? Properties and methods can be enumerated, but I dont know if the local variables within methods can, because they may not be in scope at the time of enumeration.

Yuri; Sorry you have a sour feeling towards modern languages, and yes I admit it is frustrating when something doesnt operate in a style youre accustomed to, but do please consider that I (at least) have been writing apps for a number of years and never needed the functionality you describe. My error messages might look like:

VB.NET:
Try
  'dangerous op
Catch(ioe as IOException)
  MessageBox.Show(string.Format( _
    "A dangerous operation didnt complete. This operation requires that " & _
    "at least one option be on, conflicting options are not set, that" & _
    "you have permission to write to the chosen file, and that no-one" & _
    "else is editing the file when you press the button" & _
    "The error message was:{0}" & _
    "The file you chose was: {1}" & _
    "Options in use were: {2}", ioe.Message, openFileDlog.Filename, optionFlags.ToString()))
    'optionFlags is a Flags Enum
End Try

There is a good case for writing sensible error messages.

If you tell your user:
What went wrong
What you think caused it (or what the restrictions are)
What actions they can take

Then they can quite often work out what to do. Gone are the days of "Error: Not supported. Contact support" or "Error 5: Invalid procedure call or arg" - Write concise but helpful messages and your users will solve most of their problems themselves. Apply strict patterns to your development and engage your brain thinking about the possibles, cater for the wild inputs, the stupidities. Youll find that you write programs with very few bugs, and the enhanced error messaging will mean that the user will solve their own problems without bothering you. Its time invested now to stop the users annoying you later :D
 
I need to debug the application when something totally unexpected happened because there is no way to gather entire information otherwise.

Ah, but how often does something totally unexpected happen? I dont recall such events as being frequent :)
Also, does a snapshot of everything really help? You might know that a value is null.. but it wont tell you WHY it is null. Its like knowing your name is Yuri.. WHY is it yuri? Why did your parents call you that? If you never asked, you wont know, so you will still need to ask the user questions.. The snapshot of the data cannot tell you.

So, I may have the opportunity for supervising my product forever, and get my piece from the user/customer pie.
Hopefully not forever! Microsoft dont support Windows 98 any more! :D
7 years maybe.. "Forever" in computing terms :D


what is the reason to make it less convenient or less effective?
I wouldnt say that not having a "dump all variables" feature makes a language less effective.. It just makes you a better programmer :)

I hope that the stateful reflection example I posted is readable to you. I can translate it into VB if you like.. Do investigate other ways of inventing your wheel though!
 
Cjard,

Thanks for your thoughts and code example. No, I do not need help in translating from C to VB as I feel fluent enough in all of them.
It is very good to have you and JohnH here exchanging ideas and opinions, I can learn a lot from this.

Having my ~30 years experience in programming (c, vb, foxpro, etc) there are only three of your opinions I cannot share. It is just phylosophical or emotional attachment, put it this way, and not too serious.
"Modern" I would not use for Dot.NEt in comparison with VFP. Dot.Net is just MS choice in fight to dominate on the market and get more money.
Overcaming language limitations does not make me better programmer, but make me more experienced in this particular (only) language.
Having "everything at the glance", not "dump all variables", make my work effective.

Again, thanks for your time and attention to this matter. You helped me to understand better where I am and what can be done.
 
"Modern" I would not use for Dot.NEt in comparison with VFP. Dot.Net is just MS choice in fight to dominate on the market and get more money.

Well, in as far as .NET is a pure OO language (because it is IMHO based on the work Microsoft put into Java before Sun sued them and won) that makes it a relatively modern thing. I dont think there's anything particularly about VB.NET that promotes good programming practice, because VB6 typically wasnt the preserve of "hardcore" programmers and VBN's market appeal is based on the VB6 population so it includes backward compatibilties and legacy functions to make the transition from this procedural (VB6) language easier. For some, that's where the transition ends.

.NET also displays more modern notions of independence. Where java is one syntax that runs on a range of platforms, .NET is a range of syntaxes that run on one platform. BOth hence (as you state) can garner wide market appeal. If anything .NET is proving to be more flexible than its java roots, because not only are users creating other syntaxes (Cobol.NET, Ruby.NET) but users are trying to port it to other platforms (MONO) - Microsoft dont have to put any cash into this, and it serves to enhance their product.. Talk about win-win!

So, yeah, I'd say it was modern, in concept - this idea of hardware abstraction and syntax abstraction is a different perspective on the former ways we did things multi-platform (sourcecode and compile for target)

Maybe one day, someone will make a processor that runs IL (what all the .net syntaxes "compile" to) directly.. Would it be the first time that the language the chip operates in natively, existed before the chip did? Hmm...


Overcaming language limitations does not make me better programmer, but make me more experienced in this particular (only) language.
Looking at them another way; they arent limitations - they are just things that arent implemented in the way you like to do things (because they can either be done another way, or you can change the way you do things)
Ultimate flexibility doesnt necessarily confer ultimate power, as im sure you can appreciate. Hopefully your usage of .NET wont be a constant battle within you, trying to make it do things how you would have done in VFP.. Instead, look a little into the way the language and the users do things and try to find something youre happy with, that is a step apart from what you've accumulated to now..

On a side note, I watched Wife Swap last night. They took a city-born designer fashionista with an OCD for cleaning and fengshui, and swapped her with a farming wife from iowa, whose family ate a raw diet purely from their own farm, and didnt believe in cleaning chemicals , pesticides etc. As you can imagine, it's just asking for trouble, but the thing that amazed me most was that city wife spent most of her time telling the farmers that they were wrong. Both wives seemed to take very little away from the experience (though farm wife was a better adopter of "city" ways because she seemed slightly more open minded), and the city wife never once seemed to say "OK, well this life must work for them because they are actually still alive, slimmer and fitter than I am; it's wrong for me, but it's not wrong totally"
The other thing that astounded me was, when farm wife took city family to a farm, they balked at the idea of killing a chicken, saying "I dont want to kill a chicken; I want to go to the store and get chicken" - like a few other people I've known, they didnt seem to correlate the clucking creature in farmer wife's hands, with the packet of pink stuff they buy at the shops. In buying chicken from the shops they were somehow "not killing a chicken" and "being civilised"; I'd have loved for one of the presenters to ask them to think about how much of a state they'd be in if the farmers decided to stop producing chicken for the stores..

How does this fit into the current discussion? In a sort of "accept what the Dark Side do" kind of way. VBN and VFP are different, I'm sure. I never used VFP so I have no accurate comment, but I see VBN and VB6 as two worlds apart as far as city and farm. I started in java and C, went through VB6 and into .NET so its like going full circle, and at each step of the way I tried to pick up on the new language's best bits, and not worry too much about the bits I disagreed with..

Have a play, I'm sure youll gain experience, feel more confortable, and then consider that the extra experience DOES make you a better programmer.. Plus, your stricter grounding in (IMHO) more regimented languages will for sure make you a better-than-average programmer in .NET

Having "everything at the glance", not "dump all variables", make my work effective.
I'm sure VFP had something like a debugger, but if it didnt, then really get into .NET's debugger; Coupled with the structured error handling, it is very very good!

Again, thanks for your time and attention to this matter. You helped me to understand better where I am and what can be done.
No probs; as a community this place has one of the best "feels" ive experienced in a lot of years of forumming - we're here to help, debate etc.. Hope you enjoy this place as a valuable resource for a long time to come (I use it for help with all .NET languages, not just VBN - just make sure most your code samples are in VBN and they dont mind!)
 
Cjard,

Sure VFP has the debugger, and VFP is OOP oriented language, but also with power command interpreter (as an example see my original post). For example, similar to DotNet with /Try ..Catch …Finally/ you can get the Exception object and work with it in VFP.
Therefore I think that VFP belongs to what you call as “modern’ language.

I think, we can continue to exchange opinions about “modern languages”, but it will be not fruitful. Having no experience in VFP you cannot convince me in your opinion, and I cannot induce you to accept my opinion for the same reason.

I remain sincerely grateful to you for your direct and detailed answer to my original question.

Yuri Rubinov
 
Cjard,

Sure VFP has the debugger, and VFP is OOP oriented language, but also with power command interpreter (as an example see my original post). For example, similar to DotNet with /Try ..Catch …Finally/ you can get the Exception object and work with it in VFP.
Therefore I think that VFP belongs to what you call as “modern’ language.
I read up on it a bit, and yes, you can hammer some modern concepts out with it in ways other languages can, but the things I was specifically looking for in terms of strong type checking and compile time code safety, werent there. As an example, i looked for how VFP implements polymorphism, which would be a good indicator of its OOP-enabled state.

I found a few articles that basically indicated that polymorphism of sorts is available in that you can create two classes that have a method of the same name, and the language will allow you to use them indiscriminately, but it doesnt do any compile-time checking that what you put in there is garbage, so it seems more like a syntactical or scripted convenience, evaluated at runtime as to whether a method is called or not. In "from the ground up" OO languages we call this hiding, or overloading (rather than overriding which is the "proper OO way, and allows for compile time checking") but generally you cant do it because the inheritance hierarchy is much more precious

Objects too, such as Exception, have a whole branch hierarchy of their own, and handling is very structured. While it might be possible to replicate the effects of what you see as .NET error handling, I dont know if it's possible to recreate it in the same way.

VFP support is ending, after many evolutions. It certainly seems to have some of the features of a "modern" language as I'm deeming here, but it's not immediately obvious that this is a designed-in process

I think, we can continue to exchange opinions about “modern languages”, but it will be not fruitful. Having no experience in VFP you cannot convince me in your opinion, and I cannot induce you to accept my opinion for the same reason.
Well, to an extent, I disagree. I know what I think constitutes a modern language, and I did look into VFP to see if it contains those elements.. I didnt find exactly what I was looking for, but I'm still open to the notion that I might! If you can demonstrate inheritance, polymorphism, encapsulation, and compile-time type safety checking, proper multithreading, delegation (milestones of what IMHO are 'modern' programming language features) then sure, I can accept that VFP is "modern" but it seems more like VB6; a bit hacked-in or unsafe because it wasnt designed for those things originally

I remain sincerely grateful to you for your direct and detailed answer to my original question.
No problems..
 
Back
Top