简体   繁体   中英

Assistance converting vb.net code to c#

I haven't used vb.net in years and need to convert some code to c#. So far it's going ok, but I came across the following code that has me stumped on how to convert it to c#.

Dim UnsavedLogEntries As StringBuilder
UnsavedLogEntries = context.Cache.Item(ObjNameInCache)
UnsavedLogEntries (vbCrLf + LogEntryTimeStamp + UserID + LogEntry)
Dim UnsavedLogEntries As StringBuilder
UnsavedLogEntries = context.Cache.Item(ObjNameInCache)
UnsavedLogEntries.Append(vbCrLf + LogEntryTimeStamp + UserID + LogEntry)

in c# this is the closest equivalent

System.Text.StringBuilder UnsavedLogEntries;
UnsavedLogEntries = (System.Text.StringBuilder) context.Cache.Item[ObjNameInCache]; // ObjNameInCache is some string variable somewhere in your code
UnsavedLogEntries.Append(System.Environment.NewLine + LogEntryTimeStamp + UserID + LogEntry);

from the comments: ... regarding the last line. It should actually be: UnsavedLogEntries.Append ...

Telerik , Tangible , Varycode - there are a number of online and downloadable converters.

I tossed this into Instant C#, which I've used for large code conversions with good accuracy, and this is what it returned:

    StringBuilder UnsavedLogEntries = null;
    UnsavedLogEntries = context.Cache.Item(ObjNameInCache);
    UnsavedLogEntries[Environment.NewLine + LogEntryTimeStamp + UserID + LogEntry];

There are a number of online cheatsheets you can use for conversion help as well.

Good luck with your conversion.

I was able to figure it out. It would translate to the following in C#:

StringBuilder unsavedLogEntries;
unsavedLogEntries = (StringBuilder)context.Cache[ObjNameInCache];
unsavedLogEntries.Append(Environment.NewLine + logEntryTimeStamp + userId + logEntry);

A couple of things also. C# does not have an Item method on Cache. Also I needed to cast it to a StringBuilder to resolve not being able to convert from obj to StringBuilder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM