C# Vb: When They Say Static Classes Should Not Have State?
Apr 23, 2011when they say static classes should not have state/side effects does that mean:
static void F(Human h)
{
h.Name = "asd";
[code].....
when they say static classes should not have state/side effects does that mean:
static void F(Human h)
{
h.Name = "asd";
[code].....
[code]Now how do i do this in c#. I know there is not handles clause in c# so i need some function that is called and assign the event handlers there. However since its a shared class there is no constructor i must put it somewhere outside of a function.
View 2 RepliesThis is a problem. Because in VB terms its a class that CAN benstantiated(although only internally) and you can create member functions in the class. This is not possible in CSharp. Here you create a static class and if you create a member function the compiler will output an error.And who knows... somewhere down the line another developer picks up my class and starts creating shared functions that return instances of that class. I know this might be a design/documentation problem but it can still happen.
View 9 RepliesI have a base class with several derived classes. I want all of my derived classes to have the same Public Shared (static) method with their own implementation. How do I do this? Is it even possible?
View 3 RepliesIf I have a 'Shared' object defined in the App_Code folder; is that object shared amongst all users/visitors of my website? Or is it shared on a per-user basis?I don't know if that makes sense or not; but I've got a puzzle/solver I'd written earlier that I'm trying to incorporate into a simple ASP.Net site. It's probably poorly written, but I'd used a Shared boolean to determine if a solution had been found. The code works, now as an ASP.NET site, but if two visitors to my site were both validating their puzzle at the same time, would they both be accessing the same boolean?
View 3 RepliesI have two classes, one nested in the other. [code]Neither "Name" or "ID" are unique between operations and records.I wish to construct a dictionary using LINQ = Dictionary(Of String, Of List(Of Integer), whereby the keys are uniqe examples of Names in my collection and the values are the collective set of distinct IDs that are associated with those names.
View 2 RepliesHow can I structure my classes so that the user interfaces though a single class while the supporting classes are hidden from their view? I think its best understood in an example:
Public Class MyInterface
Public Economic as EconomicClass
Public Sub New()
MyBase.New()
[code].....
So you might ask why am I even separating them? It's strictly for others who will be working with this interface. I need to funnel them though a logical structure:
interface.Economic.MyMethod
interface.Currency.MyMethod
etc
This way everything is already handled for them in the background and they only need to run the method they need. I don't know if I can have it both ways in VB.NET.
Here is some example code of what I mean:
vb
Public Class Form1
Private Sub Button1_Click(ByVal sender as Object, e as systemEventArgs) Handles Button1.Click
[Code]....
I m trying to raise Click event of Textbox explictly but I m getting "Derived classes cannot raise base classes" error.[code]....
View 1 Repliesso far i got
code
For Each item As Reflection.FieldInfo In GetType(NameSpace.ClassWithNestedClasses).GetFields
rtfAppend(item.Name & ":" & Tab & item.GetValue(Me))
Next
For Each item As Reflection.PropertyInfo In GetType(NameSpace.ClassWithNestedClasses).GetProperties()
[code]...
which gets me the simple string vars and properties of my top class, but how can i apply this to loop through all sub classes and get there vars and props?
Searching the internet finds no less than 700 different ways and opinions to accomplish what I'm trying to do, and I would just like to know the simplest, most bullet-proof way possible.
[Code]...
I simply want to extract the string "bar," which is inside <hostName></hostName>I can do this easily enough with simple string functions, but I'd like to do it with one of the .NET XML classes/methods.
I am rewriting a C# class in VB. The C# class has a static constructor in it:
static Class()
{
...
}
which, through the Call Stack, I found is called by the main window's InitializeComponent() method.Everywhere I've looked has told me that the corresponding static constructor in VB is just:Shared Sub New()
but this method is never invoked. Am I creating my VB static constructor right? Is it likely something else that has nothing to do with my static constructor?
I am writing a code for a webapplication in vb.net. I am using a shared variable in the vb code.
Consider the static variable value is 3 for a user who hits the web application. Again when another user hits the same web application, whether the static variable will be 3 again or whether he will have a separate value for static variable for his session?
This will probably be a basic questions for the LINQ & architecture experts however I am failing to understand a problem i've encounted when trying to update a 'Static ObservableCollection.
Me.Grid1.ItemsSource = ContactList Me.Grid2.ItemsSource = From s In ContactList Where s.ContactTypes.Any(Function(t) t.ContactTypeName = "Christmas List")
If I add a new Contact with the ContactType "Christmas List" to the ContactList ObservableCollection, Grid1 reflects the additional Contact however Grid2 does not reflect the change unless I rebind.
Anyway to Reflect the change in Grid2 to show the new Contact with the queried ContactType
We have a DateTime Service that pulls the Database date down when we need to use a Date. Now, If I want to run a test using "Todays" Date using FitNesse, What would be the easiest way of creating a way to have (OurService).CurrentDate be static? We have a control that ads Days,Years, or Months to the current date.. and I would like to be able to test this.
View 1 RepliesI am currently using Visual Basics 2010 (Ultimate), and I am trying to modify a free-project listed here:I want the above project to use these hard-coded values below:Key= "087CF262E0E856B8F911B369EE027CF9330F6288343651B1198379A9813BC8C4"
IV = "11D2A07242B79C752E6B53CDD14792C9"So basically i want to encrypt AND decrypt with the above keys.
I have the following code:
Public Class TestClass
Public Sub Main()
If theGlobal IsNot Nothing Then Throw New Exception("What gives!")
End Sub
Private Shared theGlobal As Object = Nothing
[Code] .....
Why is theGlobal object NOT Nothing?
how to limit static integre.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Static a As Integer[code]......
In the following example GetList returns an instance of a static (shared) variable. That one needs locking in order to be thread-safe.But what about DoSomething which doesn't use any static variables outside the method? Does it need locking too?I'd like to clarify that in this particular case I expect DoSomething to always print 0-100 in sequence (i.e. no 0123745...) regardless of number of calling threads. Or, in general, that different threads don't affect each other's variables (printing to console is only an example). The language in the following example is VB.NET.[code]
View 3 RepliesIm trying to load a google static map in a winform. I found a control online that would helpme but i cant get it working. i got the control from Here. i was not able to drag on the control on to the form like others. i have also email the dev of the control but have not heard back yet.
[Code]...
Do vb.net static variables work on an IIS web garden?
View 1 RepliesI have an excel sheet developed by a vendor that I'm stuck with the current functionality as a short term solution. The users workflow have them pull 4 separate sets of data which they are copying and pasting into a different spreadsheet where they append the data to previous days' pricing for trend analysis. That macro is built looking for the first blank row in column A, simple enough.
However, they then add the current day's date to this other spreadsheet as an additional column to do that trending. I found a nice link to a site that mentioned I could do this logic by adding code by right clicking the sheet name and selecting 'View Code'. I then entered the following logic that states if column A is populated, populate Column T with today's date statically (Today and Now don't work because I need it to stay at that date when it is refreshed). My issue is that this works great if I enter test data into cell A and hit enter. However it is not populating the date if a copy and paste is executed into the column A. Is there something I'm missing or a recommendation to have this work with copy/paste?
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("A2:A15000")) Is Nothing Then
[code].....
what is the Equivalent of a Static class in VB.Net. Is it Modules?And When i used one of the C# to VB.Net conversion tools then the code "Public Static class Class1 " was converted to "Public NotInheritable Class Class1"
View 1 RepliesI have a C# method in my datatier that I am trying to convert to VB.Net. I converted it to VB.Net but when I bring up the datatier class the method is not showing. It has been a long time since i have used VB.Net and forgot alot of things [code]I believe it has to do with the declaration, which i have as:Public Static Function UserActInfo(ByVal _eno As String) As useraccount..
View 1 RepliesI have three classes Employee, Manager and Salesman. Manager and Salesman classes inherits Employee class.
Employee :
Public MustInherit Class Employee
' Field data.
Protected empName As String
Protected empID As Integer
Protected currPay As Single
[CODE]...
Manager :
Public Class Manager
Inherits Employee
[CODE]...
Salesman :
Public Class Salesman
Inherits Employee
[CODE]...
Now I have created a object of salesman and manager using the following code:
Dim objSalesMan as Employee=new Salesman("xyz",1,2000,5000)
Dim objManager as Employee=new Manager("abx",2,5000,"production")
Is this a good programming pratice or should I use:
Dim objSalesMan as new Salesman("xyz",1,2000,5000)
Dim objManager as new Manager("abx",2,5000,"production")
Is this code correct or is there any possibility of some random threading deadlocks etc?
Is it a good idea to use static properties and locking together? Or is static property thread-safe anyway?
Private Shared _CompiledRegExes As List(Of Regex)
Private Shared Regexes() As String = {"test1.Regex", "test2.Regex"}
Private Shared RegExSetupLock As New Object
[Code]....
If it's not obvious code is compiling regexes and storing in a List(Of Regex) so they can run faster. And it's shared so every instance of class can get benefit out of it.
I have a field which is static and readonly. The requirement is that the value should be allocated to the field at the login time and after that it should be readonly. How can i achieve this ?
public static class Constant
{
public static readonly string name;
}
is there a difference in performance when making properties static/shared that were instance properties before, maybe any locking mechanisms?There is a heavily used object in HttpCache which was accessible through a property in a page-instance(Service.aspx). Now i wonder if it would be better to make it static because the HttpCache is shared across the application anyway.
View 4 RepliesI've got a shared class (static in C#) which mostly carries some settings data that any class in the application can read and sometimes write. Also there are some static properties which holds some internal states.
Now I want to revert this class to initial stage of it. With all default variables etc. Assume that the user want to reset the current state and start over without restarting the application.
In a singleton model I'd simply renew it with something like this :
Public Sub Reset()
_Instance = New MyClass()
End Sub
However this is not possible in a Shared class. Or should I switch back to Singleton?
I Want to know which one is preferred while coding to use Static Methods or normal instances, I prefer to use static if they where few but if there was many of them I start to get some doubts
[code]...
if the EmployeeManager Has Many methods (selects deletes updates) is it ok to make them all static.and if it was Normal instance. wouldn't be a drawback if the object is initiated every time specially if GetAllEmployees() is heavily used.What is the better approach to use?