Implement Class Constructor In VB?
Jul 19, 2010I just would like to know how to implement class constructor in this language.
View 2 RepliesI just would like to know how to implement class constructor in this language.
View 2 RepliesI have an abstract class which requires a delegate to function. I pass the delegate into the constructor. Now that I have a non default constructor I need to call the abstract class's constructors from the concrete class which means that I need to use MyBase.New(...). I have included a quick example below.
Public MustInherit Class BaseClass
Public Delegate Sub WorkMethod()
Private _Work As WorkMethod
[code]....
I have tried to do this but I keep getting the following error: "Implicit reference to object under construction is not valid when calling another constructor".Can I not do what I am trying to do above? I initially had the delegate setup in its own setter method. But then I am creating a deceptive API because it does require a point to a method to work properly.
I have just started using MVVM design pattern with unity and I'm strugling to work out how you add a constructor.my code currently works if I comment out my constructor that requires input the view gets added to the shell.
Protected Overrides Sub ConfigureModuleCatalog()
MyBase.ConfigureModuleCatalog()
Dim moduleCatalog As ModuleCatalog = CType(Me.ModuleCatalog, ModuleCatalog)
moduleCatalog.AddModule(GetType(GridModule))
End Sub
how do I pass in variables into my GridModule ?
I have some classes, BsfciFile and StudyFlashCard. Bsfci is the extension to which I save my set of flashcards in an INI format. I am currently working to transform my code from using Windows API calls to access the INI to using a IniFile class that I found on the internet. I would like the BsfciFile to have a Array of StudyFlashCard objects, but I would like the StudyFlashCard class to use the IniFile class object contained in the BsfciFile class. I can pass the IniFile from the BsfciFile class to the constructor for the StudyFlashCard class, but I want it to modify the same IniFile as the BsfciFile class has later on.
[Code]...
I need to create unit testing project for my current website. The currentw ebsite si written in VB. All unit testing examples are using interface to create mock object. My current VB class does not implment any interface. Can I add interface and implement it to my current class and functions without affecting or changing codes to any pages in my website that call the functions? For examples my current class is like:
[Code]...
Suppose I have piece of code like this:
Public Interface ISomething
....
End Interface
[code]....
I am trying to make the properties of class which can only be set through the constructor of the same class
View 7 RepliesI am working on a general helper class to sort ListView SubItems. I wrote a base class that has much of the code I need. It includes a MustOverride for the Compare method so that the various inherited classes can implment their own comparisons based upon their type. For the value types, I end up with very similar code such as the following, where x and y are ListViewItems: Public Overloads Overrides Function Compare(ByVal x As Object, ByVal y As Object, ByVal sortColumnIndex As Integer, ByVal sortOrder As System.Windows.Forms.SortOrder) As Integer [code]
View 3 Repliesi'm working with different classes and different constructors. It's working fine until i try to inherit a class that also has a constructor. vb asks for mybase.new but when i add that vb says that a constructor cannot call itself, when i try it in a different way vb asks the parameters of the constructor of the class that i want to inherit.
View 11 Repliesi've declared a bitmap at class level + i want to load an image into that variable in the constructor. i don't know why but it won't work and everything i've tried won't work either.am i missing a reference or something?
[Code]...
I have an abstract class in vb.net with two subclasses. In the abstract class I have a constuctor that looks like this:[code]I would like to create a second constructor that doesn't take any arguments and just initializes the args to default values.[code]When I attempt to create a new subclass using the second constructor the compiler complains that I'm missing two args to the constructor.Is there a reason I can't overload the constructor in the abstract class?
View 2 RepliesI have a custom collection class that I would like to pre-populate with data from the database. Basically it would search for "submembers" that are related to the "member" data record using the "member"'s primary key id.
I figured the best way to do this would be in the constructor of the class, but I am not so sure now. The collection class does not have any properties (the member class does).[code]...
I'm making a class that reads a file and processes data in it, but I need some error handling. I'm giving it a constructor that takes a file location and processes that file. How can I make it so the constructor tells the program whether it found the file or not? Optimistically the program that calls this class would check for that first, but since I'm not going to be the only one that uses this class I'd like to know how to secure such a thing. It's probably bad practice to do this within the class, but I'd like to be sure it's as portable as possible.
View 6 RepliesI discovered that an event raised (directly on indirectly) in the constructor cannot be handled outside the very class. To prove if that was the actual problem, I wrote a simple exemplary app.
Class with the event:
Namespace Utils
Public Class A
Public Event Test()
Public Sub New()
CallTest()
[Code] .....
The reason of such behavior became quite obvious after writing those pieces, but maybe there is a way to omit it?
How do I force the Visual Studio compiler to generate an error when a required method is not being called in the constructor of a child class? Like when you edit the form designer code, the compiler complains when InitializeComponent()isn't the first call in the constructor of a form. Is this even possible in VB.NET?
View 2 RepliesIn the past when I have written classes and constructors, I named the variables in the constructor parameter something different than what would have been stored in the actual class itself.What I do now is name them the same, and reference the internal variables with Me.varname.Here is a class I just started building.Is my naming convention incorrect? [code]
View 1 RepliesI am trying to write some unit tests on a class that looks like this, using Moq [code] the parameterized constructor is private or internal one of the two methods relies on the result of the other.I want to check that when GetThisOrThat is called with a value, it actually calls GetThis. But I also want to mock GetThis so that it returns a specific well-known value.To me this is an example of Partial Mocking, where we create a Mock based on a class, passing the parameters for the constructor. The problem here is that there is no public constructor, and therefore, Moq can not call it..I tried using the Accessors generated by Visual Studio for MSTest, and use those accessors for the mocking, and this is what I came up with [code]
View 1 Replies[Code]...
Is there an explanation for this limitation? I mean, it's still in the constructor for the object, and the base class constructor is still executed first.
I'm doing a lab for school, and I came across something I have never done before: create a default constructor within my class. It involves creating a private field to store the connection string, then create a default constructor that sets the connection string.
Here is what I have so far:
Public Class Appointments
Private sqlconnection As String = ConfigurationSettings.AppSettings("ConnectionString")
Private Property connectionstring() As String
Get
Return sqlconnection
[Code] .....
I have a base class which must be overriden. This base class sits in one dll shared by all. The derived classes will each be in their own dll such that the addition of a new derived class can be done by distributing a new dll. The set of dlls, and therefore the set of derived classes, will be discovered by the main program on startup. That's all pretty straightforward. The problem is that the main program needs to be able to query a database to figure out which type of derived class it needs, then create a class of that type. Naturally, it can't know which types will be available ahead of time because even I don't know that. The program has to be dynamically extensible.The obvious way to do this is to have an interface in the dll that is implemented by a class with a default constructor. The sole purpose of that class would be to return an object of the type (one of the derived types) defined in that dll. The derived types can't really be this class, because they can't have a default constructor since the base class doesn't have a default constructor, and the derived class can't make up the arguments that the base class constructor needs.
So basically, each dll that houses a derived class would also house a simple class that implemented an interface for the sole purpose of creating that particular derived class. The main program would examine the dll to find a class that implemented the interface, and once it found one, it could ask it what type of class it created, or it could ask it to create an instance of that class. It seems like there ought to be an easier way to dynamically add new classes to the project. It seems like the derived class ought to be able to tell the main project what type it was. This could be done, except that the derived class would have to exist before any of its methods could be called unless the methods were shared. Since you can't have a shared method in an interface, the interface itself would make no sense at that point, in which case I would need to discover whether the class in the dll derived from the base class in the second dll, just to figure out whether or not I wanted to create it. That's getting convoluted by now, so I guess I'll leave it there. The basic point is that the base class is known to the main program from a common dll. The number and types of derived classes can't be known at this time, so the program has to be able to discover them dynamically. Each derived class will sit in its own dll (or there could be more than one in a dll), and has to be discovered, and instances created, as needed, by the main program.
Implementing a simple MVC in vb. The controller instance creates and holds instances of the model and view:
Public Class Controller
Public Property myModel As Model
Public Property myView As View
[code].....
class inherit from another class and implement an interface?
View 4 RepliesI was looking on the interweb to see if there were any good examples on how to initialize shared members within a class while still initializing instance variables. I did find an expression that might fit to the answer:
[code]...
How do I initialize both instance and shared members without re-initializing the shared members every time an object is created from a class? Thanks!
I have a class inside a class.I need to expose the properties of the 2nd class to other classes, therefore it is public.However, I do not wish other classes to be able to create instances of this 2nd class, it should only be instantitated from its parent class.I thought I could resolve this issue by making the constructor of the 2nd class private, but this even prevents the parent class from instantiating its child class! How can I work around this, do I have to make the constructor public and allow anybody to create an instance of the class?
View 3 RepliesI have a userControl11 (either in winform or wpf) which has a ValueChanged custom event. If I put it in client form and in form_load set its value to 100, it will trigger the ValueChanged event. But if I set this value inside the constructor of UserControl1 the custom event won't trigger. How can I force it to do so ?whatever the technical reason, functionally it does make sense. If the object is initializing its value from some sources unknown to the client form and the client form has a textbox bound to this usercontrol value, it is sure convenient that it could refresh its textbox at any time including when the form loads just using one single event handler. Without this the client form has to create another initializer for this bound textbox at form load.
Below the source code of my trials in winform:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
[code]....
I wish to put the XMLSerializer behind a 'facade' class of mine so that user wont have to supply type info to the constructor. But doing this has a problem. Consider this class:
Class XmlFormatter
Private Shared xs As XmlSerializer
Public Function Deserialize(ByVal serializationStream As Stream) As Object
Dim o As Object = Nothing
[code]....
The problem is that the user of this class cannot use Deserialize without first using Serialize because the XMLSerializer instance is created in Serialize and it is shared. But using Deserialize without this instance will simply return Nothing.
what's the rationale behind this limitation: Constructor call is valid only as the first statement in an instance constructor i want to pass an argument to my constructor which validates this argument and calls mybase.new according to this argument but it doesn't let me
example:
Public Class prob
Inherits System.ApplicationException
Public Sub New(ByVal problem As String, ByRef inner_exception As Exception)
[code]....
I want to modify the base Array class by creating a new class called "FileArray".I have some text files that are read into a string array. Each element in the array is a line from the text file. I want to update the GetValue method by overloading it or overriding it so that it will update some value that will give an indication to what the last line was. I have a couple of ideas on how I can do this:
1) Use a public integer, like intLine, that gets updated everytime GetValue is called.
2) Create a new property of Array, such as "LastLine" that would return the last line number (which would basically be the last index value +1) and then use a public array to store the files (though this is less desirable as the array may be used by several different functions)
An easier way would be to be able to determine the last element that was returned, which is basically what I think I am describing above.
Say I want to know more about a class mshtml.htmlinputelement for example.Say I want to know. What is his parent classes? What interface the class implement? How do I do so through object browser?
View 1 RepliesI am trying to build a class. I am having a difficult time conceptualizing how a class is built and how it works. I want to be able to build a USEFUL class and implement it in an application.
View 1 Replies