Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9244153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:57:35+00:00 2026-06-18T08:57:35+00:00

I come from an Objective-C background, and I’m writing some VB.NET that includes a

  • 0

I come from an Objective-C background, and I’m writing some VB.NET that includes a delegate. I have a class Worker that performs some task. One of the properties of this class is an ILogger object, which has a method for appending to some log file. (I want my Worker object to be decoupled from the logging, which will be handled by my GUI.) In Objective-C I could just declare

@property (nonatomic, strong) id<LoggingDelegate> logger;

and then sprinkle my code with lines like

[logger appendToLog:@"Beginning polarity reversal..."];

In Objective-C, logger doesn’t actually need to be set to an object for this to work; if it’s a null reference (“nil” in Objective-C), the appendToLog: message is silently ignored by the runtime.

In VB.NET I can do something similar:

Public Property logger As ILogger

and

logger.AppendText("Beginning polarity reversal...")

But of course, logger actually needs to be set to an object for this to work. What’s the idiomatic way to handle the case that I don’t have a logger object attached to my worker? (More generally, is my implementation of the delegate pattern idiomatic in VB.NET?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:57:36+00:00Added an answer on June 18, 2026 at 8:57 am

    Every time you use the optional object, you could check if it’s null first, for instance:

    If logger IsNot Nothing Then
        logger.AppendText("Beginning polarity reversal...")
    End If
    

    Or, you could create a private method that does the extra check for you:

    Private Sub AppendTextToLog(text As String)
        If logger IsNot Nothing Then
            logger.AppendText("Beginning polarity reversal...")
        End If
    End Sub
    

    Or, as vcsjones recommended, you could default the property to a dummy object:

    Public Class MyClass
        Private Class DummyLogger
            Implements ILogger
    
            Public Sub AppendText(text As String) Implements ILogger.AppendText
                ' Do nothing
            End Sub
        End Class
    
        Public Property Logger As ILogger
            Get
                Return _logger
            End Get
            Set(ByVal value As ILogger)
                _logger = value
            End Set
        End Property
        Private _logger As ILogger = New DummyLogger()
    
        Public Sub DoWork()
            _logger.AppendText("Beginning polarity reversal...")
        End Sub
    End Class
    

    Or, the closest thing to an “optional delegate” in VB.NET is actually an Event:

    Public Class MyClass
        Public Event AppendText(text As String)
    
        Public Sub DoWork()
            RaiseEvent AppendText("Beginning polarity reversal...")
        End Sub
    End Class
    

    Update

    As of VB 14.0 (Visual Studio 2015), you can now use the ?. operator (it’s called the “null conditional” operator):

    logger?.AppendText("Beginning polarity reversal...")
    

    Or, if you want to use an actual delegate, you can use the same operator with delegates too. So, if you have a delegate variable called appendToLog, you could call:

    appendToLog?.Invoke("Beginning polarity reversal...")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am very new to VB.NET, and I come from an Objective-C background. In
I come from a Java background and am getting more into .NET, what are
I come from a .NET background and need to do a web project in
I come from a PHP background and I'm just getting my teeth into some
I come from java/cpp environment where singletons do have their advantages over class(static) methods,
I am very new to xcode & Objective-C (having come from PHP) i have
I come from a Java background and I am learning Objective C. I am
I come from a C# background and I am learning Objective-C. I was wondering
I have a class that inherits from UIView However when trying to use the
While attempting my first sub-class in Objective-C I have come across the following warning

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.