Convert Properties in vb6 to C#
-
Private objTestStr As String
Public Property Get TestStr() As String
Set TestStr = objTestStr
End Propertyhow will the code looks like in c#?
In C# you can create a Property in multiple ways : Full blown Property :
private string testString;
public string TestString
{
get
{
return testString;
}
set
{
testString = value;
//Generally when you use full blown properties
//you do so because you need to do some sort of
//validation or other testing or to set other
//properties or fields as well
}
}Or you can use Auto Implemented Properties:
public string TestString { get; set; };
For a good explanation of C# properties see the MSDN page here[^]
Everyone dies - but not everyone lives
-
In C# you can create a Property in multiple ways : Full blown Property :
private string testString;
public string TestString
{
get
{
return testString;
}
set
{
testString = value;
//Generally when you use full blown properties
//you do so because you need to do some sort of
//validation or other testing or to set other
//properties or fields as well
}
}Or you can use Auto Implemented Properties:
public string TestString { get; set; };
For a good explanation of C# properties see the MSDN page here[^]
Everyone dies - but not everyone lives
-
Private objTestStr As String
Public Property Get TestStr() As String
Set TestStr = objTestStr
End Propertyhow will the code looks like in c#?
Your code shows a read-only property.
private string objTestStr;
public string TestStr
{
get { return objTestStr; }
}Well, naming conventions differ a little, _TestString would be preferred instead of objTestStr. You could also use:
public string TestStr { get; private set; }