What's wrong with this VB code?
If test Then
do stuff
#If Not Debug Then
Else
#End If
do other stuff
End If
What's wrong with this VB code?
If test Then
do stuff
#If Not Debug Then
Else
#End If
do other stuff
End If
I wrote an extension method for this not too long ago.
<System.Runtime.CompilerServices.Extension()> _
Public Function GetColumnAsString(ByVal rdr As System.Data.IDataReader, ByVal columnName As String, Optional ByVal valueIfNull As String = "") As String
Dim obj As Object
If rdr Is Nothing Then
Return valueIfNull
Else
obj = rdr(columnName)
If obj IsNot Nothing AndAlso obj.Equals(DBNull.Value) = False Then
Return obj.ToString()
Else
Return valueIfNull
End If
End If
End Function
Note: it does not check for make sure the column specified exists. Using your example it would be
Response.Write(DR.GetColumnAsString("TerminatedDate", "%nbsp;"))
You could also embed it for more of a coalesce functionality.
Response.Write(DR.GetColumnAsString("TerminatedDate", DR.GetColumnAsString("SomeOtherDate", "%nbsp;"))
I think the real problem with this method is that InnerException is not a collection. You may be able to use a converter to convert InnerException to an array of exception objects. However, there is a much easier method. Here is a simple sample Window that does something similar to what I think you want. XAML
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type s:Exception}">
<Expander Margin="20,0,0,0" Header="{Binding Message}" Content="{Binding InnerException}"/>
</DataTemplate>
</Window.Resources>
<DockPanel>
<Button DockPanel.Dock="Bottom" Click="Button_Click">Error!</Button>
<ContentPresenter Name="Bob" />
</DockPanel>
</Window>
CODE (Pardon the VB)
Class MainWindow
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Try
a()
Catch ex As Exception
Me.Bob.Content = ex
End Try
End Sub
Private Sub a()
Try
b()
Catch ex As Exception
Throw New ArgumentException("WOW!", ex)
End Try
End Sub
Private Sub b()
Try
c()
Catch ex As Exception
Throw New InvalidCastException("no good", ex)
End Try
End Sub
Private Sub c()
Throw New InvalidOperationException("Bad magic")
End Sub
End Class
I use WPF's awesomeness with DataTemplates. Any exception object it displays will look like an Expander.
I think your problem is that .
is greedy. You should make it lazy with ?
. (Reference[^]) This <a(.)*?href=(\"|')(?<url>(.)*?)(\"|')(.)*?>(?<text>(.)+?)</a>
should work. Although it is untested. Lazy lookup is less efficient than regular lookup so you may want to use something more appropriate like (?<url>[^'"]*)
. I have found http://www.regular-expressions.info/[^] to be a great resource.
The last one is easy
SomeControl.ListOpened += new EventHandler(SomeControl_ListOpened);
converts to
AddHandler SomeControl.ListOpened, Addressof SomeControl_ListOpened
The other two are rather tricky. They use lambda functions which VB doesn't really support. Essentially
SomeControl.SiteReached += (s, ea) => miBack.Enabled = false;
Says, "Add a new handler for SiteReached
that takes two parameters (s and ea). This function will set miBack.Enabled = False
." Making the Sub in VB is rather easy
Sub SomeControl_SiteReached(s as Sender, ea as System.EventArgs)
The hard part is duplicating the middle. If miBack
is global or accessible from SomeControl_SiteReached
then you are fine. If miBack
is local to ManuItem1_Click
, then you have a problem. You will have to find some way to change the Enabled
state for miBack
.
I think that would be the way to go. I would also declare commandC in classA as abstract and force an override. Alternative: If you have a lot of commands that would have to overridden, you would want to use interface inheritance. -interfaceX would not have the definition for commandC. -You could then create interfaceY that inherits interfaceX and define commandC. -Classes B-D would inherit classA and implement interfaceY. -In classX you would call interfaceY. The only problem with your method: If you later change the definition of interfaceX you would have to change all the classes (classA would have to be modified even though there is no real change). This would also be true if you wanted to add a method. It all depends on the size of your classes/interface and how often they will change.
1. Name the button something line "PART_Button"
. 2. Then override OnApplyTemplate()
. 3. In OnApplyTemplate
use GetTemplateChild("PART_Button")
. That will get you a reference to the button to add your event handler. Of course you will want to check the returned object to make sure is is not null. Someone may change the template for your control. ;)
I would say that is not necessarily true. It depends on what you mean by 'working'. You could be connected but have DNS issues that would prevent a successful ping. I only say that because I did have DNS issues and ran into this often. :(
Sorry, I didn't verify that. I thought that was the case but I suppose that is left over from working on an Access application :( . Which I know behaves that way. However, I do try to check all variables before using them, even when they are "guaranteed" to be instantiated.
You should also check tvXmlTree.SelectedNode to make sure something is selected. If you deselect a node then this event will fire and your code will fail.
Well... I didn't verify that it would or would not work. I thought "why would it not?" The message about the application not handling window messages is ridiculous. Every window in Windows must send and receive Windows messages on some level. That is the way Windows works. I also tried using SendWait like the error suggests but that doesn't work. The only thing I can think of is to have some class with a static/shared event and method that raises that event. Then your window that needs the refresh could register to that event and the class that needs to send the refresh could call the method that sends the event. I hope that is not too confusing. It doesn't seem like a great solution but it will work.
There is a SendKeys in .Net http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx[^] But, I would recommend you change your structure so you don't need to send key strokes to your own application.
I would use something like this...
private void CreateScreen(string FullName, params object\[\] args)
{
Type t ;
Form f;
t = Type.GetType(FullName);
if (t != null) {
f = Activator.CreateInstance(t, args) as Form;
if (f != null) {
f.Show();
}
}
}
This would also allow you to pass arguments to the constructor. You may also have to pass the Full name of the Form including the Namespace. I don't remember how Type.GetType() works. Edit: Sorry, forgot I was in C#.
modified on Monday, February 8, 2010 11:02 AM