Jump to content
Xtreme .Net Talk

Bucky

*Experts*
  • Posts

    803
  • Joined

  • Last visited

Everything posted by Bucky

  1. If that's the case, the best method I can think of is to throw an exception (preferrably create your own) inside the method and then just catch it outside the method call.
  2. I'd reccomend returning -1 for an index that could not be found. This is how index-returning methods of the .NET framework handle the situation. Just check the return value to see if it's -1 before continuing.
  3. Ah, okay. Use the Return keyword to return the array from the function, instead of setting sbpGoal = nsPanel: Return nsPanel
  4. What is the problem you are having? I can spot one error with the code: nsPanel() should be declared as nsPanel(2), which will set its length to 3 (items 0, 1, and 2).
  5. [mshelp=ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/vbcon/html/vbconPrevInstancePropertyChangesInVisualBasicNET.htm]Click the little arrow next to me -->[/mshelp]
  6. How about StatusBar.Panels.Count ? :)
  7. The .NET Framework must be installed on the client machine for the application to run. The Framework does not run in the background of the computer; it is merely a set of runtimes that are executed when the application runs.
  8. You need to enclose the A in single quote marks to pass it as a Char type instead of as a String. int myAsc = Convert.ToInt32('A'); In VB.NET, this is accomplished with a trailing c after the quote marks: Dim myInt As Integer = Convert.ToInt32("A"c)
  9. Having seen multiple questions regarding showing forms and passing variable, I thought it'd be necesarry to explain the differences of using Forms in VB6 and VB.NET. This'll also take a little work off people having to explain this idea more than once. Please feel free to leave any comments and suggestions. I'd like to know if the tutorial is too wordy, and if I should shorten it. ------- Forms in VB.NET This short and simple tutorial is long overdue, and hopefully it will clear up confusion regarding the use of forms in VB.NET, compared to their use in VB6. This tutorial assumes the reader's knowledge of basic VB.NET syntax and the basics of object-oriented programming. This tutorial is intended to answer such questions as: Why can't I show (name of form) just by using (name of form).Show()? How can my form access variables that are in other places? What is the answer to Life, the Universe, and Everything? Visual Basic 6 handled (note the use of the past-tense) forms in a secretive, almost mysterious way. Forms were this magical entity that could be shown and hidden at a moment's notice. They had some qualities of classes, but they still hid a lot of functionality from the user. The coder transferring from VB6 to VB.NET must think of forms in an entirely new way. A Form in VB.NET (and indeed, the entire .NET Framework) is now a standard class module. A Form acts as any other class does: a new instance of the Form class is initialized, the instance is manipulated and used, and then it is disposed of. This is very important to understanding that everything you know about forms in VB6 is wrong. ;) Working With Forms That being said, here is a short example of a Form (Form1) opening another Form (Form2) upon the click of a button (Button1). Public Class Form1 Inherits System.Windows.Forms.Form ' Windows Forms Designer generated code region Private otherForm As New Form2() Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click otherForm.Text = "Foo" otherForm.Show() End Sub End Class Here's where the difference of showing forms comes into play. In the General Declarations area of Form1, I declared a new instance of the Form2 class. The local variable otherForm is accessible to any subroutines inside the Form1 class. The Text property of the form was changed to show how other members of otherForm can be accessed. Passing Values Between Forms If forms are so disconnected, how are values passed between them? You can't just get the value of Form1.VariableName from Form2, because that's how VB6 did it, and thus VB.NET does it differently. If you can get into this state of mind, then you will pick up the changes in VB.NET quickly. :) Now, I bet you're wondering, why not just create some public variables in a module and call it a day? Like what was just mentioned, this is how VB6 does things, and thus it is wrong. Modules should be considered obsolete, because they go against the OOP paradigm. If you want to create members accessible to your entire project, create them as static (Shared in VB.NET) members of a class. Here, however, it makes more sense to pass the values to the Form's constructor. Because Forms are classes, you are free, as an inheritor of the class, to override or overload its constructor. Using this technique, you can overload the constructor of a Form to accept variables being passed to it. Take the Form1/Form2 example again. Here, the code for both Form1 and Form2 is shown. Public Class Form1 Inherits System.Windows.Forms.Form ' Windows Forms Designer generated code region Private otherForm As New Form2("Foo") Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click otherForm.Show() End Sub End Class ' ---------------------- Public Class Form2 Inherits System.Windows.Forms.Form ' Windows Forms Designer generated code region Private param As String Public Sub New(ByVal newParameter As String) MyBase.New() InitializeComponent() param = newParameter End Sub Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click MessageBox.Show(param) End Sub End Class The only change in Form1 is that the declaration of Form2 passes the string "Foo" to its constructor. In Form2's code, the constructor has been overloaded (the original Sub New is inside the Windows Forms Designer code region) to accept a string and set it to a local variable. Then, when Button1 is clicked on Form2, the variable is displayed to the user. Any type and number of variables can be passed to constructors, including other Form instances (if, for example, you wanted to refer back to Form1 from Form2). It's important to note that the first two lines of the overloaded constructor are required in any constructor, for they make sure that the form and all its control initialize properly. The InitializeComponent sub is, not surprisingly, also in the code region. Let's say that you wanted to be able to access all the members of Form1 from Form2. Instead of passing a string the constructor, like in the example, you would pass a parameter of the Form (or Form1) type instead. Here's the revised code. Form2 now shows the contents of TextBox1 on Form1 to the user when the button is clicked. Note how Form1 passes itself (Me) to the constructor of Form2, and that Form2's constructor now accepts a variable of type Form1. Public Class Form1 Inherits System.Windows.Forms.Form ' Windows Forms Designer generated code region Private otherForm As New Form2(Me) Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click otherForm.Show() End Sub End Class ' ---------------------- Public Class Form2 Inherits System.Windows.Forms.Form ' Windows Forms Designer generated code region Private callingForm As Form1 Public Sub New(ByVal newCallingForm As Form1) MyBase.New() InitializeComponent() callingForm = newCallingForm End Sub Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click MessageBox.Show(callingForm.TextBox1.Text) End Sub End Class Summary When working with forms, remember that: Forms are merely classes that can be manipulated in any way a class can. To pass values to a Form (including another Form), overload the constructor of the Form to accept these values. If you try to do something in VB.NET using a VB6 technique, chances are it won't work and/or it's bad coding practice. Find a new way. The answer is 42. Feel free to PM me with any questions or comments. Happy coding!
  10. Have you tried using Attributes.Add in this manner? lblError.Attributes.Add("style", "text-align:center;")
  11. First you need to convert the Text, which is a string, into a number that can be compared numerically. Then use a combination of the greater-than (>) and less-than (<) operators to check for betweenness. Dim value As Integer value = Convert.ToInt32(TextBox1.Text) ' Convert to Integer If value > 1 And value < 5 Then Label1.Text = "You are between 1 and 5" End if If you want it to be between 1 and 5 inclusive, then use the >= and <= operators, instead.
  12. A "side effect" would be changing the value that the user passed into a function without them expecting it, which is not usually done in a property accessor. Like Robby said, functions should be used to change and return values. For example, it's more advisable to have a Disconnect() method than to have the user set the Connected property to false.
  13. Any code that eats up all the processor's power should be placed in its own thread to execute. Check out [mshelp=ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconthreading.htm]Threading[/mshelp] to create a thread to carry out your laborious task. Then the animation, among other functions of your computer, will be, uh, functional.
  14. In the ASP.NET design layout, under the "HTML Controls" tab of the toolbox, add a Hidden control to the page. You can programmatically access this control on the server-side (through ASP.NET's code-behind), and then access it similarly in JavaScript scripts on the client-side. Make sure that, after you add the control to the page, you right-click it an choose "Run as server control" so that is posts back to the server properly.
  15. If the image is small enough, just store the entire image in a variable. If you want to undo the action, Dispose of the current image and set the Graphics object to the old image. Otherwise you could just draw right over the line with the same points and thickness, but with a color that matches it with its background.
  16. Bucky

    Memory Leak

    How quickly is the timer's interval set for? And after how long does the error occur?
  17. Bucky

    Dates in C#

    What determines what a "month" is? If the starting and ending dates were both in the middle of a month, there's no way to determine what "month" passed, if any at all. If "Uno" is always going to be the start of a month, then just subtract the difference in the months, add the difference of the years multiplied by twleve (to account for different years), then add in the days. So, in summary, with all the assumptions made above (pseudo-code): Months = Dos.Months - Uno.Months + (12 * (Dos.Year - Uno.Year)) Days = Dos.Days Or, if you want to assume that a "month" is always 30 days (which you've done here, even though January has 31 days), just do: Months = xx.Days \ 30 Days = xx.Days Mod 30
  18. Each language compiles to an intermediate language, MSIL, that is language-independent. You can actually view this language by using the .NET Framework SDK tool ILDASM. This is the code that the framework executes when an app runs. Often there will be slight differences in how code is converted to MSIL, but it is not significant in terms of speed.
  19. Well if the project name and the namespace the class is in should be the same, then just remove the namespace daclaration from the code file and the class will be in your project's namespace, KIF.
  20. You don't need to include the DLL; if the code file is in the same project, the code will compile right into the app and you can use it. Another thing I forgot to point out is that you might need to fully qualify the function by including its namespace, also.
  21. Put the functions inside a class, and declare the functions as Shared. Then you can use them without having to declare an instance of the class. So, here's your code... Public Class Whatever Public Shared Function Something() As String ' Something End Function End Class And here's the HTML: <%=Whatever.Something()%> The <% %> code blocks don't even need to be within the <form> tag. Also, if this is all you're doing with them I suggest you use a Label control and set its Text property in the Load event of the Page, instead.
  22. textBox2.Text = table.Item[key]
  23. The {0} thing is not specific to WriteLine(); it is a feature of the [mshelp=ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfSystemStringClassFormatTopic.htm]String.Format()[/mshelp] method, which Console.WriteLine() conveniently calls for you. You can include more parameters by specifying {1}, {2}, etc. and passing more parameters to the function.
  24. Have your program create a batch file that contains all the DOS commands you want to execute. Then use Process.Start() to run the batch.
  25. Yes, but then I cannot call Close and then Listen on the same port, because a socket will still be bound to that port.
×
×
  • Create New...