Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Trying to morph some old procedural code to turn it from a dump module to a smart class. I am making some headway but I'm not sure I am going about this the right way. In general here are the steps I have taken thus far:

 

Created a constructor for my new class.

Took most of the parameters for functions and made them private properties so I don't need to pass them around to each method.

My functions now become methods of the class correct?

 

Previously, most of my functions would return a string and that string would be "Success" or ex.Message. This worked for me because I could assess the return value and determine if what happened was what I wanted.

 

My code is transactional in that once it starts to run if something bombs along the way I need to know so that I can rollback and move on to the next instance of my class that I will spawn.

 

I don't know if I'm explaining this correctly (or at all). Basically what I had was an asp.net page that would handle the transactional element by making web service calls that returned the "Success" string. The asp.net page also handled setting up the files to act on. So if the web service returned an error message the asp.net 'front end' would log that error in Oracle and bump over to the next file to work on.

 

I am taking this asp.net code out of there and incorporating it into my new class. This will make the asp.net page a much thinner client and incorporate most of my logic and transactions in the web service.

 

I haven't asked a question yet, right. Well, how would I handle this return value from the function in my class? Should I make a private boolean and set it to true and then if something bombs flip it to false? I need to assess it at each step though?

 

Basically the 2 things driving this conversion are: 1)to get this OOP-friendly for the next client who needs it, and the next.. 2)to remove some burden from the 'front end' page so that piece can be more flexible (i won't just have to be an asp page it could be anything).

 

Am I barking up the right tree here? This OOP stuff can become confusing because I am writing all this code that really doesn't appear to do much on the surface.

Wanna-Be C# Superstar
Posted
Oh, I also wanted to ask if there was some sort of guideline that somebody might have written somewhere about moving code from procedural to OOP. Is this the definition of 'refactoring'? Certainly there must be some doc out there about a general way to go about it?
Wanna-Be C# Superstar
Posted

I thought of one just now:

 

Previously I had a function that accepted 2 parameters and the first thing I did in the function was to test the parameters for null and then give them a default value like so:

 

Function (A as String, B as String) As String
If A = String.Empty Then
 A = "MyDefaultA"
End if
If B = String.Empty Then
 B = "MyDefaultB"
End if

 

Now what I have done is move those parameters into private properties of my class:

 

Property A() As String
       Get
           Return _A
       End Get
       Set(ByVal Value As String)
           If Value = String.Empty Then
               _A = "MYDEFAULTA"
           Else
               _A = Value.ToUpper
           End If
       End Set
   End Property

 

And I set this property first before calling my method.

Forgive me if this is trite and basic but I didn't know this stuff.

I did read that one benefit of classes is that you appear to give the calling code access to these variables but in fact you really do not. The calling code, or client, can try to set these variables to what they want but the CLASS controls what happens to them.

A great benefit of this is that I was having issues querying Oracle because sometimes the strings weren't in the right case and the query would fail even though the strings matched in every way except CaSe. So I had these .ToUpper lines everywhere . No more.

Wanna-Be C# Superstar
Posted
Oh' date=' I also wanted to ask if there was some sort of guideline that somebody might have written somewhere about moving code from procedural to OOP. Is this the definition of 'refactoring'? Certainly there must be some doc out there about a general way to go about it?[/quote']

There is like a master book on refactoring which breaks it down into a bit by bit process.

 

I wouldn't think Refactoring's main purpose is Procedural -> OOP so much as streamlining and simplifying... which would most likely be OOP.

 

The point is to make code that is easier to read and update. More modular.

 

http://www.refactoring.com/ is a webpage devoted to it. I havn't had a chance to read my Refactoring book. It keeps being "Next" on my list along with Design Patterns, but other things keep comming up... like projects and the MCAD/MCSD stuff.

 

The one thing I do remember about in any detail from my scan of the first chapter was that you need to do things in "baby steps". Do a small thing, save and test. Wish I could be more help

Posted

A great benefit of this is that I was having issues querying Oracle because sometimes the strings weren't in the right case and the query would fail even though the strings matched in every way except CaSe. So I had these .ToUpper lines everywhere . No more.

exactly, the Data interfaces the app appropriately, instead of the app interfaces the data appropriately.

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

I hit a bit of a snag when trying to get an oracle connection up

 

Private _OracleConn As New OracleConnection(ConfigurationSettings.AppSettings("ConnectionString"))

ReadOnly Property OracleConn() As OracleConnection
       Get
           Return _OracleConn
       End Get
   End Property

Dim strTypeSQL As String = "SELECT FILETYPE FROM MyTable"
       Dim objTypeCmd As New OracleCommand(strTypeSQL, OracleConn)
       Try
           OracleConn.Open()
           FileType = CType(objTypeCmd.ExecuteScalar, String)
           OracleConn.Close()
           If FileType = String.Empty Then
               Me.ToText("No filetype exists")
           End If
       Catch ex As Exception
           Me.ToText(ex.Message)
       Finally
           OracleConn.Close()
       End Try

 

I get ye olde 'object ref not set to an instance' error

 

In my procedural code I had :

 

 Dim OracleConn As New OracleConnection(ConfigurationSettings.AppSettings("ConnectionString"))

 

At the top of the module and I liked it because I didn't need to write that everywhere I wanted to use the connection.

 

HappY Birthday Joe Mamma:)

Wanna-Be C# Superstar
  • *Experts*
Posted

I would definitely read the Refactoring book my Martin Fowler. There is only a brief section near the end on moving a large chunk of procedural code and I personally didn't find that section that helpful. But, I did recently refactor a very large chunk of "black box" code into a handful of classes and the design is MUCH easier to read, change, etc.

 

If you read the book, the first thing you'll notice is that you always refactor in small steps and test a lot inbetween to make sure you're not breaking anything. As an example, my blackbox code needed a number of changes to fix some bugs. It was about a week of changes before I was ready to make any changes.

 

First off, my starting class was 4682 lines long. Yep, a big one. One public method to calculate fees based on a DataSet.

 

I did the following, which worked for me:

1. Remove many if not all temp variables from the various functions. This took a day or two to replace them with equivalent named methods.

2. Break up existing code into methods. Many of the existing methods were large, one was about 400 lines and many (10+) were 100-200 lines long.

3. Move some of the "common" functions to a utility class.

 

On day 3 or 4 I started reviewing the now hundreds of methods and deciding what classes I could use and what methods or properties they needed to expose. That involved mostly creating a half dozen or dozen classes and moving my new methods (that were temp variables) into the class.

 

Finally, I did some more advanced OOP like moving stuff out of the DataSet and into full-fledged classes. The previous design had done everything off the DataSet which was a bad idea. It was very easy to use the DataSet for everything but looking back, it was like having a TON of global variables since all the code had access to the dataset. I could never be sure who (which piece of code) was changing what (what tables/rows/fields).

 

The new design requires a lot less comments on the basic flow of the program. The only comments are in the Why's (from a spec) and not the "What this code is doing". The code is now infinitely more clear as to what it's doing. Also, by moving code into classes I can easily see who's changing what values.

 

A final step, which happened after I did the main refactoring was to go back and move some methods around where they made more sense. I also made a lot of the properties private and exposed the needed functionality through methods.

 

For example, rather than have ClassA use a property of ClassB for some calculation, it often made more sense to have ClassB expose a method to do the calculation itself and ClassA just used the result.

 

They key is to take small steps, make sure you're not breaking anything, and don't worry so much about what's Right (big R). Refactoring is all about not having to worry about getting it right the first time, if you don't know what Right is. If you have a bunch of procedural code, refactor in small bits and eventually you'll start to "see" where the classes/methods are.

 

It's hard to explain, but if you start trying it you'll see.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

I'm following you on that. It is starting to make more sense the more I stare at it. I like the idea of a utility class to do remedial chores. I'm working that one in.

 

I figured out the issue with the Oracle connection was not how I was creating it, that part worked (not sure if its the best way). It was the return value that was set to nothing because the query didn't return any results. I need to plan for that. Guess I could set it to default to something when there are no query results?

Wanna-Be C# Superstar
Posted

So now I have

Property FileType() As String
       Get
           Return _FileType
       End Get
       Set(ByVal Value As String)
           If Value Is Nothing Then
               booSuccess = False
               _FileType = String.Empty
           Else
               _FileType = Value.ToUpper
           End If

       End Set
   End Property

 

And booSuccess is sort of my 'global' transaction flag. It starts as true and if anything goes wrong it gets flipped to false.

but this means I need to be testing it over and over before I move to another part of my code. But I guess that is what I want so that is what I have to do. Unless, I can put in the Set method of the booSuccess property something that would fire if it were flipped to false at any point.

 

Hummm.

Wanna-Be C# Superstar
Posted
So now I have

Property FileType() As String
Get
Return _FileType
End Get
Set(ByVal Value As String)
If Value Is Nothing Then
booSuccess = False
_FileType = String.Empty
Else
_FileType = Value.ToUpper
End If

End Set
End Property

 

And booSuccess is sort of my 'global' transaction flag. It starts as true and if anything goes wrong it gets flipped to false.

but this means I need to be testing it over and over before I move to another part of my code. But I guess that is what I want so that is what I have to do. Unless, I can put in the Set method of the booSuccess property something that would fire if it were flipped to false at any point.

 

Hummm.

create an event called booSuccess_Changed (or booSuccess_False) or something in the class. Raise the event whenever you set it to false. Add a handler for the event on the procedure you want to handle the event.

Posted

Only one question. In my OOP class they told me that the advantage of OOP is that it was easy to read... easy to maintain (read update) and that it can be easily modulable.

 

So... isn't the goal of all this to transform from procedural to OOP ? Since OOP offer all those advantage ? Yes ? No ? Toaster ?

 

Maybe I'm wrong... Maybe I've smoked too much of the grass in front of my home...:-\

"If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown

"Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me

"A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend.

C# TO VB TRANSLATOR

Posted
Only one question. In my OOP class they told me that the advantage of OOP is that it was easy to read... easy to maintain (read update) and that it can be easily modulable.

 

So... isn't the goal of all this to transform from procedural to OOP ? Since OOP offer all those advantage ? Yes ? No ? Toaster ?

 

Maybe I'm wrong... Maybe I've smoked too much of the grass in front of my home...:-\

If done properly (like using Design Patterns, even making up your own Patterns) OOP will render code more readable without even looking at the code. Looking at the pattern would help someone figure out what does what.

 

Yes, OOP should help make things much more clear than procedural.

 

As far as I know there are degrees of OOP, and in some cases the most "Correct" and "Elegant" OOP design might be a flop performance wise.

Posted

Denaes,

 

Do you think you could supply me with a short sample template to create this event for booSuccess_Changed?

 

I think what I have to do is create the handler routine and then whenever I call a procedure that has any chance of changing the value I then add some code that recognizes when the event is raised?

 

Does this involve a delegate at all?

Wanna-Be C# Superstar
Posted

Hope this is what you're looking for.

 

The class in which the event is created and triggered:

 

[size=2][/size][size=2][color=#008000]' Standard declaration. Public makes it available to an outside class

[/color][/size][size=2][/size][size=2][color=#008000]' which instantiates this class "WithEvents". 

[/color][/size][size=2][/size][size=2][color=#008000]' Funny, even in VB this looks like a C# declaration.

[/color][/size][size=2][/size][size=2][color=#0000ff]Public[/color][/size][size=2] [/size][size=2][color=#0000ff]Event[/color][/size][size=2] BoolChanged([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] Bool [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Boolean[/color][/size][size=2])

[/size][size=2][color=#008000]' Private member variable. 

[/color][/size][size=2][/size][size=2][color=#0000ff]Private[/color][/size][size=2] mBool [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Boolean

[/color][/size][size=2][/size][size=2][color=#0000ff]Public[/color][/size][size=2] [/size][size=2][color=#0000ff]Property[/color][/size][size=2] Bool() [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Boolean

[/color][/size][size=2][/size][size=2][color=#0000ff]Get

[/color][/size][size=2][/size][size=2][color=#0000ff]Return[/color][/size][size=2] mBool

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Get

[/color][/size][size=2][/size][size=2][color=#0000ff]Set[/color][/size][size=2]([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] Value [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Boolean[/color][/size][size=2])

[/size][size=2][color=#008000]' only change the variable and raise

[/color][/size][size=2][/size][size=2][color=#008000]' the event if mBool and the new value

[/color][/size][size=2][/size][size=2][color=#008000]' are different

[/color][/size][size=2][/size][size=2][color=#0000ff]If[/color][/size][size=2] mBool <> Value [/size][size=2][color=#0000ff]Then

[/color][/size][size=2]mBool = Value

[/size][size=2][color=#0000ff]RaiseEvent[/color][/size][size=2] BoolChanged(mBool)

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Set

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Property

[/color][/size]

 

I don't know why, I'm Copy & Pasting the code, but it seems to have lost it's indentation and everything.

 

Relevent code in Form1, the class which receives the delegate/event and the procedure raised when the event is raised

 

[size=2][color=#0000ff]Dim[/color][/size][size=2] [/size][size=2][color=#0000ff]WithEvents[/color][/size][size=2] boolEventTest [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]New[/color][/size][size=2] TestEvent



[/size][size=2][color=#0000ff]Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] ComboBox1_SelectedIndexChanged([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] ComboBox1.SelectedIndexChanged[/size]
[size=2]'  This is just a combobox changing the Bool property on the class. 
boolEventTest.Bool = [/size][size=2][color=#0000ff]CType[/color][/size][size=2](ComboBox1.GetItemText(ComboBox1.SelectedItem), [/size][size=2][color=#0000ff]Boolean[/color][/size][size=2])

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub

[/color][/size][size=2][/size][size=2][color=#0000ff]Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] Bool_Changed([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Boolean[/color][/size][size=2])

MessageBox.Show("Bool has changed to: " & e.ToString)

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub

[/color][/size][size=2][/size][size=2][color=#0000ff]Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] Form1_Load([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] [/size][size=2][color=#0000ff]MyBase[/color][/size][size=2].Load

[/size][size=2][color=#0000ff]AddHandler[/color][/size][size=2] boolEventTest.BoolChanged, [/size][size=2][color=#0000ff]AddressOf[/color][/size][size=2] Bool_Changed

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub

[/color][/size]

 

Also provided the project in case you wanted to open, run and view it in it's whole.

 

Hope that helps some.

TestEvents.zip

Posted

Very Nice. Thank you much. Sorry about the multiple posts. I just thought it had diverged enough from this topic.

 

I learned much from your code and got it up and running in my app. I guess I was most confused about where to put everything.

And in terms of 'how .NET recognizes the event' (which was tripping me up before) well it recognizes it boo has changed because you tell it. that part is obvious now.

 

I still have a few more tiny questions and I will be set. Sorry. This stuff is really confusing for me for some reason.

 

My implementation of this code is in a web service. My web service has a web method that is sort of the 'calling code'. It instantiates an instance of a class that I have. So i don't have a form load. I put the AddHandler line in my web method like so:

 

Dim WithEvents aTrans As CADTrans

   Private Sub Bool_Changed(ByVal e As Boolean)
       ToText("changed boo to " & e.ToString)'writes to a text file
   End Sub

   <WebMethod(Description:="Accepts a dgn path")> _
   Public Function Translate(ByVal aPath As String) As Boolean

       aTrans = new CADTrans(aPath)
       AddHandler aTrans.BoolChanged, AddressOf Bool_Changed

       aTrans.booSuccess = False 'for testing

       Translate = aTrans.booSuccess
       Return Translate

   End Function

 

That seemed to work. Then in my CADTrans class I have:

 

Public Event BoolChanged(ByVal booSuccess As Boolean)

   Private _booSuccess As Boolean
   Public Property booSuccess() As Boolean
       Get
           Return _booSuccess
       End Get
       Set(ByVal value As Boolean)

           If _booSuccess = False Then
               RaiseEvent BoolChanged(_booSuccess)
           End If
       End Set
   End Property

 

This seems to get the job done as it writes out to a text file. You don't have too many forms of output for a web method, so i'm using that to test.

 

So my question is: Is that all I need to do? I mean if I call any method of my class and it happens to change the value of booSuccess then this event will be triggered? If that is true then I see the power of using these kinds of delegates. I was afraid I would need to slap this handler all over the place (because I thought it went in the class method). I guess I still need lines of code in each method to see if booSuccess is true each time a method is run.

 

The only other thing I was wondering about is this: If I am really going to be able to use this construct as a 'global transaction flag' then I am going to need to know where the code blew up when it did. How do I trap for the name of the method that caused booSuccess to be false?

 

Thanks for all of your help and being patient. That zip was very helpful. I printed that one out for the wall mount. :)

Wanna-Be C# Superstar
Posted
I guess I was most confused about where to put everything.

And in terms of 'how .NET recognizes the event'

Not a whole lot involved in making handlers in VB.Net, and it can be very powerful and clean up your code considerably more than having procedure calls all over with if...Then or case statements directing them. In a large app, that could take up 100's of lines of code.

 

My implementation of this code is in a web service. My web service has a web method that is sort of the 'calling code'. It instantiates an instance of a class that I have. So i don't have a form load. I put the AddHandler line in my web method like so:
It doesn't matter where "AddHandler boolEventTest.BoolChanged, AddressOf Bool_Changed" is located. In fact I'm fairly certain that there are even other ways of doing this.

 

What you need to walk away knowing is that this line of code is what adds the Handler from myClass.BoolChanged to your procedure.

 

It doesn't matter if it's in Form_Load, a button, a mouseover, etc.

 

What does matter is that a handler won't be attached to your procedure (in this case Bool_Changed) until this code is fired.

 

Bool_Changed is just a normal procedure with the argument of a Boolean. As such you can call it or use it elsewere.

 

I'm sorry I can't really help you out with Web services specifically, thats after Win programming on my MCAD list.

 

 

So my question is: Is that all I need to do? I mean if I call any method of my class and it happens to change the value of booSuccess then this event will be triggered?
Almost. the code I used would trigger whenever booSuccess changed.

 

The code you used would only trigger whenever booSuccess was False and could continue to trigger if something like a loop kept setting it to false.

 

If BooSuccess <> Value

BooSuccess = Value
[color=#0000ff]RaiseEvent[/color][size=2] BoolChanged(BooSuccess)[/size]

End If

 

This will only result in the event triggering if the value is changed (true becomming false; false becomming true)

 

The only other thing I was wondering about is this: If I am really going to be able to use this construct as a 'global transaction flag' then I am going to need to know where the code blew up when it did. How do I trap for the name of the method that caused booSuccess to be false?Instead of "Public Property BooSuccess as Boolean" Try this:

 

Data Type boolArgs created to pass multiple arguments

[size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Class[/color][/size][size=2] BoolArgs

[/size][size=2][color=#008000]'private variables

[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2] mBool [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Boolean

[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2] mSender [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String

[/color][/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Property[/color][/size][size=2] BooleanResult() [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Boolean

[/color][/size][size=2][color=#0000ff]Get

[/color][/size][size=2][color=#0000ff]Return[/color][/size][size=2] mBool

[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Get

[/color][/size][size=2][color=#0000ff]Set[/color][/size][size=2]([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] Value [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Boolean[/color][/size][size=2])

mBool = Value

[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Set

[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Property

[/color][/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Property[/color][/size][size=2] Sender() [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String

[/color][/size][size=2][color=#0000ff]Get

[/color][/size][size=2][color=#0000ff]Return[/color][/size][size=2] mSender

[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Get

[/color][/size][size=2][color=#0000ff]Set[/color][/size][size=2]([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] Value [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2])

mSender = Value

[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Set

[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Property

End[/color][/size][size=2][color=#0000ff]Class

[/color][/size]

 

Altered get/set properties.

I split it into a get Property and a Set property.

Get returns just the boolean variable.

 

Set passes the BoolArgs to provide information for the event.

 

[size=2][color=#008000]' Standard declaration. Public makes it available to an outside class

[/color][/size][size=2][color=#008000]' which instantiates this class "WithEvents". 

[/color][/size][size=2][color=#008000]' Funny, even in VB this looks like a C# declaration.

[/color][/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Event[/color][/size][size=2] BoolChanged([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] BoolArg [/size][size=2][color=#0000ff]As[/color][/size][size=2] BoolArgs)

[/size][size=2][color=#008000]' Private member variable. 

[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2] mBool [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Boolean

[/color][/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]WriteOnly[/color][/size][size=2][color=#0000ff]Property[/color][/size][size=2] setBool() [/size][size=2][color=#0000ff]As[/color][/size][size=2] BoolArgs

[/size][size=2][color=#0000ff]Set[/color][/size][size=2]([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] Value [/size][size=2][color=#0000ff]As[/color][/size][size=2] BoolArgs)

[/size][size=2][color=#008000]' only change the variable and raise

[/color][/size][size=2][color=#008000]' the event if mBool and the new value

[/color][/size][size=2][color=#008000]' are different

[/color][/size][size=2][color=#0000ff]If[/color][/size][size=2] mBool <> Value.BooleanResult [/size][size=2][color=#0000ff]Then

[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] tempArgs [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] BoolArgs

mBool = Value.BooleanResult

[/size][size=2][color=#0000ff]With[/color][/size][size=2] tempArgs

.BooleanResult = Value.BooleanResult

.Sender = Value.Sender

[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]With

[/color][/size][size=2][color=#0000ff]RaiseEvent[/color][/size][size=2] BoolChanged(tempArgs)

[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If

[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Set

[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Property

[/color][/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]ReadOnly[/color][/size][size=2][color=#0000ff]Property[/color][/size][size=2] getBool() [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Boolean

[/color][/size][size=2][color=#0000ff]Get

[/color][/size][size=2][color=#0000ff]Return[/color][/size][size=2] mBool

[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Get

[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Property

[/color][/size]

 

I also attached a new Zip of whats happening with the BoolArgs and how to use setBoolSuccess to differentiate what caused it.

 

Remember, on the BoolArgs Class, you can provide whatever information you think you'll need and in whatever method you desire.

 

You don't always have to use it, but you should always set it. This is like the Args in a button_Click event. How often do you use those args? Not very. But the Args are always there and set in case you want to use them.

 

Instead of Sender being a string, it can be the reference to the sending class as an object, you could include all sorts of information. A timestamp for when it's set to False (as a string or Date), a Notes as string to pass along information... whatever you want.

 

Just make sure you fill out all that information when you change boolSuccess (and thus raise the event), and in the setBoolSuccess pass all those parameters through the event.

TestEvents complex arguments.zip

Posted

Awesome. Thank you so much for taking the time to help me out. This is going to be a tremendous help for my coding.

This forum is the best because there are folks like you that take the time to help people. Hoepfully, when i get a better understanding of all of this I too will be able to give back by helping someone else.

This would be a good topic for a code tutorial.

Wanna-Be C# Superstar
Posted
Awesome. Thank you so much for taking the time to help me out. This is going to be a tremendous help for my coding.

This forum is the best because there are folks like you that take the time to help people. Hoepfully, when i get a better understanding of all of this I too will be able to give back by helping someone else.

This would be a good topic for a code tutorial.

No problem.

 

I've been comming here since literally my first days of VB.Net programming asking questions like "I did this in VB6, how do I do it in .Net". I enjoy helping others when I get the chance and hope you do the same when you get the chance :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...