C# - .NET - When To Use The Using Statement
Mar 20, 2011
Dim x As New Serialization.XmlSerializer( ... )
Using file As New FileStream(myFile, FileMode.Create)
Using writer As XmlWriter = XmlTextWriter.Create(file)
[code]....
I've read that you're only supposed to use a Using block on objects that have a .Dispose() (i.e. implements IDisposable), which is why I'm thinking there shouldn't be a Using on "writer", but instead a writer.Close() at the end. But "file" has both a .Dispose() and a .Close(), so which do I use? The Using or a file.Close()?
Note: I'm using an XmlWriter because I can customize the output. I removed the settings here, though.
View 5 Replies
ADVERTISEMENT
Oct 14, 2009
See
Public Class Form1
Dim sql As String
Dim conn As New OleDb.OleDbConnection
Dim da As New OleDb.OleDbDataAdapter
[CODE]...
The problem, The INSERT statement works fine in the form load but not in the button click event?
View 29 Replies
Jan 1, 2010
I was convinced that If <expression> Then <statement [:statement]> Else [statements] in concrete form of If a = b Then SayHello() Else SayBye() End has sense. I read article on msdn on If-then-else, but I forgot why I was reading, so I concluded, that snippet above means this
If a = b Then HelloIsSaid : IsNotEnded Else ByeIsSaid : IsEnded But I have tested it now, and I see, that Else without statement is nothing more than decoration. It would be pretty good if it had function I described. Do you think its good request? Or do you know any circumstance where this Else has some function?
View 13 Replies
Jun 8, 2009
y friend and I are re-learning Visual Basic, and we are stumped on this bit of code.
For intAsterisks As Integer = 0 To intLine - 1
lblAsterisks.Text = lblAsterisks.Text + "*"
Next
View 6 Replies
Jun 24, 2011
I have the following SQL:
[Code]...
I want to put it (the select count statement) in this LINQ statement so I can get the sales count in my linq statement: Dim TheLeads = (From L In DB.Leads Where L.IsDeleted = False Select L).ToList() Is this possible to do in LINQ?
View 1 Replies
Feb 23, 2009
add an if statement and an exit statement to my do loop that exits when my future value (FV) is greater than 1000, then to change the exit statement to a continue statement so my loop will continue even though my fv is greater then 1000, point is to get this to run even though my if statement doesnt do anything. problem something wrong in my code and an exception error (xception of type 'System.OverflowException' occurred in mscorlib.dll)
so can someone show me where or why I have an error is, what am I overthinking now! I could use a hint, OMG i could use a tutor for that matter
[Code]...
View 3 Replies
Jun 23, 2010
A co-worker wants to convert the following IfThenElseIf statement into a Select Case statement.
Explain why this is not possible.
If temperature = 100 Then
X = 0
ElseIf population > 1000 Then
X = 1
ElseIf rate < .1 Then
X = -1
End If
View 7 Replies
Aug 4, 2010
I cant get this SQL statement to insert any of the values in the form to add to the database
sql
sql = "INSERT INTO prevresults (ExamDate, ExamTime, CorrectAnswers, PassPercentage) VALUES (date.now, time.now, rightanswers, percentage.text)"
[code]....
View 4 Replies
May 24, 2012
Return statement which is true in an if statement
View 3 Replies
Apr 22, 2011
I'm writing something that will examine a function and rewrite that function in another language so basically if inside my function F1, i have this line of code var x=a.b(1) how do i break up the function body into symbols or "tokens"?I've searched around and thought that stuff in System.Reflection.MethodInfo.GetMethodBody would do the trick however that class doesn't seem to be able to have the capabilities to do what i want..dit 2:basically what I'm trying to do is to write a program in c#/vb and when i hit F5 a serializer function will (use reflection and) take the entire program (all the classes in that program) and serialize it into a single javascript file. of course javascript doesn't have the .net library so basically the C#/VB program will limit its use of classes to the .js library (which is a library written in c#/vb emulating the framework of javascript objects)
View 4 Replies
Oct 5, 2009
how to use them then before, but am still a little confused with a few aspects. Here goes:
1.) Lets say you have a method that checks for a certain condition(s) and if it fails Throws an exception. Does it have to be in a try/catch block? Meaning can the "Throw" statement exist in a block with no try/catch statement?
2.) Now lets say we have a method that has a try catch block and in it there is a throw statement. When the throw statement is executed does it first try to find an appropriate catch block in the same method or does it immediately go back to the calling method without looking at the catch statements in the current method where the exception was thrown?
3.) I created a custom exception class that inherits from ApplicationException. Next I created a method which has a catch block that catches this type of exception and does some action. Is the System(i.e CLR) smart enough to throw an exception of this type, or does it only throw exceptions from SystemException?
4.) I know that some people are more liberal in their use of exceptions and others use it more sparingly when truly strange stuff happen like the DB going down. I am writing code where I am getting some info back from the database, converting it and then storing it. Sometimes there might be data that comes back from the database and other times the field is empty and the Null value comes back. Therefore in the instances where Null comes back from the database I should not convert the value to anything, since I will get an error. What should I do in this situation? Should I let the CLR throw the exception when it goes to convert the Null value or should I check for the Null value and if it exists not convert?
5.) In general when I throw exceptions, is it sensible to only throw exceptions of the type Application Exception or are there instances where the programmer throws exceptions of the type SystemException?
View 2 Replies
Jul 11, 2010
I'm building a listview search feature in my app. (See the code below). It works really well, except if you search "T" then the code removes all items, if you search "t" then it removes every item except for those starting with "t". Is there any way to make a 'like' statement, or use some kind of wildcard to make the statement not case sensitive?
For Each itm As ListViewItem In ListView2.Items
If itm.Text Like TextBox1.Text & "*" Or itm.SubItems.Item(1).Text Like TextBox1.Text & "*" Then
Else
[code].....
View 2 Replies
May 2, 2012
I have a line of code that is working in one statement but not the second statement. I'm not sure what I'm doing wrong, I am learning slowly but surely, and it is by no small margin because of the ave found here =) The error is occuring in the intResult = intSelection x intCount line of the SECOND Do While Loop.
The blue squiggle line is under "intCount" just FYI
Dim intSelection As Integer
Dim intCount As Integer = 0
[code]....
View 1 Replies
Jan 29, 2009
I have the following vb code which works as expected when it's in a code-behind page. I tried to move it to a shared or component page since I will need to use it in multiple pages:
Imports Microsoft.VisualBasic
Public
Class DetTime
'Determine eastern time
[CODE]...
However the "if" statement shows an error " statement cannot appear outside of a method body" and the Microsoft solution is to "move statement into a procedure or subroutine"
View 1 Replies
Oct 31, 2011
I have coded a case statement that changes the background colour of a panel. [code]When the code is run, it works for the case of colour1 that is NOT 1 or 2. However, when the colour1 is 1 or 2, it is supposed to pick a number from set array and run another case statement. Instead, it only picks the last value (for 1, this is 5, and for 2, this is 7).
View 3 Replies
Mar 17, 2010
Where should i put the Using statement in Visual Basic 2008 Express Edition? as i put in under general declaration, it said "statement cannot appear outside of a method body"
View 1 Replies
Jan 11, 2012
I have a table named "Admin(a)"And i have a SQL statement:
SELECT *FROM Admin(a);
But (a) gives me a error.How should i express Admin(a) in a sql statement?
View 2 Replies
Aug 14, 2011
I am new to VB8 and I cannot get the IF statement right.I think I am not getting the Dim correct.I have posted before with an IF problem and this program is ok until the last IF.
[Code]...
View 12 Replies
Jun 2, 2011
I am using Visual Studio 2005 and am unable to use the "Using" statement. I have just moved some code over from a .net 4 application, but that does not seem to be the problem as it is still not usable in a new app.
[Code]...
View 2 Replies
Feb 28, 2012
Equivalence of "With...End With" in c#?So i don't know if this question has been asked before and I am not a big fan of vb.net. But one syntax that I am missing in C# is the With syntax.[code]Is there a syntax in C# that I have missed that does the same thing?
View 7 Replies
Aug 28, 2009
I am using SQL 2005 on a Windows 2003 server. I have a database with a table called "Courses". In Courses, I have a Column called "UserChar2". In UserChar2, if left blank, does not show an image; if the letter Y is in UserChar2, it displays an image (1.gif) as the course being certified. This currently works in an XML file, the code from the XML is (##if:eq,rowquery_UserChar2,"Y","<span style="position: relative; z-index: 123; top: -40px; left: -90px; width: 80px; height: 66px; background: url(/brokered/images/1.gif) no-repeat;")
I would like to have this work in my ASP page. The code I have currently in the ASP page is (strSQLB = "SELECT Courses.UserChar2 FROM Courses WHERE Courses.UserChar2 = CASE WHEN Courses.UserChar2 = 'Y' THEN 'background: url(/1.gif) no-repeat;' END and (Courses.CampusIdentifier = '0')").
The ASP page also pulls information from the Courses table with this statement (strSQL = "SELECT Courses.CourseCode, Courses.CourseName, Courses.Course_ID, Courses.CEU, Courses.Course_Description, Courses.CertificationType, Courses.UserChar1, Courses.Notes, OrderPricing.ProductCode, OrderPricing.CampusID, OrderPricing.GroupID, OrderPricing.ProductName,
[code]....
What I would like is some help with being able to display the image using the (<% BoRs.Fields("UserChar2") %> ) code.
View 6 Replies
Jan 13, 2011
My understanding of the form of the IIF function is: MyVariable = IIF(logic test, value if true, value if false) I ran across some code that I can't understand:
bWeightOdd = IIf(bLen Mod 2, 2, 1)
The logic test in here has me stumped: bLen Mod 2 where bWeightOdd and bLen have been dimensioned as type Byte. wouldn't bLen Mod 2 just resolve to a numeric value?? I don't see a test here, merely a calculation. I don't understand how this resolves to a True or False.
View 17 Replies
Nov 15, 2010
Now i am doing a project in vb.net, in which i have to verify a particular character is between "A" to "Z" or "a" to "z".
I wrote the vb.net code like below.
if Mid(Trim(str), k , 1) between chr(65)
to chr(90)
or Mid(Trim(str), k, 1) between chr(97)
and chr(122)
But system is showing an error message as, ")" expected.
whether "BETWEEN" statement is a valid statement in vb.net or not
View 1 Replies
Oct 18, 2011
To convert the following Visual Basic statement into a PHP equivalent:
CODE:
View 4 Replies
Apr 24, 2009
I keep getting this error in my code: " end of statement expected".[code]Please use code tags when posting your code.
View 8 Replies
Sep 6, 2011
I have this piece of code which is calling some functions in a web service. however the original was written in VB and when I have converted it it gives me an error in my c# stating that 'The name Information does not exist in the current context' I have checked the VB and it win there either!
[Code]...
View 4 Replies
Sep 3, 2010
How would you create an extension method which enables me to do the following (warning: exteme pseudo-code)...
class FooBar
{
Int32 Foo { get; set; }
String Bar { get; set; }
}
new FooBar().With(fb => new Func<FooBar, Object>(instance =>
[Code]...
View 5 Replies
Sep 29, 2011
I'm trying to make this cleaner... I know there's lots of shortcuts for If statements, but I don't know what's best.
If item.RecurrenceId > 0 Then
xmlTextWriter.WriteElementString("recordtype", "2")
ElseIf IsNothing(item.OngoingEndDate) = False Then
xmlTextWriter.WriteElementString("recordtype", "1")
Else
xmlTextWriter.WriteElementString("recordtype", "0")
End If
View 5 Replies
Sep 29, 2009
In VB.NET there is the WITH command that lets you omit an object name and only access the methods and properties needed. For example:
With foo
.bar()
.reset(true)
myVar = .getName()
End With
Is there any such syntax within Java?
View 5 Replies
Sep 9, 2010
I am using Visual Studio 2008 and have connected a database correctly as I have done a login that works fine, although when I try to insert information submitted in the text boxes a different table, it doesn't enter after I end program to check it still has no data in.
Dim con As SqlCeConnection = New SqlCeConnection("Data Source=NESdb.sdf")
Dim myDA As SqlCeDataAdapter
Dim myDataSet As DataSet
[code]....
View 3 Replies