C# - Anonymous Method Alternative For Design Pattern?
Apr 18, 2012
With C# anonymous delegates, we can avoid declaration of extra method, I know in VB.NET they're not available, but is there a design pattern that I can use so that I can avoid writing an extra method each time? I need it for SPSecurity.RunWithElevatedPrivileges which will be used a lot of times in our code.
Using VB.Net, I'm looking for best practices in dealing with the following idiom:[code]What I really want is something VB.Net doesn't offer -- additional and simultaneous iterators on For Each statements.[code]I'm interested in both Linq and non-Linq recommendations, comments about Copy method design, comparisions to C# or other languages.
I am working on a parser for a serial data protocol. I have an overarching Packet class, a couple sub-classes such as CommandPacket and StatusPacket, and then a few sub-classes of each of those:
[Code]..
While I don't think it is entirely relelvant to the discussion, I am doing this in VB.NET. There is also a similar (but not quite the same) question posted here: Java - subclass validation design pattern
I've been studying design patterns off-and-on now for a project I've recently been working on. I've been fortunate with this last one not to have to work with databases, but will soon have an opportunity.
I'm trying to replicate a design pattern used in a graphical programming language called LabVIEW. One thread produces commands, and another thread, or series of threads, consumes the commands.This is very similar to using Queues in VB...but there doesn't seem to be an easy way to start the consumer thread when new items are added to the queue.
Here is what I would like to accomplish, please let me know if there is a straightforward way to accomplish this with out of box .Net functionality.1) Start the Consumer Thread...it sits idle, waiting for some new commands to appear in its Queue.2) Producer places a few commands into the Queue. Example: "Home X Axis", "Home Y Axis", "Initialize Pump", "Display Ready Message"3) Consumer thread 'wakes up' at seeing the new commands, then performs the 4 tasks specified in 2.4) Consumer thread goes 'idle' again, and simply waits for new commands to appear in the Queue.
Process can continue on and on forever. The main problem I see with Queues is that there is no 'Wait for Something to Appear in Queue then Retrieve it" functionality. I don't want to use a perpetual loop to keep checking ever n milliseconds.
Public Sub Control1Updated() Handles control1.TextChanged control2.Text = SomeFunction(control1.Text) End Sub
[code]....
Basically the pairs of {control1.Text, control2.Text} and {control2.Text, control1.Text} should be the same. If control1.Text is changed to let's say "a" then control2.Text is ALWAYS "b". If control2.Text is changed to "b" then control1.Text is ALWAYS "a". How do I achieve this with events without going into an infinite loop? [the best I can think of is to make a check if the other control.Text is already the desired value].
In the MVC design pattern we must use a controller(s) classes.
What are the fucntions (features) of the controller apart from handling communication bewteen Model and View?e.eg; can we include input validation code in the controller?
In teh MVC design pattern, Controller is strongly related to the view (GUI). IN my application i need to generated a new BrandID, i used stored procedure to retreive the highest BrandID from teh DB (The model calls the stored procedure and sends the retreival value to the Controller), then in the controller increments the BrandID by "1" to create a new brand ID. SInce, controller is strogly related to "view", should i include this "brandID increment" part in teh controller or in the model classes?
I have the following in C#: public static void StartAnimation(UIElement animatableElement, DependencyProperty dependencyProperty, double toValue, double animationDurationSeconds, EventHandler completedEvent)
There are two ways to create methods on the fly. Through Anonymous method and through Lambda expression (relevant for C# language as far as i know). Though there is not much difference between them. How to create Anonymous methods in VB.NET ?
I have a winform which allows people to edit data from a database, to simplify things assume there is a Customer table in database with 3 fields - Name, City, Country. Through winform(s) people can Add/Edit/Delete customers.For each of these actions we need to save:
What the field names are (Name,City,Country in this case)What the field values were before they were modified What the field values are after they are modified.If the action is Add or Delete then 2 and 3 will be the same.I have already implemented this using XMLSerialisation (but not using any of the design patterns) and my XML output looks like this.
The solution can handle different areas of the system with different number of fields (i.e the same thing works when you are modifying Products for instance).is there a well defined design pattern to deal with this kind of behavior?
having difficulty with understanding object lifetime / instantiation.I have a windows from with a datagridview and a panel. The datagridview has a checkbox column and a list of names which is populated from a database.The panel has a few text boxes (e.g. name, age, favourite sports team) and a 'save changes' button.
The idea is that when the user selects a person in the datagridview by checking the appropriate checkbox then the panel shows the relevant data for that person (name, age, fav sports team) by pulling the data from a database. If the user would like to update the person's details they can do so by typing in the textboxes and then clicking the 'save changes' button. All quite simple so far.
The way I have designed this is to have a 'panel' class and to create an instance when the user checks a checkbox.(NB - the reason I have created a 'panel' class is that I intend to replicate the datagridview and panel on other tabpages within my form and thought it would be useful to have a generic 'panel' class that I could re-use.)
My code looks a bit like this:
If CheckBoxClicked Then Dim UpdatePanel As New UpdatePanel(MyForm.NameTextBox, MyForm.AgeTextBox, MyForm.FavSportTeamTextBox, MyForm.SaveButton) UpdatePanel.GetData() ...
When the user clicks the SaveButton the data is updated to the database and I have a display message (simple textbox) which says "Update successful" and displays for 5 seconds before hiding itself.
Here is the problem:Suppose a user checks a checkbox, reviews the data and does not make any changes, and then checks another checkbox and decides to update the data by clicking the 'save changes' button. What happens is that the "Update successful" message is displayed twice.
I think this is because every time the user checks a checkbox an instance of UpdatePanel is created. If a I select five different people using the checkboxes and then hit 'save changes' I get five "Update successful" messages.
I am not sure how to overcome this. It feels like each time a checkbox is clicked I need to check if an instance of UpdatePanel exists and then destroy it. I tried using UpdatePanel = Nothing and also investigated IDisposable and GC.Collect() but with no luck.
I'm trying to create an extension method that returns an IEqualityComparer based on a lambda function. Heres the extension method:
<Extension()> Public Function Comparer(Of T)(Func As Func(Of T, T, Boolean)) As IEqualityComparer(Of T) Return New GenericComparer(Of T)(Func) End Function
Imports System.Reflection Public Class Test Private Field As String End Class Module Module1 Sub Main() Dim field = GetType(Test).GetField("Field", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)
I found this code below to find the external IP address of the system it is run on, but I was wondering is there another method of finding this information out without the need of using this external website incase it isnt operational anymore.[code]
What would be an cleaner alternative for a code like this.
Dim dAdapter As New SqlCeDataAdapter dAdapter = New SqlCeDataAdapter("SELECT name FROM Users WHERE username='" & GlobalVars.user & "' AND category = '1'", ConnectionString)
Assume you have controls A, B, C, D and E all with a Visibility property. You also have states 1,2,3,4,5 and 6 in which various combinations of your controls would be displayed. Currently this is being handled by switch statements for each state: i.e.
Select Case PageState case "1" a.visible = false b.visible = true
[code]....
As you can imagine, this becomes a pain as every state needs a show/hide statement for each control. How can I refactor this so that adding controls and/or states becomes trivial?My first instinct is to extend the control and add a collection of states that it should display for but this sounds like overkill.
Edit:I was deliberately vague in my question in case this has other implications. In my current instance the "controls" in question are ASP Panels. Does that change anything?
One of the problems that I constantly see with Winforms applications is that the GUI thread is stuck while a long-running task is running or just plain stops refreshing (yes, I know - needs threading). Is there a code template/design pattern I can use when reacting to mouse clicks or button presses? Should there be a processing thread always running in the GUI app? Basically, how do I write a great Winforms Application that is easy to maintain and doesn't have any quirky refresh bugs?
I found that this answer was asked and answered before VS2010 was actually released.[code]to a .NET Framework 4.0 project in Visual Studio 2010 and it does not compile.Do you now if this feature is really implemented and what I am doing wrong?
I am currently using the registry to get file descriptions for files. As an example, so that a DOCX file will return the description "Microsoft Word". I am doing this from the HKEY_CLASSES_ROOT registry key.However, registry access is extremely slow, and I need to do several thousands of these. Is there an alternative way to do this?
The only way I can think of to display this moustache is by creating a new form and locking it to a certain point, that all works.My problem is with all the events surrounding this. E.g. minimizing the actual application etc...I know how to handle those in code but that is painstaking as I have to think of every possible event which would result in a problem then manually code for the moustaches relative actions. So, I would like to know:
I've got an app which uses a background worker to copy files. It works fine but some files are quite large and it "appears" as though the app isn't doing anything. Is there an alternative to the File.Copy method of copying files so I can report on the progress of individual file copies, like the percentage complete?
Let say I have a function inside a class which require 4 parameter (table name,field name,field value, return field) When I use this function my form, such as
myclass.Search("customers", <-- after pressing the "," a popup list will be displayed that contain all the fields in "customers" table.
I have created my webpage in asp.net in 1024*768 resolution, my problem was that when i change my monitor resolution then the controls in my webpage will be displayed in unmanaged manner .
How to arrange items in my webpage which support multiple resolution ...
Whatever the resolution of my monitor the controls in my webpage will display as it is as managed in 1024*768 reolution !