Search Results

Search found 2 results on 1 pages for 'snmcdonald'.

Page 1/1 | 1 

  • .NET: Interface Problem VB.net Getter Only Interface

    - by snmcdonald
    Why does an interface override a class definition and violate class encapsulation? I have included two samples below, one in C# and one in VB.net? VB.net Module Module1 Sub Main() Dim testInterface As ITest = New TestMe Console.WriteLine(testInterface.Testable) ''// Prints False testInterface.Testable = True ''// Access to Private!!! Console.WriteLine(testInterface.Testable) ''// Prints True Dim testClass As TestMe = New TestMe Console.WriteLine(testClass.Testable) ''// Prints False ''//testClass.Testable = True ''// Compile Error Console.WriteLine(testClass.Testable) ''// Prints False End Sub End Module Public Class TestMe : Implements ITest Private m_testable As Boolean = False Public Property Testable As Boolean Implements ITest.Testable Get Return m_testable End Get Private Set(ByVal value As Boolean) m_testable = value End Set End Property End Class Interface ITest Property Testable As Boolean End Interface C# using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace InterfaceCSTest { class Program { static void Main(string[] args) { ITest testInterface = new TestMe(); Console.WriteLine(testInterface.Testable); testInterface.Testable = true; Console.WriteLine(testInterface.Testable); TestMe testClass = new TestMe(); Console.WriteLine(testClass.Testable); //testClass.Testable = true; Console.WriteLine(testClass.Testable); } } class TestMe : ITest { private bool m_testable = false; public bool Testable { get { return m_testable; } private set { m_testable = value; } } } interface ITest { bool Testable { get; set; } } } More Specifically How do I implement a interface in VB.net that will allow for a private setter. For example in C# I can declare: class TestMe : ITest { private bool m_testable = false; public bool Testable { get { return m_testable; } private set //No Compile Error here! { m_testable = value; } } } interface ITest { bool Testable { get; } } However, if I declare an interface property as readonly in VB.net I cannot create a setter. If I create a VB.net interface as just a plain old property then interface declarations will violate my encapsulation. Public Class TestMe : Implements ITest Private m_testable As Boolean = False Public ReadOnly Property Testable As Boolean Implements ITest.Testable Get Return m_testable End Get Private Set(ByVal value As Boolean) ''//Compile Error m_testable = value End Set End Property End Class Interface ITest ReadOnly Property Testable As Boolean End Interface So my question is, how do I define a getter only Interface in VB.net with proper encapsulation? I figured the first example would have been the best method. However, it appears as if interface definitions overrule class definitions. So I tried to create a getter only (Readonly) property like in C# but it does not work for VB.net. Maybe this is just a limitation of the language?

    Read the article

  • Override Linq-to-Sql Datetime.ToString() Default Convert Values

    - by snmcdonald
    Is it possible to override the default CONVERT style? I would like the default CONVERT function to always return ISO8601 style 126. Steps To Reproduce: DROP TABLE DATES; CREATE TABLE DATES ( ID INT IDENTITY(1,1) PRIMARY KEY, MYDATE DATETIME DEFAULT(GETUTCDATE()) ); INSERT INTO DATES DEFAULT VALUES; INSERT INTO DATES DEFAULT VALUES; INSERT INTO DATES DEFAULT VALUES; INSERT INTO DATES DEFAULT VALUES; SELECT CONVERT(NVARCHAR,MYDATE) AS CONVERTED, CONVERT(NVARCHAR(4000),MYDATE,126) AS ISO, MYDATE FROM DATES WHERE MYDATE LIKE'Feb%' Output: CONVERTED ISO MYDATE --------------------------- ---------------------------- ----------------------- Feb 8 2011 12:17AM 2011-02-08T00:17:03.040 2011-02-08 00:17:03.040 Feb 8 2011 12:17AM 2011-02-08T00:17:03.040 2011-02-08 00:17:03.040 Feb 8 2011 12:17AM 2011-02-08T00:17:03.040 2011-02-08 00:17:03.040 Feb 8 2011 12:17AM 2011-02-08T00:17:03.040 2011-02-08 00:17:03.040 Linq-to-Sql calls CONVERT(NVARCHAR,@p) when I cast ToString(). However, I am displaying all my data in the ISO8601 format. I would like to override the database default if possible to CONVERT(NVARCHAR,@p,126). I am using Dynamic Linq-to-Sql as demoed by ScottGu to process my data. PropertyInfo piField = typeof(T).GetProperty(rule.field); if (piField != null) { Type typeField = piField.PropertyType; if (typeField.IsGenericType && typeField.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) { filter = filter .Select(x => x) .Where(string.Format("{0} != null", rule.field)) .Where(string.Format("{0}.Value.ToString().Contains(\"{1}\")", rule.field, rule.data)); } else { filter = filter .Select(x => x) .Where(string.Format("{0} != null", rule.field)) .Where(string.Format("{0}.ToString().Contains(\"{1}\")", rule.field, rule.data)); } } I was hoping my property would convert the expression from CONVERT(NVARCHAR,@p) to CONVERT(NVARCHAR,@p,126), however I get a NotSupportedException: ... has no supported translation to SQL. public string IsoDate { get { if (SUBMIT_DATE.HasValue) { return SUBMIT_DATE.Value.ToString("o"); } else { return string.Empty; } } }

    Read the article

1